Repository navigation
fix(mux): close an H1 client connection after the backend's Connection: close - #1648
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 05:40
…n: close sozu forwards a backend's `Connection: close` to the client, but `ConnectionH1::writable` decided whether to keep the client connection on `keep_alive_frontend` alone, which only follows the client's request. For a close-delimited body (neither `Content-Length` nor chunked coding, RFC 9112 §6.3 rule 8) the body can only end with the close, so the client waited for more body until the frontend timeout, and the response to a pipelined request was appended to that body. The `Terminated` branch of `ConnectionH1::writable` now keeps the connection only while both `keep_alive_frontend` and `keep_alive_backend` hold. Otherwise it takes the existing `response-complete` exit, the one a client `Connection: close` already takes: the access log, then `defer_close_for_tls_flush`, a write-only shutdown once the response is flushed (RFC 9112 §9.6). A pipelined request already buffered is dropped with the connection instead of being processed. `keep_alive_backend` also covers every close-delimited body, since `ConnectionH1::terminate_close_delimited` only runs for such a backend. The decision stays in the H1 connection: `HttpContext` also serves H2 frontends, where clearing `keep_alive_frontend` would send GOAWAY after every response from a `Connection: close` backend. The H2 path is unchanged; `Connection` never reaches an H2 client and the body ends with END_STREAM. Closing rather than re-framing the body as chunked: HAProxy's `h1_set_cli_conn_mode` also closes the client on a response without a known length, and re-framing would add a chunked output path, a `Transfer-Encoding` injection and a client-version gate to the mux. `test_h1_content_length_complete_then_backend_close_stays_alive` and the third leg of `test_keep_alive` pinned the defect itself: both reused a client connection after sozu had forwarded `Connection: close`. They now require the close. `test_h1_close_delimited_body_ends_at_backend_close` becomes `test_h1_close_delimited_body_closes_client` and reads to EOF, with a variant for a body read together with the EOF and a pipelining test. `test_h1_framed_keep_alive_responses_keep_the_client` and two H2 tests pin the connections that must stay open. Closes #1642 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h1-close-delimited-closes-client
branch
from
September 28, 2026 07:00
ef4b6c3 to
21f9ff9
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 28, 2026
…ter its headers (#1651) ## What An HTTP/1.1 request with neither `Content-Length` nor `Transfer-Encoding` now ends after its headers, for every method and version (RFC 9112 §6.3 rule 7). Before, kawa 0.7.1 parsed it as close-delimited, and a request pipelined behind it in the same segment was forwarded raw to the first request's backend as its "body": not routed, not Basic-auth checked, without `Sozu-Id` or `X-Forwarded-*` (CWE-444). Details in #1650; the parser-level defect is CleverCloud/kawa#23. ## Protocol / security impact - **Fix site**: `HttpContext::on_request_headers` (`lib/src/protocol/kawa_h1/editor.rs`) sets `ParsingPhase::Terminated` when it finds `parsing_phase == ParsingPhase::Body && body_size == BodySize::Empty`, replacing the commented-out GET-only guard. kawa pushes the end-of-headers `Flags` right after the callback with `end_stream: kawa.is_terminated()`, so the request is forwarded complete; `ConnectionH1::readable` stops reading the frontend, and the keep-alive branch of `ConnectionH1::writable` parses, routes and edits the pipelined request on its own. - **Why the `ParsingPhase::Body` conjunct**: `pkawa::handle_header` calls the same callback for an H2 request while `body_size` is still `Empty`, then frames a request whose DATA follows as chunked unless the callback terminated it. Keying on `Empty` alone would drop H2 request bodies without `content-length`; `an_h2_request_is_left_for_pkawa_to_frame` pins it (seen red with the conjunct removed). - **Unchanged**: close-delimited responses (`on_response_headers`, `ConnectionH1::terminate_close_delimited`), requests with `Content-Length` or chunked framing, the CL.TE guard (it runs first and returns on rejection). - **Interactions checked**: `Expect: 100-continue` without length (no body to wait for), `GET` + `Upgrade: websocket` (no body; the 101 still switches to the pipe — existing WebSocket e2e tests pass), HTTP/1.0 (same rule). `CONNECT` is not tunnelled by Sōzu; it used to stream client bytes to the backend as a close-delimited request body and no longer does. Bytes a client sends behind an upgrade request before the 101 (forbidden by RFC 6455 §4.1) are now held and reach the backend after the 101 through the pipe, in the same segment as the request or a later one; before the fix they reached the backend before the 101 as the request's body (measured with a temporary e2e probe, not committed). - **Cost**: no allocation added; `a_bare_request_costs_one_scratch_and_one_copy_per_forwarding_header` still counts 7. ## Tests - e2e `test_h1_unframed_request_then_{same_cluster,other_cluster,auth_gated_cluster}{,_tls}` (`e2e/src/tests/h1_security_tests.rs`): one write carries `<METHOD> /a/first` without length plus `GET /{a,b,c}/second`, for `GET`, `HEAD`, `DELETE`, `POST`, over a clear and a TLS listener with three path-routed clusters (`/c` requires Basic auth). The first backend must receive exactly one request line, the second request must reach its own cluster as a distinct edited request with its own `Sozu-Id` and `X-Forwarded-For` (or be answered 401 by Sōzu for `/c`, with its backend never contacted), and the client must read two responses. **Red on 96b4bca in all 24 cases**, e.g. `request lines ["GET /a/first HTTP/1.1", "GET /b/second HTTP/1.1"], Sozu-Id ["01M3KAX0331KN97P81H6552FY8"]`; green with the fix. - unit (`editor.rs`): `a_request_without_length_ends_after_its_headers` (GET, HEAD, DELETE, POST, HTTP/1.0 POST, `Expect: 100-continue`, `Upgrade: websocket`; red without the fix: `GET / HTTP/1.1: must end after its headers, got Body`), `a_framed_request_and_an_unframed_response_keep_their_bodies`, `an_h2_request_is_left_for_pkawa_to_frame`. ## Commands run On the pre-rebase parent 96b4bca (full set), then after rebasing on 3ee0b6d: fmt, both clippy runs, all-features build, doc, `cargo test -p sozu-lib --lib`, the e2e subset `h1_security_tests tls_tests websocket 100_continue pipelining`, and the citation checker. The full workspace suite ran on 96b4bca only. - `cargo build --locked`, `cargo build --all-features --locked` - `cargo clippy --workspace --all-targets --locked -- -D warnings` - `cargo clippy --workspace --all-targets --no-default-features --features crypto-ring,opentelemetry,splice,simd --locked -- -D warnings` - `cargo +nightly fmt --all --check` - `cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8` - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked` - `.github/scripts/check_doc_citations.py`: `--self-test`, `--root .github/scripts/testdata/citations` (exit 1 as expected), `--base origin/main`, plain ## Docs `lib/src/protocol/kawa_h1/LIFECYCLE.md` (§2 bullet, new §2.2), `CHANGELOG.md` (Security), `CLAUDE.md` Security-sensitive areas (one sentence). Conflicts only with #1648 on `CHANGELOG.md` (both insert at the top of Security); clean against `origin/main` (`git merge-tree --write-tree`). Closes #1650 Signed-off-by: Florentin Dubois <[email protected]>
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
After a response carrying the backend's
Connection: close, sozu forwarded that header to a keep-alive H1 client but kept the client connection open:ConnectionH1::writable(lib/src/protocol/mux/h1.rs) decided onkeep_alive_frontendalone, which only follows the client's request. For a close-delimited body (neitherContent-Lengthnor chunked, RFC 9112 §6.3 rule 8) the body can only end with the close, so:Closes #1642.
Fix
The
Terminatedbranch ofConnectionH1::writablenow keeps the connection only while bothkeep_alive_frontendandkeep_alive_backendhold. Otherwise it takes the existingresponse-completeexit, the one a clientConnection: closealready uses: access log, thendefer_close_for_tls_flush(write-only shutdown once the response is flushed, neverShutdown::Both), RFC 9112 §9.6. A pipelined request already buffered is dropped with the connection and never processed; the client retries it (§9.3.2).keep_alive_backend == falsecovers both cases in the issue: an explicit backendConnection: close(forwarded unchanged byHttpContext::on_response_headers), and every close-delimited body, becauseConnectionH1::terminate_close_delimitedonly runs for such a backend. No new field, no allocation.Why not in
HttpContext::on_response_headers:HttpContextalso serves H2 frontends, andConnectionH2::write_streamssends GOAWAY whenkeep_alive_frontendis false. Clearing it there would GOAWAY every H2 client behind aConnection: closebackend after each response. Two new H2 e2e tests go red with that variant (checked locally) and stay green with this fix.Frontend H2: not affected.
Connectionnever reaches an H2 client (RFC 9113 §8.2.2) and a close-delimited body ends with END_STREAM (test_h2_close_delimited_body_ends_stream_at_backend_close, from #1640). The new tests check that the H2 connection then serves another stream.Close vs. re-framing as chunked
RFC 9112 would let a proxy re-frame the body as chunked and keep the client connection. I chose closing:
h1_set_cli_conn_mode(src/mux_h1.c), response pass:!(H1_MF_XFER_LEN) || H1_MF_CONN_CLO → H1S_F_WANT_CLO. Its chunk injection needsH1_MF_XFER_LEN(a known length without CL/chunked, e.g. from an H2 backend), so it never re-frames a close-delimited H1 body. HAProxy also strips the server'sConnectionheader (H1_MF_CLEAN_CONN_HDR), so for a framed response withConnection: closeit keeps the client alive. sozu forwards that header, so it has to close.Transfer-Encodinginjection, elidingConnection: close, an HTTP/1.1-client gate, all in the security-sensitive mux. That means more code and more risk for a legacy-backend case.Eliding
Connection: closeon framed responses to keep the client alive, as HAProxy does, could come later as an optimisation. It is not needed for correctness.Tests changed on purpose
Two existing tests pinned the defect itself: each reused a client connection after sozu had forwarded
Connection: closeto it.test_h1_content_length_complete_then_backend_close_stays_alive(fix(mux): end a Content-Length response truncated by a backend close in error #1640) →test_h1_content_length_complete_then_backend_close_closes_client: reads to EOF and requires the full0123456789body, then the close. The reuse leg is gone because RFC 9112 §9.6 forbids it.test_keep_alive, leg "front: keep / back: close": now requires the client connection closed (receive_until_eof+!is_connected()). The next leg reconnects.test_h1_close_delimited_body_ends_at_backend_close→test_h1_close_delimited_body_closes_client: its doc said it deliberately did not pin this defect. It now reads to EOF.Tests
Red on
main(96b4bca), green here:test_h1_close_delimited_body_closes_client…\r\n\r\nabcdreadtest_h1_close_delimited_body_read_with_the_eof_closes_client(body + EOF in one read)test_h1_close_delimited_body_is_not_followed_by_a_pipelined_responseabcdHTTP/1.1 200 OK…pong1: second response glued into the bodytest_h1_content_length_complete_then_backend_close_closes_clienttest_keep_aliveassert!(!client.is_connected())Non-regression (green before and after):
test_h1_framed_keep_alive_responses_keep_the_client: a CL response and a chunked response keep the client connection, and a clientConnection: closestill closes it.test_h2_close_delimited_body_keeps_the_h2_connectionandtest_h2_content_length_with_connection_close_keeps_the_h2_connection: no GOAWAY, and stream 3 is answered.No
sleepadded and no timeout raised. Ports come fromport_registry, and reads useraw_read_until_eof/receive_until_eof.Commands run
All on
ef4b6c3a, exit 0 each:cargo build --lockedcargo build --all-features --locked(clang toolchain foraws_lc_fipswith gcc 16)cargo clippy --all-targets --locked -- -D warningscargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warningscargo +nightly fmt --all -- --checkRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --lockedcargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8: 2204 passed, 0 failed, 14 ignoredcheck_doc_citations.py:--self-test, broken fixture tree (exit 1 + report as expected),--base origin/main, plainDocs
lib/src/protocol/mux/LIFECYCLE.md§8.4: new paragraph on the H1 close after a cleanConnection: closeresponse.lib/src/protocol/kawa_h1/LIFECYCLE.md§2:on_response_headersbullet.CHANGELOG.md: Fixed.Signed-off-by: Florentin Dubois [email protected]