Repository navigation
fix: force connection: close on chunked upstream requests - #158
Conversation
Hardens against HTTP request smuggling (GHSA-ggv3-7p47-pfv8), the connection-reuse half of the advisory that complements the existing `deleteLength` desync fix (#153). `setupOutgoing` now forces `Connection: close` on the upstream request — regardless of agent — whenever the request carries `transfer-encoding`, or its `Connection` header lists `transfer-encoding` as hop-by-hop. Since httpxy defaults to keep-alive agents, a chunked request would otherwise reuse the upstream socket; if a lenient upstream ever desynced, leftover bytes could be read as a pipelined next request and poison another user's response. Tearing the socket down after any chunked request removes that amplifier and also closes the `Connection: upgrade` + chunked-body bypass. Genuine WebSocket upgrades (no `transfer-encoding`) keep their header. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #158 +/- ##
==========================================
+ Coverage 94.95% 95.23% +0.28%
==========================================
Files 8 8
Lines 793 840 +47
Branches 328 352 +24
==========================================
+ Hits 753 800 +47
Misses 35 35
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
connection: close on chunked upstream requests (GHSA-ggv3-7p47-pfv8)connection: close on chunked upstream requests
📝 WalkthroughWalkthroughThis PR adds request-smuggling hardening to ChangesConnection Close Hardening for Request Smuggling
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant ProxyMiddleware
participant setupOutgoing
participant UpstreamServer
Client->>ProxyMiddleware: OPTIONS request (transfer-encoding: chunked)
ProxyMiddleware->>setupOutgoing: build outgoing request options
setupOutgoing->>setupOutgoing: detect transfer-encoding header or hop-by-hop Connection token
setupOutgoing->>setupOutgoing: force outgoing.headers.connection = "close"
setupOutgoing->>ProxyMiddleware: return hardened outgoing options
ProxyMiddleware->>UpstreamServer: forward request with connection: close
UpstreamServer->>ProxyMiddleware: response
ProxyMiddleware->>Client: response
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/_utils.ts (1)
155-176: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueHardening logic is correct and well-documented.
The two-pronged detection (direct
transfer-encodingheader + hop-by-hop Connection marker) correctly covers both vectors, and theObject.entries+.toLowerCase()scan properly handles non-lowercased keys injected viaoptions.headers. The upgrade + chunked bypass is closed: the existing upgrade-preservation block at lines 145–153 no longer shields requests that carrytransfer-encoding.One minor redundancy:
outgoing.headersis always an object by this point (set at line 95 as{ ...req.headers }), so the guard at line 166 is unnecessary. Not a defect—just a copy of the pre-existing pattern at line 146.🧹 Remove redundant guard
- outgoing.headers = outgoing.headers || {}; const carriesTransferEncoding = Object.entries(outgoing.headers).some(🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/_utils.ts` around lines 155 - 176, The hardening logic in the outgoing request header handling is correct, but the `outgoing.headers = outgoing.headers || {}` guard is redundant because `outgoing.headers` is already initialized earlier in the same flow. Remove that unnecessary fallback in the `_utils` request-header normalization logic and keep the existing `carriesTransferEncoding` / `connectionMarksTransferEncoding` checks and `connection = "close"` behavior unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/_utils.ts`:
- Around line 155-176: The hardening logic in the outgoing request header
handling is correct, but the `outgoing.headers = outgoing.headers || {}` guard
is redundant because `outgoing.headers` is already initialized earlier in the
same flow. Remove that unnecessary fallback in the `_utils` request-header
normalization logic and keep the existing `carriesTransferEncoding` /
`connectionMarksTransferEncoding` checks and `connection = "close"` behavior
unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3467e184-f463-4b1d-b971-c9d9b84af7e0
📒 Files selected for processing (4)
AGENTS.mdsrc/_utils.tstest/_utils.test.tstest/middleware/web-incoming.test.ts
The request-smuggling hardening (GHSA-ggv3-7p47-pfv8) previously lived only in `setupOutgoing`, leaving two parallel outgoing-request paths that could still reuse an upstream keep-alive socket after a chunked request: - `proxyFetch` defaults to a shared keep-alive agent and forwards a client-supplied `transfer-encoding` header verbatim. - The `web-incoming` `followRedirects` 307/308 replay builds its request options by hand, bypassing `setupOutgoing`. Extract the logic into a shared `forceConnectionCloseForTransferEncoding()` helper and apply it on every outgoing path: `setupOutgoing`, `proxyFetch`'s `_sendRequest` (covering the initial request and each redirect hop), and the ProxyServer redirect replay. Add regression tests for each; all fail without the fix. Co-Authored-By: Claude Opus 4.8 <[email protected]>
) Port the upstream vercel/next.js rewrite-request-smuggling suite to httpxy. Previously coverage only asserted the mechanism (`connection: close` is emitted); this adds an outcome test: a crafted chunked request hiding a smuggled `GET /secret` in its body is sent to an httpxy proxy fronting a deliberately lenient, desync-prone upstream, and the suite asserts the smuggled request never reaches the upstream across the same Connection-header permutations and methods (keep-alive; keep-alive,upgrade; transfer-encoding, upgrade; OPTIONS; non-rfc strip). All 5 scenarios fail without the hardening. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Summary
Adds the connection-reuse half of the HTTP request-smuggling fix for GHSA-ggv3-7p47-pfv8 (CVE-2026-29057). This complements the
deleteLengthdesync fix already onmain(#153).The upstream Next.js/http-proxy patch had two hunks:
deleteLength(already fixed in fix: preservetransfer-encodingon chunked DELETE/OPTIONS #153): no longer rewrites a chunkedDELETE/OPTIONSintocontent-length: 0while streaming the body — the proxy no longer manufactures a desync.setupOutgoing(this PR): forceConnection: closeon the upstream request for any chunked (transfer-encoding) request, so the upstream socket is never reused.Why Hunk 2 matters for httpxy specifically
A framing desync is only exploitable for cross-user response poisoning if the upstream keep-alive socket is reused afterward. httpxy defaults to keep-alive agents, so — unlike stock http-proxy (which defaults to no agent → close) — chunked-request sockets were reused by default. That is the exact amplifier Hunk 2 removes. Even though httpxy no longer creates a desync itself, a lenient/legacy upstream (e.g. one that honors
Connection: transfer-encodingas hop-by-hop, or mishandles chunked framing) could, and the reused socket would let leftover bytes be served as another user's response.Change
The hardening now lives in a shared helper,
forceConnectionCloseForTransferEncoding()(src/_utils.ts), which forcesConnection: close— regardless of agent — when the outgoing request carries atransfer-encodingheader, or itsConnectionheader liststransfer-encodingas hop-by-hop. This also closes theConnection: upgrade+ chunked-body bypass vector. Genuine WebSocket upgrades (notransfer-encoding) keep theirConnectionheader intact.Crucially, the helper is applied on every outgoing-request path, not just
setupOutgoing— httpxy has three parallel paths that build upstream requests, and all three defaulted to keep-alive socket reuse:setupOutgoingsrc/_utils.tsweb/wsinitial requestsproxyFetch→_sendRequestsrc/fetch.tsfollowRedirects307/308 replay hop (the helper re-runs on each recursion)followRedirectsreplaysrc/middleware/web-incoming.tssetupOutgoingWithout the last two, a chunked request over
proxyFetch's default keep-alive agent, or a chunked ProxyServer request that gets 307/308-redirected with a caller-supplied keep-alive agent, would still return a reusable socket to the pool — leaving the exact smuggling amplifier this PR aims to remove.Tests
Mechanism tests (assert
Connection: closeis emitted):test/_utils.test.ts— 4 unit tests: TE forcescloseeven with the default keep-alive agent;Connection: keep-alive, transfer-encodingforcesclose;Connection: upgrade+ chunked forcesclose; real WS upgrades are untouched.test/middleware/web-incoming.test.ts—#chunked-connection-teardown: end-to-end raw-socket test asserting the backend actually receivesconnection: closefor a chunked request through a default keep-alive proxy. Plus a#followRedirectsregression asserting the 307 replay of a chunked request is sent withconnection: close.test/fetch.test.ts—proxyFetchregressions: chunked request over a keep-alive agent forcesclose;Connectionhop-by-hop marker forcesclose; 307 replay forcescloseand preserves the body; a non-chunked request keepsconnectionintact.End-to-end outcome test (
test/request-smuggling.test.ts, ported from the upstream vercel/next.jsrewrite-request-smugglingsuite):Sends a raw crafted chunked request that hides a second
GET /secretrequest inside its body, through an httpxy proxy fronting a deliberately lenient, desync-prone upstream (pipelines on keep-alive, honorsConnection: close), and asserts the smuggled request never reaches the upstream — across the sameConnection-header permutations and methods as upstream:Connectionheaderkeep-alivekeep-alive, upgradeTransfer-Encoding, upgradeTransfer-Encoding, upgradekeep-alive, upgradeAll new tests were verified to fail without this change (the smuggled
GET /secretreaches the upstream) and pass with it. Full suite (lint + typecheck + 314 tests) is green.Docs
Updated
AGENTS.md: corrected the staledeleteLengthdescription, documented the keep-alive default and the TE →Connection: closerule, documented the shared helper with the three outgoing paths it now covers, and listed the newrequest-smuggling.test.tsend-to-end suite.🤖 Generated with Claude Code
Summary by CodeRabbit
Connection: closewhen needed, helping prevent request-smuggling issues.DELETEandOPTIONSrequests:content-length: 0is only added when no body-related headers are already set.