Skip to content

fix(mux-h2): end a chunked H1 body with the last chunk before H2 trailers - #1724

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-h1-trailer-last-chunk
Oct 1, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-h1-trailer-last-chunk

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1722

Problem

An H2 request with no content-length is chunked towards an H1 backend. When it ended with a trailer HEADERS frame, the backend read 5\r\nhello\r\ngrpc-status: 0\r\n\r\n. The last chunk 0\r\n (RFC 9112 §7.1) was missing before the trailer fields, so a strict backend read grpc-status: 0 as an invalid chunk size. On a keep-alive backend connection this breaks request framing. A trailer block with no field left (empty, or every field elided) gave 5\r\nhello\r\n\r\n, a chunked body that never ends, so the H1 peer waited for a next chunk. A response from an H2 backend to an H1 client lost its last chunk the same way.

Cause

pkawa::handle_trailer (lib/src/protocol/mux/pkawa.rs) closed the trailer block with a single Flags { end_body: false, end_header: true, end_stream: true }. kawa's H1 serializer writes 0\r\n only for a Flags block with end_body on a chunked message, so nothing wrote the last chunk.

Fix

Once it has accepted the block, handle_trailer now queues Flags { end_body: true, end_chunk: false, end_header: false, end_stream: false } before the trailer fields; a refused block adds no marker. That is the block shape kawa's H1 parser gives a chunked trailer section. For a chunked message, kawa's H1 serializer now writes 0\r\n, the fields, then the empty line. H2BlockConverter ignores end_body, so H2→H2 is unchanged. H1→H1 was not affected: kawa's parser already queues the marker, and h1_security_tests.rs asserts 0\r\nGrpc-Status: 0\r\n\r\n.

The code change is one blocks.insert at the index where the trailer fields start. The docs are updated in the same change: lib/src/protocol/mux/LIFECYCLE.md §9 gets invariant 32, and CHANGELOG Unreleased gets a Security entry.

Tests

  • handle_trailer_follows_the_last_chunk_on_h1 (unit, pkawa.rs): runs handle_trailer on a chunked request kawa and a chunked response kawa, serializes each with kawa::h1::BlockConverter, and asserts 5\r\nhello\r\n0\r\ngrpc-status: 0\r\n\r\n.
  • handle_trailer_with_every_field_elided_ends_the_chunked_body_on_h1 (unit, pkawa.rs): same, with every trailer field elided; asserts 5\r\nhello\r\n0\r\n\r\n.
  • test_h2_request_trailers_follow_last_chunk_h1_backend (e2e, h2_security_header_injection.rs): an H2 client and a raw H1 backend. It asserts the exact chunked body the backend reads, with a grpc-status trailer (…0\r\ngrpc-status: 0\r\n\r\n) and with a trailer that is fully elided (…0\r\n\r\n).

Both tests were run red with the fix removed:

e2e:  H2-TRAILER-LAST-CHUNK-H1 FAIL — chunked=true body="5\r\nhello\r\ngrpc-status: 0\r\n\r\n" expected="5\r\nhello\r\n0\r\ngrpc-status: 0\r\n\r\n"
unit: Request: the trailer section must follow the last chunk
        left: "5\r\nhello\r\ngrpc-status: 0\r\n\r\n"
       right: "5\r\nhello\r\n0\r\ngrpc-status: 0\r\n\r\n"
unit: Request: an emptied trailer block must still end the body
        left: "5\r\nhello\r\n\r\n"
       right: "5\r\nhello\r\n0\r\n\r\n"

With the fix they pass.

Interaction with #1720

#1720 (fix/h2-trailer-forbidden-fields) also touches handle_trailer. git merge-tree of the two branches reports no conflict. Its e2e test test_h2_trailer_forbidden_fields_dropped_h1_backend accepts the backend bytes with or without the 0\r\n last-chunk line (strip_prefix("0\r\n")), so it passed while this defect was present. Once both PRs land, that optional strip could become a required 0\r\n.

Validation

  • cargo +nightly fmt --all --check → 0
  • cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings → 0
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked → 0
  • cargo test -p sozu-lib --all-features --locked → 0 (1447 passed; after the review follow-up, -- pkawa → 0, 74 passed)
  • cargo test -p sozu-e2e --locked -- trailer → 0 (18 passed); -- protocol_pair_matrix → 0 (28 passed); -- h2 → 0 (268 passed) — trailer rerun after the follow-up → 0 (18 passed)
  • cd fuzz && cargo +nightly fuzz build → 0
  • check_doc_citations.py --self-test, --root ., --root . --base origin/main, --audit --root . → 0 each

…lers

