Repository navigation
fix: security and correctness hardening (request smuggling, path/method ACL bypass, crashes, leaks) - #7
Merged
Conversation
Security
- Request smuggling: the framing of the remote request is now always
defined by the proxy. A payload received without content-length
(HTTP/2 source, DELETE/OPTIONS...) was written unframed on a keep-alive
connection and parsed as a second request by the remote server. Parsed
payloads of chunked requests were sent with both content-length and
transfer-encoding.
- Route prefix escape: targets containing dot segments (also %2e
encoded), backslashes, tabs or line breaks are rejected with 400. The
URL parser resolved them, so "/public/../admin" matched a "/public/*"
route but reached "/admin" on the remote server.
- Denial of service: unsupported protocols (absolute-form requests when
no base is set), invalid remote status codes and invalid header values
crashed the process with an uncaught exception.
- Hop-by-hop request headers are not forwarded anymore. "upgrade" made
the request hang forever when the remote switched protocols,
"connection: close" disabled remote connection reuse.
- Remote connections were leaked when the client disconnected before the
remote response was received. The remote request is now aborted, and
unconsumed responses are destroyed.
- A remote failure after the response headers were sent appended an
error message to the payload of a seemingly successful response. The
request callback is now guaranteed to run once, and the client
connection is terminated instead.
- URL cache keys of different (base, source) pairs could collide.
- Response headers nominated by the remote "connection" header, plus
"trailer" and "proxy-authenticate", are stripped.
Correctness
- Buffer/typed array payloads were forwarded as their JSON representation.
- Form payloads were forwarded as JSON when the content-type had
parameters (e.g. charset), nested values and arrays were mangled.
- Payloads not handled by the body parsers (req.body = {}) were replaced
by "{}".
- IPv6 literal hosts could not be reached.
- Generic remote failures are reported as 502 instead of 500, timeouts
have a dedicated error code.
- Fragments are never forwarded, mixed case response headers of custom
request clients keep their values.
- Proxy options are not passed down to the http agents anymore.
Also: typings fixes, documentation of the proxy behavior, CI running on
supported Node.js versions.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011Px3ETB9QhGQ9rSwjGpEcH
- Requests of clients that disconnected before proxy() was invoked (e.g. while asynchronous middlewares were running) are not forwarded anymore. The 'close' event was already emitted, so the remote request was never aborted and its connection could be held forever. - Methods that are not upper case are rejected. HTTP/2 servers accept "delete" as a method name, the http client sent it as "DELETE", bypassing method based access control at the gateway. - CONNECT is rejected with 405, tunnels are not supported and the request was never settled. - Remote status codes not allowed in HTTP/2 (>= 600) crashed the process when the source was an HTTP/2 request. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Px3ETB9QhGQ9rSwjGpEcH
- Request smuggling: custom stream payloads (req.body = Stream) were framed using the content-length of the client request, which does not describe them (e.g. a decompressed payload). Exceeding bytes were parsed as a second request by the remote server. They are now always sent using chunked transfer encoding. - Access control bypass: sources starting with "//" were collapsed to a single slash, "//admin" did not match an "/admin" rule at the gateway but reached "/admin" on the remote server. They are rejected with 400, as well as targets containing any control character, or starting or ending with a space. - Errors thrown by rewriteHeaders, onResponse and onClientConnectionTerminated were uncaught exceptions leaking the remote connection. They are answered with 500 and reported as process warnings. - When rewriteRequestHeaders returns both framing headers, content-length is dropped. - content-encoding is not forwarded when the payload was decoded by a body parser. - Payloads that can not be serialized are rejected with 400 instead of throwing. - HTTP/2 streams are reset when the remote response fails midway, a truncated chunked response was ended gracefully. - Dates are serialized in form urlencoded payloads. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Px3ETB9QhGQ9rSwjGpEcH
The test servers echoing the request URL had no content type, which code scanning reports as reflected cross-site scripting. They are only used by the test suite, the responses are now explicitly served as plain text. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Px3ETB9QhGQ9rSwjGpEcH
When onResponse threw, the 500 error was sent with the headers of the remote response already copied to the client response. A stale content-length desynchronized keep-alive client connections, and content-encoding/cache-control described a payload that was never sent. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A full review of the proxy core for security, correctness and performance. Every issue below was reproduced against 1.1.3 before being fixed, and each fix has a regression test. A second, independent adversarial review of the fixed code found seven further issues, which are included here.
standardis clean; coverage is 99%.Security fixes
Request smuggling
content-lengthonDELETE/OPTIONS: the payload was written unframed on a keep-alive connection, so the remote server parsed it as a second request.req.body = Stream) were framed with the client'scontent-length, which does not describe them (e.g. a decompressed payload). Exceeding bytes were parsed as a second request.content-lengthandtransfer-encoding.Access control bypass
/public/../admin(also%2eencoded, or using backslashes) matched a/public/*route but reached/adminon the remote server. Now400.//adminwas collapsed to/admin, bypassing an/adminrule at the gateway. Now400.deleteas a method name and the HTTP client sent it asDELETE. Methods that are not upper case are now400.Process crashes (remote DoS)
baseis not set.099for HTTP/1 clients,>= 600for HTTP/2 clients).rewriteHeaders,onResponseandonClientConnectionTerminated. They are now answered with500and reported withprocess.emitWarning.Hangs and leaks
upgrademade the request hang forever when the remote server switched protocols.CONNECTis rejected with405.proxy()was invoked. The remote request is now aborted, and unconsumed responses are destroyed.Response corruption
Gateway Timeoutwas appended to the payload of a seemingly successful response. The request callback now runs at most once, and the client connection (HTTP/2: the stream) is terminated instead.Other
connectionheader, plustrailerandproxy-authenticate, are stripped.(base, source)pairs could collide.Correctness fixes
Buffer/typed array payloads were forwarded as their JSON representation.content-typehad parameters (e.g.; charset=UTF-8); nested values and arrays were mangled.req.body = {}) were replaced by{}.content-encodingis dropped when a body parser decoded the payload.400instead of throwing.base,cacheURLs,requests) are no longer passed down to the HTTP agents.requestswas typed as anAgent, the stream type came from an undeclaredpumptypes dependency,queryStringonly allowed strings.Behavior changes
502instead of500(the body already said "Bad Gateway").//are rejected instead of collapsed. Three existing tests asserting the collapse were updated.onClientConnectionTerminatedusually receives anECONNRESETerror and noresponse.POSTwithout payload is forwarded withcontent-length: 0instead of an empty chunked payload.expectandproxy-authorizationare no longer forwarded.Not changed
timeout; a default would break long-polling users.ECONNRESETstill maps to504./public/..;/adminor/public/..%2fadminare forwarded untouched. A remote server that normalizes further could still be reached that way; this is documented in the README.Test plan
npx standardnpm teston Node 18, 20, 22 and 24test/14.hardening.test.jsGenerated by Claude Code