Skip to content

Commit 8a975fc

Browse files
committed
avoid index out of bounds in api.workspaceBuild
1 parent 29f1d5d commit 8a975fc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

coderd/workspaceapps_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,8 @@ func TestAppSharing(t *testing.T) {
13091309
// Verify that the apps have the correct sharing levels set.
13101310
workspaceBuild, err := client.WorkspaceBuild(ctx, workspace.LatestBuild.ID)
13111311
require.NoError(t, err)
1312+
require.NotEmpty(t, workspaceBuild.Resources, "workspace build has no resources")
1313+
require.NotEmpty(t, workspaceBuild.Resources[0].Agents, "workspace build has no agents")
13121314
agnt = workspaceBuild.Resources[0].Agents[0]
13131315
found := map[string]codersdk.WorkspaceAppSharingLevel{}
13141316
expected := map[string]codersdk.WorkspaceAppSharingLevel{

coderd/workspacebuilds.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ func (api *API) workspaceBuild(rw http.ResponseWriter, r *http.Request) {
5050
return
5151
}
5252

53+
// Ensure we have the job and template version for the workspace build.
54+
// Otherwise we risk a panic in the api.convertWorkspaceBuild call below.
55+
if len(data.jobs) == 0 {
56+
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
57+
Message: "Internal error getting workspace build data.",
58+
Detail: "No job found for workspace build.",
59+
})
60+
return
61+
}
62+
63+
if len(data.templateVersions) == 0 {
64+
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
65+
Message: "Internal error getting workspace build data.",
66+
Detail: "No template version found for workspace build.",
67+
})
68+
return
69+
}
70+
5371
apiBuild, err := api.convertWorkspaceBuild(
5472
workspaceBuild,
5573
workspace,

0 commit comments

Comments
 (0)