← Back to Pull requests

proxy: remove the headers Connection names before handling a request #361

Open
claravanstaden opened this pull request with 279 lines diff across 6 files
View on GitHub
claravanstaden claravanstaden commented on Dec 03, 2026 17:06 UTC
Author

httputil.ReverseProxy removes the headers named in the Connection header as hop-by-hop, but with a Director it does so after aperture has authenticated, priced and rate limited the request. aperture then decides on headers the backend never gets. For example, an HTTP/1 client that names its own credential (Connection: Authorization) passes authentication and reaches the backend without that header.

Before and after


Before
1. An HTTP/1 client sends a valid L402 with Connection: Authorization.
2. aperture authenticates the request with that credential.
3. The reverse proxy removes Authorization as hop-by-hop.
4. The backend gets an authenticated request with no identity.

After
1. The same request arrives.
2. aperture removes Authorization at once, because Connection names it.
3. The request has no credential, so a paid path gets a 402 and nothing reaches the backend.

Fix


At the start of serveHTTP, removeConnectionHeaders removes every header Connection names and keeps only the tokens that name no other header (close, keep-alive, te, upgrade), so aperture decides on the headers the backend gets. Headers the operator configures for a service are set afterwards and now reach the backend even when a client names them. A request that names a header a reverse proxy adds, such as X-Forwarded-For, is refused with 400 instead, since removing it would hide that a proxy relayed the request; that list moves to netutil so the dashboard guard uses the same one. HTTP/2 forbids Connection, so only HTTP/1 requests change.

Tests


  • TestConnectionHeadersRemovedFirst: a credential named in Connection (any case, among other tokens, on a second line) gets 402 and nothing reaches the backend; a similar name leaves the credential alone; naming a forwarding header gets 400 on any path; a configured header still reaches the backend.

  • TestRemoveConnectionHeaders: which headers and tokens are removed and kept.
  • Diff: 279 lines (+264 / -15) across 6 files Reviewer: GustavoStingelin Review duration: 3976m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.44) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.44 · 279 lines diff across 6 files
    0.44
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 2 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.337
    Real PR Conversation Timeline
    4 items
    claravanstaden claravanstaden commented on Dec 04, 2026 11:36 UTC
    Author

    Validation:

  • Claude (ll-code-review): 3 rounds, nothing above Low left.

  • Codex (gpt-6-astra, max effort): no findings. A Content-Length Medium was checked and dismissed.

  • Regtest: 9b402d4 merged into an integration build on master 2433deb, behind a local regtest Loop stack (lnd 0.19.2, loop 0.35.0) with an HTTP/1.1 test service. A valid L402 gets 200. The same L402 with Connection: Authorization gets 402. Connection: X-Forwarded-For, X-Real-IP or Forwarded gets 400. master returns 200 for both cases.
  • GustavoStingelin GustavoStingelin commented on code on Dec 04, 2026 16:44 UTC
    Code Review
    proxy/proxy.go:1
    @@ -969,6 +988,63 @@ func (p *Proxy) acceptForService(header *http.Header, resourceName string,
     	return accepted, false
     }
     
    +// connectionTokens are the Connection tokens that name no other header to
    +// remove, and are kept.
    +var connectionTokens = []string{"close", "keep-alive", "te", "upgrade"}
    +
    +// connectionNamesForwardingHeader reports whether the Connection header names
    +// one of the headers a reverse proxy adds.
    +func connectionNamesForwardingHeader(header http.Header) bool {
    +	for _, value := range header["Connection"] {
    +		for _, token := range strings.Split(value, ",") {
    +			token = textproto.TrimString(token)
    +			for _, name := range netutil.ForwardingHeaders {
    +				if strings.EqualFold(token, name) {
    +					return true
    +				}
    +			}
    +		}
    +	}
    +
    +	return false
    +}
    +
    +// removeConnectionHeaders removes the headers that the Connection header names
    +// as hop-by-hop, as the reverse proxy would when it forwards the request, and
    +// keeps only the tokens that name no other header. HTTP/2 forbids the
    +// Connection header, so this only changes HTTP/1 requests.
    +func removeConnectionHeaders(header http.Header) {
    +	var kept []string
    +	for _, value := range header["Connection"] {
    +		for _, token := range strings.Split(value, ",") {

    Both Connection scans use strings.Split, which allocates a slice of every token before authentication or the blocklist check. Repeated standard tokens also grow kept before it is joined. Could we use strings.SplitSeq in both loops and retain each standard token at most once?

    I benchmarked both helpers together on this commit with synthetic ~800 KB Connection values. Each benchmark operation runs both helpers; it does not include the full HTTP request path.

    | Header tokens | Allocated/op (PR) | Allocated/op (SplitSeq + deduplication) | Time/op (PR) | Time/op (with change) |
    | --- | ---: | ---: | ---: | ---: |
    | Repeated removable tokens | 12.8 MB | 16 B | ~21.7 ms | ~18.9 ms |
    | Repeated close tokens | 16.5 MB | 48 B | ~6.3 ms | ~4.0 ms |

    go test ./proxy passed with the change applied.

    GustavoStingelin GustavoStingelin requested changes on Dec 04, 2026 16:46 UTC
    Changes Requested Reviewer

    Requested changes.

    Assignees
    claravanstaden claravanstaden
    Labels
    risk: low confidence: 89.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44% (MEDIUM) · Higher = larger blast radius
    44%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.9% · Lower = rushed or shallow review
    89.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.5% · Safe to merge
    4.5%