pkawa::handle_trailer closed an H2 trailer block with a single Flags
block without end_body, the marker for which kawa's H1 serializer writes
the last chunk. An H2 request with trailers therefore reached an H1
backend as 5\r\nhello\r\ngrpc-status: 0\r\n\r\n, the first trailer
field standing where RFC 9112 section 7.1 puts the last chunk, which
breaks request framing on a keep-alive backend connection. A trailer
block with no field left (empty, or every field elided) gave
5\r\nhello\r\n\r\n, a chunked body that never ends. A response from an
H2 backend to an H1 client lost its last chunk the same way.

Once the block is accepted, queue Flags { end_body: true } before the
trailer fields, the shape kawa's H1 parser gives a chunked trailer
section; a refused block adds no marker. H2BlockConverter ignores
end_body, so an H2 peer is unaffected.

Closes #1722

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/h2-h1-trailer-last-chunk branch from d81ffd1 to a45bbf0 Compare September 30, 2026 15:34
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 3d3f077 into main Oct 1, 2026
51 of 52 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h2-h1-trailer-last-chunk branch October 1, 2026 05:40
FlorentinDUBOIS added a commit that referenced this pull request Oct 1, 2026
…1731)

Closes #1730

## Change

H2→H1: drop the trailer fields of a Content-Length-framed message instead of writing them after the body.

HTTP/1.1 carries a trailer section only with chunked transfer coding (RFC 9112 §7.1.2), and a message framed by Content-Length ends with its last body byte (RFC 9112 §6.3). RFC 9110 §6.5 lets a recipient discard trailers.

- `ConnectionH1::drop_length_framed_trailers` (`lib/src/protocol/mux/h1.rs`) runs in `ConnectionH1::writable` before kawa's H1 `prepare`. For a `BodySize::Length` message whose queue ends with a trailer block, it removes the trailer fields and clears `end_header` on the closing flags, so nothing is written after the body. This covers requests to an H1 backend and responses to an H1 client.
- Each dropped block logs a `warn!` and increments `h2.trailers_dropped_content_length` at the write.
- `pkawa::handle_trailer` still validates, elides and queues the block, so H2 peers keep these trailers and the `h2.trailer.*_elided` counters are unchanged.
- Docs: `doc/configure.md` metric row, `lib/src/protocol/mux/LIFECYCLE.md` §8.4, CHANGELOG (Unreleased, Security).

#1724 (last chunk before H2 trailers on a chunked H1 body) is merged; this branch is rebased on it.

## Tests

- `a_length_framed_message_writes_no_trailer_to_an_h1_peer` (`h1.rs`): request and response, with one trailer field and with none; H1 bytes are exactly `hello`.
- `a_length_framed_header_section_is_not_taken_for_trailers` (`h1.rs`).
- `a_length_framed_trailer_block_reaches_an_h2_peer` (`converter.rs`): DATA, then HEADERS with END_STREAM holding the trailer field.
- `handle_trailer_filters_and_keeps_a_length_framed_block` (`pkawa.rs`): elision counters fire for a length-framed block.
- `test_h2_length_framed_request_trailers_keep_h1_backend_framing` (e2e): the backend bytes are split by HTTP/1.1 framing, and the byte after the body starts the next request line.

## Validation

- fmt, MSRV clippy, rustdoc `-D warnings`, `cargo test -p sozu-lib --all-features`, fuzz build, and the 4 `check_doc_citations.py` modes all exit 0.
- e2e `protocol_pair_matrix` and `h2_security_header_injection` pass.
- e2e `trailer`: 21 passed, 1 failed, `test_h1_trailer_field_limit_exceeded_split_closes_backend`. That failure is pre-existing on `main` (H1→H1, chunked body) and is being fixed separately.



Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 3, 2026
…1809)

