Skip to content

fix(mux): close an H1 client connection after the backend's Connection: close - #1648

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-close-delimited-closes-client
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-close-delimited-closes-client

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

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 on keep_alive_frontend alone, which only follows the client's request. For a close-delimited body (neither Content-Length nor chunked, 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;
  • with pipelining, the response to the next request was appended to that body.

Closes #1642.

Fix

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 uses: access log, then defer_close_for_tls_flush (write-only shutdown once the response is flushed, never Shutdown::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 == false covers both cases in the issue: an explicit backend Connection: close (forwarded unchanged by HttpContext::on_response_headers), and every close-delimited body, because ConnectionH1::terminate_close_delimited only runs for such a backend. No new field, no allocation.

Why not in HttpContext::on_response_headers: HttpContext also serves H2 frontends, and ConnectionH2::write_streams sends GOAWAY when keep_alive_frontend is false. Clearing it there would GOAWAY every H2 client behind a Connection: close backend 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. Connection never 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:

  • HAProxy closes too. 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 needs H1_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's Connection header (H1_MF_CLEAN_CONN_HDR), so for a framed response with Connection: close it keeps the client alive. sozu forwards that header, so it has to close.
  • Re-framing cost: a chunked output path in the kawa converter, a Transfer-Encoding injection, eliding Connection: 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: close on 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: close to 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 full 0123456789 body, 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 Red on main
test_h1_close_delimited_body_closes_client still open after 5 s, …\r\n\r\nabcd read
test_h1_close_delimited_body_read_with_the_eof_closes_client (body + EOF in one read) still open after 5 s
test_h1_close_delimited_body_is_not_followed_by_a_pipelined_response abcdHTTP/1.1 200 OK…pong1: second response glued into the body
test_h1_content_length_complete_then_backend_close_closes_client still open after 5 s
test_keep_alive assert!(!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 client Connection: close still closes it.
  • test_h2_close_delimited_body_keeps_the_h2_connection and test_h2_content_length_with_connection_close_keeps_the_h2_connection: no GOAWAY, and stream 3 is answered.

No sleep added and no timeout raised. Ports come from port_registry, and reads use raw_read_until_eof / receive_until_eof.

Commands run

All on ef4b6c3a, exit 0 each:

  • cargo build --locked
  • cargo build --all-features --locked (clang toolchain for aws_lc_fips with gcc 16)
  • cargo clippy --all-targets --locked -- -D warnings
  • cargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings
  • cargo +nightly fmt --all -- --check
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked
  • cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8: 2204 passed, 0 failed, 14 ignored
  • check_doc_citations.py: --self-test, broken fixture tree (exit 1 + report as expected), --base origin/main, plain

Docs

  • lib/src/protocol/mux/LIFECYCLE.md §8.4: new paragraph on the H1 close after a clean Connection: close response.
  • lib/src/protocol/kawa_h1/LIFECYCLE.md §2: on_response_headers bullet.
  • CHANGELOG.md: Fixed.

Signed-off-by: Florentin Dubois [email protected]

…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
FlorentinDUBOIS force-pushed the fix/h1-close-delimited-closes-client branch from ef4b6c3 to 21f9ff9 Compare September 28, 2026 07:00
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]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit eadfa4a into main Sep 28, 2026
51 of 52 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h1-close-delimited-closes-client branch September 28, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mux: a close-delimited response keeps the H1 client connection open after the body

1 participant