Repository navigation
Description
Summary
When a backend answers an HTTP/1.1 request completely and early (e.g. 413 or 401 with Content-Length: 0 and no Connection: close) while the client is still uploading the request body, the H1 frontend applies keep-alive as soon as the response is complete. Nothing checks that the request is complete too. Both halves of the connection pair are then reused mid-message:
- Client side (request smuggling). The keep-alive reset in
ConnectionH1::writable(lib/src/protocol/mux/h1.rs,Position::Servercompletion branch,keep_alive = keep_alive_frontend && keep_alive_backend) callsstream.front.clear(), which puts the kawa parser back toParsingPhase::StatusLine(kawa 0.7.2Kawa::clear) whilefront.storageis kept "for pipelining". The rest of the client's body is then parsed as a new request: routed, edited (Sozu-Id,X-Forwarded-*) and forwarded like any other. - Backend side (upstream desync). In the same branch
endpoint.end_stream(token, …)reachesConnectionH1::end_stream, whosePosition::Client(_, _, Connected)arm moves the backend connection toBackendStatus::KeepAliveonkeep_alive_backend && back.is_terminated()only. The backend connection is pooled while it still expects the rest of the request body, so the next request of that session is written into it and the backend reads it as body.
The two combine: the smuggled request is sent on the pooled, desynchronized backend connection.
Reproduction (origin/main a810211)
e2e test (added in the fix PR): the client sends POST /upload with its head and the first part of the body, the backend answers HTTP/1.1 413 Content Too Large\r\nContent-Length: 0\r\n\r\n on connection 0, the client reads the 413, then sends the rest of the body (which is byte-for-byte GET /smuggled HTTP/1.1\r\nHost: localhost\r\n\r\n) followed by a second request.
cargo test -p sozu-e2e --locked h1_early_response -- --nocapture → test result: FAILED. 0 passed; 2 failed
Content-Length upload:
EARLY-RESPONSE-ContentLength: backend connection 0 after the 413: "GET /smuggled HTTP/1.1\r\nHost: localhost\r\nX-Forwarded-For: 127.0.0.1\r\nForwarded: proto=http;for=\"127.0.0.1:43940\";by=127.0.0.1\r\nX-Forwarded-Port: 20053\r\nX-Forwarded-Proto: http\r\nX-Request-Id: 01M3S3BXGECERW0AQH94JV912W\r\nSozu-Id: 01M3S3BXGECERW0AQH94JV912W\r\n\r\n"
EARLY-RESPONSE-ContentLength: client after the 413: "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nSozu-Id: 01M3S3BXGECERW0AQH94JV912W\r\n\r\nok"
Chunked upload (chunk-size line sent before the response, chunk data after): same GET /smuggled forwarded with a Sozu-Id on backend connection 0, the client receives its 200, then a 400 for the trailing \r\n0\r\n\r\n.
So the body bytes became a request that sozu routed and edited as its own (bypassing whatever per-request policy the outer request was subject to), and it went out on the backend connection whose own request was still incomplete.
H2 frontend comparison
- An H2 frontend does not have the client-side half:
ConnectionH2::try_recycle_server_stream(lib/src/protocol/mux/h2.rs) defers recycling, and so theendpoint.end_streamcall, while!stream.front_received_end_of_stream("client still sending"); DATA frames stay bound to their stream id. - An H2 backend is safe too: when a stream it carries ends,
h2.rssendsRST_STREAM(CANCEL)unlessback_received_end_of_stream && front.is_terminated()(fully_completed). - The H1-backend guard in
ConnectionH1::end_streamis shared by both frontends, though: an H2 client that resets a stream (RST_STREAM →endpoint.end_stream) after receiving the whole response but before its END_STREAM would pool an H1 backend connection with a partial request as well.
Expected
RFC 9112 §9.3 lets a connection carry another message only once the previous one is complete. A backend H1 connection returns to the keep-alive pool only when both the request and the response are complete (fully parsed and fully written); otherwise it is closed. The H1 client connection is kept alive only when the request is complete too; otherwise it is closed after the response is flushed (the same defer_close_for_tls_flush exit a Connection: close response already takes), and the rest of the body is never parsed as a request.
Activity
fix(h1): keep neither connection alive after a response that complete…
fix(h1): keep neither connection alive after a response that complete…
fix(h1): linger before closing a connection whose request is still ar…
fix(h1): keep neither connection alive after a response that complete…