fix(shared): missing port range validation and broken errors.As match bypass proxy 400 Bad Request
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 1.6k
- Forks
- 438
- PR merge metrics
- No merged PRs in 30d
Description
Problem
Edge client proxy request target parsing in packages/shared/pkg/proxy/host.go accepts invalid port values (port 0 or ports exceeding 65535), returning port = 0 or out-of-range ports to downstream proxy dialers. Furthermore, when InvalidSandboxPortError is returned, packages/shared/pkg/proxy/handler.go fails to match it in errors.As due to a pointer-type mismatch (*InvalidSandboxPortError vs value struct), causing invalid port requests to fall through to uncaught error handling rather than returning 400 Bad Request.
// Observed behavior when requesting port 0 or >65535:
// GetTargetFromRequest returns sandboxID, port=0, err=nil
// handler fails errors.As match and returns 500 / unhandled error instead of 400 Bad Request "Invalid sandbox port"
In addition, TestGetTargetFromRequest in packages/shared/pkg/proxy/host_test.go evaluated require.ErrorAs(t, err, &tt.wantErrIs) against a nil error interface, which matched any non-nil error and allowed error-type mismatches to pass silently.
Root Cause
parseHostandparseHeadersinhost.goparsed ports usingstrconv.ParseUintwithout enforcing valid TCP port boundaries (1 <= port <= 65535).handler.godeclaredvar invalidPortErr *InvalidSandboxPortErroras a pointer target forerrors.As. Becausehost.goconstructsInvalidSandboxPortErroras a value struct (InvalidSandboxPortError{...}),errors.Asevaluated tofalsedue to double pointer indirection (**InvalidSandboxPortError).host_test.gopassed&tt.wantErrIs(*error) torequire.ErrorAs, which executeserrors.As(err, (*error))and matches any non-nil error regardless of concrete error type.
| Setting / Factor | Current Value / State | Intended / Expected |
|---|---|---|
| Port Validation | strconv.ParseUint accepts port 0 and >65535 |
Enforce 1 <= port <= 65535 and return InvalidSandboxPortError |
Error Match (handler.go) |
var invalidPortErr *InvalidSandboxPortError (pointer target) |
var invalidPortErr InvalidSandboxPortError (value struct target) |
Test Assertion (host_test.go) |
require.ErrorAs(t, err, &tt.wantErrIs) |
var targetErr InvalidSandboxPortError; require.ErrorAs(t, err, &targetErr) |
Reproduction Steps
- Call
GetTargetFromRequestwith host0-sandboxid.e2b.appor HTTP headerE2b-Sandbox-Port: 0. GetTargetFromRequestreturnssandboxID, 0, nilinstead ofInvalidSandboxPortError.- Call
handlerwith agetDestinationfunction returningInvalidSandboxPortError. - The handler fails to match
errors.As(err, &invalidPortErr)and returns internal error instead of400 Bad Request.
// Minimal unit test reproduction in handler:
h := handler(p, func(r *http.Request) (*pool.Destination, error) {
return nil, InvalidSandboxPortError{Port: "invalid", wrapped: errors.New("invalid port")}
})
// Result: errors.As returns false when target is *InvalidSandboxPortError
Technical Context
- Files affected:
packages/shared/pkg/proxy/host.gopackages/shared/pkg/proxy/handler.gopackages/shared/pkg/proxy/host_test.gopackages/shared/pkg/proxy/proxy_test.go
- Subsystem: Edge Client Proxy Request Routing (
packages/shared/pkg/proxy) - Impact: Low - Medium
Proposed Changes
| # | Change | File(s) Affected | Complexity |
|---|---|---|---|
| 1 | Enforce 1 <= port <= 65535 boundaries in parseHost and parseHeaders |
packages/shared/pkg/proxy/host.go |
Low |
| 2 | Update var invalidPortErr InvalidSandboxPortError in handler.go |
packages/shared/pkg/proxy/handler.go |
Trivial |
| 3 | Fix require.ErrorAs test assertion and add port 0 / >65535 test cases |
packages/shared/pkg/proxy/host_test.go |
Low |
| 4 | Add TestProxyInvalidSandboxPortError handler integration test |
packages/shared/pkg/proxy/proxy_test.go |
Low |
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in packages/shared/pkg/proxy/host.go and read parseHost, parseHeaders, and their cases in host_test.go. Run the proxy package tests, then check handler.go and proxy_test.go for InvalidSandboxPortError handling. Done means ports 0 and above 65535 are rejected, errors.As identifies the error correctly, and invalid requests return 400 Bad Request.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend, networking, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100