Skip to content

fix(api): validate timeout is positive in PostSandboxesSandboxIDConnect - #3580

Open
chill-czar wants to merge 1 commit into
e2b-dev:mainfrom
chill-czar:fix/sandbox-connect-timeout-validation
Open

fix(api): validate timeout is positive in PostSandboxesSandboxIDConnect#3580
chill-czar wants to merge 1 commit into
e2b-dev:mainfrom
chill-czar:fix/sandbox-connect-timeout-validation

Conversation

@chill-czar

@chill-czar chill-czar commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Closes #3579

Summary

  • Enforce if body.Timeout <= 0 validation in PostSandboxesSandboxIDConnect (POST /sandboxes/{id}/connect), returning 400 Bad Request ("Timeout must be greater than 0") when timeout is non-positive.
  • Bring PostSandboxesSandboxIDConnect in alignment with PostSandboxes, PostSandboxesSandboxIDResume, and PostSandboxesSandboxIDFork.
  • Add unit test coverage in packages/api/internal/handlers/sandbox_timeout_validation_test.go verifying zero and negative timeout values are rejected early.

Why

In packages/api/internal/handlers/sandbox_connect.go, body.Timeout was only validated against the maximum length limit (MaxLengthHours). Supplying 0 or negative values resulted in a negative/zero sandbox lifetime passed to startSandbox, which immediately terminated the connected sandbox due to the expiration timestamp being in the past.

Diff Overview

 	body, err := ginutils.ParseBody[api.PostSandboxesSandboxIDConnectJSONRequestBody](ctx, c)
 	if err != nil {
 		a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err))
 
 		telemetry.ReportCriticalError(ctx, "error when parsing request", err)
 
 		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))

Test Plan

  • Unit test: go test -v ./packages/api/internal/handlers -run TestSandboxConnect_RejectsNonPositiveTimeout
  • Package test suite passes: go test -v ./packages/api/internal/handlers/...
  • Linter & Formatter pass: make fmt and make lint
  • Confirmed timeout: 0 and timeout: -1 return 400 Bad Request ("Timeout must be greater than 0")

/cc @jakubno @dobrac @ValentaTomas @arkamar @tvi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(api): POST /sandboxes/{id}/connect accepts non-positive timeout values causing immediate termination

1 participant