Audit of the docs, `bin/config.toml` and `os-build/config.toml` against everything merged on `main` from 2026-10-01 to 2026-10-03 (43 commits, #1724 through #1808). Most of the new keys were already documented by their own PRs (`max_connection_attempts`, `h2_stream_refusal_percent`, health check `mode`/`accepted_statuses`, the raised flood defaults, the 16 MiB connection window). This PR fixes what was still stale.

| Area | Where | Was | Now |
|---|---|---|---|
| #1806 | `bin/config.toml`, `os-build/config.toml` answer comments | 503 only for "no backend available"; "504 means the backend timed out" | 503 also when every attempt failed; 504 = connected backend silent or frontend timeout while waiting; connect timeout fails over |
| #1806 | `bin/config.toml` `max_connection_attempts` | connect timeout always fails over | a TCP session whose connect times out is closed |
| #1806 | `doc/debugging_strategies.md` | 503 after a circuit breaker of 3 failed connections; ERROR log lines, one that no longer exists | `max_connection_attempts` (default 5), `backend.connect.retries_exhausted`, real TCP log levels and text |
| #1806 | `doc/configure.md` `front_timeout` / `connect_timeout` rows | "inactivity for a request to connect" | time to establish a backend connection; HTTP fails over, TCP closes; H1 failover bound by `front_timeout` |
| #1806 | `doc/configure.md` `client_timeout_during_response`, `backend_timeout`, `http.503.errors` | "connection-level slowness", "circuit breaker" | failover case included; established connection only; attempt budget |
| #1806 | `LIFECYCLE.md` backend timer | 504 for any linked stream | established connection only |
| #1806 | `cli.rs` help, `doc/configure_cli.md` | "answered 503" | "or a TCP session is closed" |
| #1756 | `doc/configure.md` flood metrics | RST lifetime rows described as absolute ceilings; no `window_update_stream0_window` row | floor-and-ratio semantics; row added |
| #1756/#1798 | `doc/architecture.md`, `doc/h2_mux_internals.md` | 6 / 13 `H2FloodConfig` thresholds | 14, with `h2_stream_refusal_percent` |
| #1756 | `doc/configure_admin_ops.md` | "halve" example 50/25 (old defaults); H2 knobs HTTPS only | 1000/500; HTTP and HTTPS; `h2.flood.stream_refused` |
| #1752 | `doc/configure.md` glitch tuning | "1 MiB window ... about 64" frames | 64 KiB stream window, about four |
| #1805 | `doc/health_checks.md` | `remove` resets backends to healthy; DOWN logs an error; `load_balancing.rs::BackendList::healthy` | backends keep their last state (see note); warning; `BackendList::select_tiers` in `backends.rs` |
| #1805 | `doc/metrics.md` | health-check counters "per cluster" | unlabelled |
| #1805 | `doc/configure_cli.md` | no health-check section | short section, `--mode`, `--probe-timeout` vs global `--timeout` |
| #1746 | `cli.rs` `--load-balancing-policy`, `bin/config.toml`, proto comment | HRW/MAGLEV key = UDP flow key | UDP affinity key (source IP, or IP and port) |
| UDP | `doc/configure.md` | hot upgrade drains flows bounded by idle timeout; `udp.flows.evicted` reasons | soft-stop closes flows at once; listener removal/soft-stop/abort added |
| #1770 | `doc/udp_simulation.md` | no per-source limits | in reconfig storms and the invariant list |
| #1743 | `doc/lifetime_of_a_session.md` | keep-alive backend always reused | unless its backend was removed |
| older | `command.proto` update messages | `h2_stream_shrink_ratio >= 1` | `>= 2`, as validated in `state.rs` |
| #1806 | e2e shuffle-sharding doc comment | "three connection attempts" | `max_connection_attempts` (5 by default) |

Reported here, not changed in this PR:
- `sozu cluster health-check remove` does not reset backends a probe marked DOWN (`remove_health_check_state` skips the reset that `set_health_check_config(None)` does), so they stay out of rotation until the cluster is added again. The doc now describes this. It looks like a code bug.
- `sozu top`'s H2 pane reads `h2.flood.violation.{rapid_reset,made_you_reset,ping,settings,priority,continuation}`, which the detector never emits (it emits `rst_stream_pre_response_lifetime`, `ping_window`, ...). Only `glitch_window` matches, and `h2.flood.stream_refused` is not shown.
- `h2_flood_detector.rs` comments still refer to a 1 MiB connection window, and the `h2_stream_idle_timeout_seconds` proto comment says "Default: 30" where the value inherits `back_timeout`.
- `connections.rejected_per_cluster_ip` is documented as also counting TCP rejections; only the HTTP 429 path increments it.

Validation:
- `sozu config check` passes on `bin/config.toml`, on a copy with every commented key under review uncommented (global and cluster `max_connection_attempts`, the H2 block, the health check block), and on `os-build/config.toml` with local state/socket paths. Negative controls (`max_connection_attempts = 0`; `expected_status = 200` next to `accepted_statuses`) are rejected.
- `cargo +1.93.1 test -p sozu-command-lib --all-features --locked`: 235 passed.
- `cargo +1.93.1 test -p sozu --bin sozu --locked`: 124 passed (including the `--load-balancing-policy` help tests).
- `cargo +nightly fmt --all --check` and `python3 .github/scripts/check_doc_citations.py --root . --base origin/main` are clean.



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.

H2 trailers serialized to an HTTP/1.1 peer omit the last chunk (0\r\n)

1 participant