Skip to content

H1 keep-alive after an early response reuses both connections mid-request (request smuggling, upstream desync) #1721

Closed

Description

@FlorentinDUBOIS

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::Server completion branch, keep_alive = keep_alive_frontend && keep_alive_backend) calls stream.front.clear(), which puts the kawa parser back to ParsingPhase::StatusLine (kawa 0.7.2 Kawa::clear) while front.storage is 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, …) reaches ConnectionH1::end_stream, whose Position::Client(_, _, Connected) arm moves the backend connection to BackendStatus::KeepAlive on keep_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 the endpoint.end_stream call, 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.rs sends RST_STREAM(CANCEL) unless back_received_end_of_stream && front.is_terminated() (fully_completed).
  • The H1-backend guard in ConnectionH1::end_stream is 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

  1. added 2 commits that reference this issue on Sep 30, 2026
    1a80bdf
    8052483
  2. added a commit that references this issue on Oct 2, 2026
    d3b5aef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions