Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/api/internal/handlers/sandbox_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func (a *APIStore) PostSandboxesSandboxIDConnect(c *gin.Context, sandboxID api.S
return
}

if body.Timeout <= 0 {
a.sendAPIStoreError(c, http.StatusBadRequest, "Timeout must be greater than 0")

return
}

timeout := time.Duration(body.Timeout) * time.Second
if timeout > time.Duration(teamInfo.Limits.MaxLengthHours)*time.Hour {
a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Timeout cannot be greater than %d hours", teamInfo.Limits.MaxLengthHours))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,43 @@ func TestSandboxFork_RejectsNonPositiveTimeout(t *testing.T) {
})
}
}

// TestSandboxConnect_RejectsNonPositiveTimeout verifies that POST /sandboxes/{id}/connect
// returns 400 for zero and negative timeout values. The check runs before any
// sandbox-ID length check or snapshot lookup.
func TestSandboxConnect_RejectsNonPositiveTimeout(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
timeout int32
}{
{"zero", 0},
{"negative one", -1},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctx := t.Context()
store := &APIStore{}

recorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(recorder)

body, err := json.Marshal(api.PostSandboxesSandboxIDConnectJSONRequestBody{
Timeout: tc.timeout,
})
require.NoError(t, err)

ginCtx.Request = httptest.NewRequestWithContext(ctx, http.MethodPost, "/sandboxes/abc/connect", bytes.NewReader(body))
ginCtx.Request.Header.Set("Content-Type", "application/json")
auth.SetTeamInfoForTest(t, ginCtx, minimalTeamInfo())

//nolint:contextcheck // handler reads ctx from ginCtx.Request.Context().
store.PostSandboxesSandboxIDConnect(ginCtx, "abc123")

assertBadRequestTimeout(t, recorder)
})
}
}