Skip to content

fix(h1): keep neither connection alive after a response that completed before its request - #1726

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-early-response-keepalive
Sep 30, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-early-response-keepalive

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • Client side: the keep-alive reset in ConnectionH1::writable calls stream.front.clear(), which puts the parser back to its status line while front.storage is 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.
  • Backend side: ConnectionH1::end_stream moved the backend connection to BackendStatus::KeepAlive on keep_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::writable keeps the client only when stream.front.is_terminated(), meaning the request was received whole. Otherwise it closes once the response is flushed, through defer_close_for_tls_flush("request-incomplete"), the same exit a backend Connection: close already 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_stream pools a Connected backend 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, because try_recycle_server_stream waits 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: close response mid-upload. Sozu does not drain the rest of the body to keep the connection.

Docs: lib/src/protocol/mux/LIFECYCLE.md §8.4, and CHANGELOG.md Unreleased / Security.

Tests

  • e2e test_h1_early_response_mid_content_length_upload and test_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 a GET /smuggled request) and a second request. They assert that no /smuggled request carrying a Sozu-Id reaches any backend connection, that no request is sent on the connection that answered early, and that the client gets no response to it.
    • Red on origin/main: 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.
    • Green with the fix: test result: ok. 2 passed.
  • unit a_backend_that_answered_before_the_whole_request_is_not_pooled (lib/src/protocol/mux/h1.rs): a request in Body or Chunks, or Terminated with unwritten blocks, does not pool the backend, and a complete one does. It was seen red with both front conditions removed, and again with only is_completed() removed.
  • keep_alive_requests_read_tcp_info_once_per_connection now stages its request as Terminated: it drives a complete request, and without that staging its fresh kawa looked like a request not yet received.

Review follow-ups

  • Every close in this branch now sets stream.context.closing. A TLS close deferred behind close_notify used to run the Complete branch a second time on the next writable pass. That logged the request twice (the second line was 413 with zero durations and zero bytes), counted it twice, and re-took the keep-alive decision after close_notify. The re-entry now takes the closing-context exit instead. Covered by the unit test a_deferred_tls_close_completes_the_response_once, which fails without the fix: 2 H1::Complete lines, and the second pass returned Continue (keep-alive) instead of CloseSession.
  • New counter http.close.request_incomplete, documented in doc/configure.md.
  • LIFECYCLE.md now describes the H2 backend path accurately: fully_completed tests front.is_terminated(), not is_completed(). This PR leaves that path unchanged; it is a separate follow-up.
  • CHANGELOG: Sozu does not drain the rest of the body, so a client still uploading may see a reset. A bounded lingering drain is a follow-up.

…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
FlorentinDUBOIS force-pushed the fix/h1-early-response-keepalive branch from 1a80bdf to 8052483 Compare September 30, 2026 15:32
@FlorentinDUBOIS
FlorentinDUBOIS merged commit d3b5aef into main Sep 30, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h1-early-response-keepalive branch September 30, 2026 17:07
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]>
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.

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

1 participant