proxy: remove the headers Connection names before handling a request #361
Validation:
Connection: Authorization gets 402. Connection: X-Forwarded-For, X-Real-IP or Forwarded gets 400. master returns 200 for both cases.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.
Requested changes.
GustavoStingelin
claravanstaden
httputil.ReverseProxyremoves the headers named in theConnectionheader as hop-by-hop, but with aDirectorit 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
Authorizationas hop-by-hop.4. The backend gets an authenticated request with no identity.
After
1. The same request arrives.
2. aperture removes
Authorizationat once, becauseConnectionnames 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,removeConnectionHeadersremoves every headerConnectionnames 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 asX-Forwarded-For, is refused with 400 instead, since removing it would hide that a proxy relayed the request; that list moves tonetutilso the dashboard guard uses the same one. HTTP/2 forbidsConnection, so only HTTP/1 requests change.Tests
TestConnectionHeadersRemovedFirst: a credential named inConnection(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.