e2b-dev / e2b-dev/runtime

fix(shared): missing port range validation and broken errors.As match bypass proxy 400 Bad Request

Open
#3,430 0 comments 0 reactions 0 assignees View on GitHub

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

  1. parseHost and parseHeaders in host.go parsed ports using strconv.ParseUint without enforcing valid TCP port boundaries (1 <= port <= 65535).
  2. handler.go declared var invalidPortErr *InvalidSandboxPortError as a pointer target for errors.As. Because host.go constructs InvalidSandboxPortError as a value struct (InvalidSandboxPortError{...}), errors.As evaluated to false due to double pointer indirection (**InvalidSandboxPortError).
  3. host_test.go passed &tt.wantErrIs (*error) to require.ErrorAs, which executes errors.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

  1. Call GetTargetFromRequest with host 0-sandboxid.e2b.app or HTTP header E2b-Sandbox-Port: 0.
  2. GetTargetFromRequest returns sandboxID, 0, nil instead of InvalidSandboxPortError.
  3. Call handler with a getDestination function returning InvalidSandboxPortError.
  4. The handler fails to match errors.As(err, &invalidPortErr) and returns internal error instead of 400 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.go
    • packages/shared/pkg/proxy/handler.go
    • packages/shared/pkg/proxy/host_test.go
    • packages/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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.