Skip to content

fix(shared): enforce valid port boundaries and fix error assertion in proxy target parser - #3431

Open
chill-czar wants to merge 1 commit into
e2b-dev:mainfrom
chill-czar:fix/proxy-host-port-validation-and-tests
Open

fix(shared): enforce valid port boundaries and fix error assertion in proxy target parser#3431
chill-czar wants to merge 1 commit into
e2b-dev:mainfrom
chill-czar:fix/proxy-host-port-validation-and-tests

Conversation

@chill-czar

Copy link
Copy Markdown
Contributor

Closes #3430

Summary

  • Add valid TCP port boundary validation (1 <= port <= 65535) to parseHost and parseHeaders in packages/shared/pkg/proxy/host.go
  • Fix errors.As target type in packages/shared/pkg/proxy/handler.go from *InvalidSandboxPortError (pointer target) to InvalidSandboxPortError (value struct target)
  • Fix test assertion in TestGetTargetFromRequest in packages/shared/pkg/proxy/host_test.go to properly verify InvalidSandboxPortError concrete type
  • Add test cases for port 0 and out-of-range ports (65536, 70000) for both URL subdomains and E2b-Sandbox-Port headers in host_test.go
  • Add TestProxyInvalidSandboxPortError integration test in packages/shared/pkg/proxy/proxy_test.go asserting HTTP 400 Bad Request ("Invalid sandbox port")

Why

  1. parseHost and parseHeaders parsed ports using strconv.ParseUint into uint64 without enforcing valid TCP port boundaries. Passing port 0 (e.g. 0-sandboxid.e2b.app or E2b-Sandbox-Port: 0) or out-of-range ports returned port = 0, causing downstream proxy dialers to attempt invalid dials to port 0.
  2. In handler.go, var invalidPortErr *InvalidSandboxPortError was passed as a target to errors.As. Because host.go returns InvalidSandboxPortError as a value struct (InvalidSandboxPortError{...}), double-pointer indirection (**InvalidSandboxPortError) caused errors.As to return false for all invalid port errors. This allowed invalid port errors to fall through to uncaught error handling rather than returning 400 Bad Request.
  3. In host_test.go, require.ErrorAs(t, err, &tt.wantErrIs) executed errors.As(err, (*error)) against a nil error interface, which matched any non-nil error and allowed error-type mismatches to pass silently.

Test Plan

  • Unit tests pass: go test -v ./packages/shared/pkg/proxy/...
  • Tested port 0 and > 65535 handling returns InvalidSandboxPortError
  • Tested proxy handler returns HTTP 400 Bad Request ("Invalid sandbox port") when InvalidSandboxPortError occurs
  • Confirmed require.ErrorAs correctly asserts error types in TestGetTargetFromRequest
  • No regressions in valid sandbox routing or proxy connection pooling

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

@chill-czar

chill-czar commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@jakubno @arkamar

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(shared): missing port range validation and broken errors.As match bypass proxy 400 Bad Request

1 participant