Repository navigation
fix(h1): keep neither connection alive after a response that completed before its request - #1726
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 30, 2026 13:12
FlorentinDUBOIS
force-pushed
the
fix/h1-early-response-keepalive
branch
from
September 30, 2026 13:54
7c094c2 to
1a80bdf
Compare
…d before its request A backend answering an H1 request completely and early (a 413 or a 401 on an upload) while the client was still sending the body made `ConnectionH1::writable` apply keep-alive as soon as the response left. Its reset put the request parser back to the status line, so the rest of the body was parsed, routed and forwarded as a new request (request smuggling), and `ConnectionH1::end_stream` pooled the backend connection although it still expected that body. The client connection is now kept only once the request was received whole, and otherwise closes after the response is flushed (`request-incomplete`, counted in `http.close.request_incomplete`). A backend connection is pooled only once the request was also fully written to it, and is disconnected otherwise (RFC 9112 §9.3). The backend guard also covers an H2 client resetting a stream after the whole response but before its END_STREAM. Every close of that branch marks the stream closing, so a TLS close deferred behind close_notify no longer completes the response a second time on the next writable pass (a duplicate access log and counters, and a keep-alive decision re-taken after close_notify). Closes #1721 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h1-early-response-keepalive
branch
from
September 30, 2026 15:32
1a80bdf to
8052483
Compare
This was referenced Oct 1, 2026
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 1, 2026
…nfinished request (#1748) Follow-up to #1719 and #1726. ## Problem `test_h1_trailer_field_limit_exceeded_split_closes_backend` fails on `main` since #1719 was squash-merged on top of #1726. After a 400 for an over-bound trailer section, the test expected a follow-up request on the same client connection to be served. #1726 closes the H1 client connection when a response completes before its request was received whole (`request-incomplete`). An over-bound trailer section leaves the request unfinished. Its end cannot be found, so the connection cannot carry another request (RFC 9112 §6.3). The close is intended. The test expectation was wrong. ## Change Test-side only, plus doc corrections. No runtime change. - The test now checks that the client connection reads EOF after the 400 with no further bytes, and that a follow-up request reaches no backend. The other checks are kept: head and body forwarded, 400 answered, and the backend connection holding the cut request closed. The 400 template still omits `Connection: close`, so the close can only come from the incomplete request. - CHANGELOG (#1716 entry), `lib/src/protocol/kawa_h1/LIFECYCLE.md` §2.3 and the 400-arm comment in `lib/src/protocol/mux/h1.rs` said the client connection stays as persistent as the answer's `Connection` header. They now say it closes. Coverage gap: since #1726, removing the 400-arm `end_stream` block from `ConnectionH1::readable` no longer turns this test red. The client close tears the backend down as well, and no `backend_streams` panic fires. The test doc says so. The 400 arm of #1719 has no dedicated unit test. The cut-response arm keeps one. ## Test Unmodified test on `origin/main` (`c2cad338..3d3f077`), red: ``` TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 365 bytes ending "...\r\n\r\n5\r\nHello\r\n0\r\n", client got Some("HTTP/1.1 400 Bad Request"), backend_closed=true, follow-up got "" TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: FAIL — the next request on the client connection was not served test result: FAILED. 0 passed; 1 failed ``` Updated test, green (3 iterations): ``` TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 365 bytes ending "...\r\n\r\n5\r\nHello\r\n0\r\n", client got Some("HTTP/1.1 400 Bad Request"), backend_closed=true, client_closed=true, after the 400 "", reaccepted=false test result: ok. 1 passed; 0 failed ``` ## Validation | Check | Exit code | Result | |---|---|---| | `cargo +nightly fmt --all --check` | 0 | | | `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` | 0 | | | `RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps` | 0 | | | `cargo test -p sozu-e2e --locked --features opentelemetry,splice,simd -- h1_security_tests`, 3 runs | 0, 0, 0 | 41 passed each time | | `cargo test -p sozu-e2e ... -- test_h1_early_response_mid` (#1726) | 0 | 2 passed | | `cargo test -p sozu-lib --all-features --locked --lib -- protocol::mux::h1` | 0 | 21 passed, including the #1719 and #1726 unit tests | | `cd fuzz && cargo +nightly fuzz build` | 0 | | | `check_doc_citations.py` `--self-test`, `--root .`, `--root . --base origin/main`, `--audit --root .` | 0, 0, 0, 0 | | Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 1, 2026
#1719 made both Position::Server arms of the parse-error branch in ConnectionH1::readable end the linked backend stream. Only the cut-response arm had a unit test. The 400 arm relied on the e2e test test_h1_trailer_field_limit_exceeded_split_closes_backend, which no longer catches a regression there: since #1726 the client connection closes after a 400 for an unfinished request, and that close tears the backend down too. a_request_error_answered_400_after_linking_ends_the_backend_stream links a pooled mock H1 backend, reads its complete response without starting it on the client's wire, then sends a malformed chunk. It asserts that the stream leaves backend_streams, and that the backend no longer carries it and is Disconnecting, not pooled. Seen red with the 400-arm block removed: "the rejected stream must leave the backend reverse index". Green with it. CHANGELOG: the #1716 entry names the new test. Follow-up to #1719. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 1, 2026
Follow-up to #1719. ## Problem #1719 made both `Position::Server` arms of the parse-error branch in `ConnectionH1::readable` (`lib/src/protocol/mux/h1.rs`) end the linked backend stream. Only the cut-response arm has a unit test. The 400 arm relied on the e2e test `test_h1_trailer_field_limit_exceeded_split_closes_backend`, which no longer catches a regression there: since #1726 the client connection closes after a 400 for an unfinished request, and that close tears the backend down too (see #1748). ## Change Test only. `a_request_error_answered_400_after_linking_ends_the_backend_stream` links a pooled mock H1 backend and reads its complete response without starting it on the client's wire, then sends a malformed chunk. It asserts that the request is answered on the client connection, that the stream leaves `backend_streams`, and that the backend no longer carries it and is `Disconnecting`, not pooled. The CHANGELOG #1716 entry names the new test. ## Test Red, on a `git archive` copy with the 400-arm block removed: ``` test protocol::mux::h1::tests::a_request_error_answered_400_after_linking_ends_the_backend_stream ... FAILED panicked at lib/src/protocol/mux/h1.rs:2677:9: the rejected stream must leave the backend reverse index test result: FAILED. 2 passed; 1 failed ``` Green, on this branch: ``` test protocol::mux::h1::tests::a_request_error_answered_400_after_linking_ends_the_backend_stream ... ok test result: ok. 3 passed; 0 failed ``` ## Validation | Check | Exit code | Result | |---|---|---| | `cargo +nightly fmt --all --check` | 0 | | | `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` | 0 | | | `RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps` | 0 | | | `cargo test -p sozu-lib --all-features --locked` | 0 | 1465 unit tests passed; integration tests passed | | `cd fuzz && cargo +nightly fuzz build` | 0 | | | `check_doc_citations.py` `--self-test`, `--root .`, `--root . --base origin/main`, `--audit --root .` | 0, 0, 0, 0 | | Stacked on #1739. 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.
Closes #1721
Problem
When a backend answers an HTTP/1.1 request completely and early (a 413 or a 401 on an upload, keep-alive) while the client is still sending the body, the H1 frontend applied keep-alive as soon as the response had left:
ConnectionH1::writablecallsstream.front.clear(), which puts the parser back to its status line whilefront.storageis kept for pipelining. The rest of the body was parsed as a new request, then routed, edited (Sozu-Id,X-Forwarded-*) and forwarded like any other. That is a request-smuggling primitive.ConnectionH1::end_streammoved the backend connection toBackendStatus::KeepAliveonkeep_alive_backend && back.is_terminated()only, so it was pooled while it still expected the rest of the body. The next request was written into it.Fix
RFC 9112 §9.3: a connection carries another message only once the previous one is complete.
ConnectionH1::writablekeeps the client only whenstream.front.is_terminated(), meaning the request was received whole. Otherwise it closes once the response is flushed, throughdefer_close_for_tls_flush("request-incomplete"), the same exit a backendConnection: closealready takes. The parser is not reset on that path, so the remaining body bytes are read as body and dropped with the connection. It does not require the request to have been written upstream: a default answer leaves unsent a request it never forwarded, so a keep-alive template stays keep-alive.ConnectionH1::end_streampools aConnectedbackend only when the request is terminated and completed (fully written), as well as the response. Otherwise it disconnects the backend. This guard also covers an H2 frontend whose client resets a stream after the whole response but before END_STREAM. On the client side the H2 frontend was already safe, becausetry_recycle_server_streamwaits for END_STREAM, and an H2 backend is reset with RST_STREAM(CANCEL).A request pipelined behind such an upload is dropped with the connection, and the client retries it (§9.3.2). A client that has not yet read the response when the close arrives may see a reset instead, exactly as with a backend
Connection: closeresponse mid-upload. Sozu does not drain the rest of the body to keep the connection.Docs:
lib/src/protocol/mux/LIFECYCLE.md§8.4, andCHANGELOG.mdUnreleased / Security.Tests
test_h1_early_response_mid_content_length_uploadandtest_h1_early_response_mid_chunked_upload(e2e/src/tests/h1_security_tests.rs): headers and part of the body, an early keep-alive 413, then the rest of the body (byte-for-byte aGET /smuggledrequest) and a second request. They assert that no/smuggledrequest carrying aSozu-Idreaches any backend connection, that no request is sent on the connection that answered early, and that the client gets no response to it.backend connection 0 after the 413: "GET /smuggled HTTP/1.1\r\n…Sozu-Id: …",client after the 413: "HTTP/1.1 200 OK…",test result: FAILED. 0 passed; 2 failed.test result: ok. 2 passed.a_backend_that_answered_before_the_whole_request_is_not_pooled(lib/src/protocol/mux/h1.rs): a request inBodyorChunks, orTerminatedwith unwritten blocks, does not pool the backend, and a complete one does. It was seen red with bothfrontconditions removed, and again with onlyis_completed()removed.keep_alive_requests_read_tcp_info_once_per_connectionnow stages its request asTerminated: it drives a complete request, and without that staging its fresh kawa looked like a request not yet received.Review follow-ups
stream.context.closing. A TLS close deferred behindclose_notifyused to run the Complete branch a second time on the nextwritablepass. That logged the request twice (the second line was413with zero durations and zero bytes), counted it twice, and re-took the keep-alive decision afterclose_notify. The re-entry now takes theclosing-contextexit instead. Covered by the unit testa_deferred_tls_close_completes_the_response_once, which fails without the fix: 2H1::Completelines, and the second pass returnedContinue(keep-alive) instead ofCloseSession.http.close.request_incomplete, documented indoc/configure.md.fully_completedtestsfront.is_terminated(), notis_completed(). This PR leaves that path unchanged; it is a separate follow-up.