Repository navigation
fix(mux-h2): drop forbidden fields from H2 request trailers and count them - #1720
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 30, 2026 11:52
… them #1701 made the H1 frontend elide the 25 names of TRAILER_FORBIDDEN_FIELDS from a chunked trailer section, but pkawa::handle_trailer still checked only TRAILER_SPOOF_VECTOR_HEADERS. An H2 client could therefore hand an H1 backend (chunked trailer section) or an H2 backend (trailer HEADERS frame) a Content-Length, Authorization, Cookie, Cache-Control, If-Match, Content-Type or any other field RFC 9110 section 6.5.1 keeps out of trailers. H2BlockConverter only removed Host and Trailer. handle_trailer now elides every request trailer field the shared list names while it decodes the frame, before a backend is chosen, so both backend protocols receive the same fields. Each elided field increments the new h2.trailer.forbidden_field_elided counter, the H2 counterpart of http.trailer.forbidden_field_elided. The check runs after classify_invalid_h2_header, so the connection-specific names both lists hold still reset the stream (RFC 9113 section 8.2.2). It also runs after the field count, so an elided field still counts against h2_max_header_fields. Response trailers are unchanged, as on H1. The H2Backend e2e mock gains an opt-in start_recording_trailers so the H2-to-H2 case can read the trailers the backend decoded. The pinned code blocks of doc/h2_mux_internals.md follow the lines this change shifts. Closes #1714 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h2-trailer-forbidden-fields
branch
from
September 30, 2026 12:24
a00b896 to
dcca0e5
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 1, 2026
…lers (#1724) 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 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
H2 request trailers now lose the fields RFC 9110 §6.5.1 keeps out of trailers, the same way H1 chunked trailers have since #1707. Before this change,
pkawa::handle_trailer(lib/src/protocol/mux/pkawa.rs) checked onlyTRAILER_SPOOF_VECTOR_HEADERS. It now also elides every request trailer field named in the sharedTRAILER_FORBIDDEN_FIELDS(lib/src/protocol/kawa_h1/editor.rs). Each elided field increments a newh2.trailer.forbidden_field_elidedcounter, the H2 counterpart ofhttp.trailer.forbidden_field_elided.Closes #1714
Security impact
Before this change, an H2 client could send
Content-Length,Authorization,Cookie,Cache-Control,If-Match,Content-Typeand the other forbidden fields as trailers, and they reached:H2BlockConverterremoved onlyhostandtraileron its own.A backend that merges trailers into its header view then acted on values that never went through the header-block handling (RFC 7230 §4.1.2: "might bypass external security filters").
The filter runs while the frontend decodes the frame, before a backend is chosen. The set of trailer fields left for an H1 or an H2 backend is therefore the same. This PR says nothing about how each backend protocol frames them: the H2→H1 framing defect described below is fixed separately.
Fix and ordering
The check sits in the
handle_trailerdecode closure:classify_invalid_h2_header. The connection-specific names both lists hold (connection,keep-alive,proxy-connection,transfer-encoding,upgrade, and ateother thantrailers) still reset the stream, as RFC 9113 §8.2.2 requires. They are not silently elided.te: trailersis elided. It is the onetevalue §8.2.2 allows, so it passes that check. As a request trailer it is then elided and counted like any other forbidden name.h2_max_header_fields, as on H1.Kind::Request). Response trailers are left alone, asHttpContext::filter_request_trailersleaves them on H1.The name comparison is a plain byte comparison, because
classify_invalid_h2_headerhas already refused any uppercase name. The comparison applies to the decoded name, so a name taken from the HPACK dynamic table is caught the same way as a literal one.Tests
Every new test was seen red under the matching mutation, then green with the fix restored.
Unit tests, six new in
pkawa.rs:test_handle_trailer_elides_forbidden_request_fieldsx-checksumandgrpc-statusare kept.test_handle_trailer_still_refuses_connection_specific_fieldsProtocolErrorand are not counted.test_handle_trailer_elides_te_trailerste: trailerspasses the connection check, then is elided and counted.tekept)test_handle_trailer_elided_fields_count_against_the_boundEnhanceYourCalm.field_countincrement (Ok(()))test_handle_trailer_elides_a_forbidden_name_from_the_dynamic_tableauthorization, named by HPACK dynamic index 62 from an earlier block, is still elided and counted.authorizationkept)test_handle_trailer_keeps_forbidden_names_in_responsesE2E tests, in
e2e/src/tests/h2_security_header_injection.rs:test_h2_trailer_forbidden_fields_dropped_h1_backendreads the raw bytes the H1 backend receives. RED:…grpc-status: 0\r\ncontent-length: 6666\r\nhost: forged.example\r\ncache-control: forged\r\nif-match: "forged"\r\nauthorization: Bearer forged\r\ncookie: session=forged\r\ncontent-type: text/forged\r\n\r\n. The test checks which fields survive, not the chunked framing around them. It tolerates the missing0\r\nlast-chunk line described below, which is being fixed separately.test_h2_trailer_forbidden_fields_dropped_h2_backendreads the trailers the H2 backend decoded. RED:grpc-status,content-length,cache-control,if-match,authorization,cookieandcontent-typeall arrived.hostwas already removed byH2BlockConverter.The
H2Backendmock gains an opt-inH2Backend::start_recording_trailers.H2Backend::startis unchanged, so the response timing of existing users stays the same.Separate defect, tracked and fixed separately
On H2→H1, the H1 backend receives the trailer section without the last-chunk line:
This already happens on
origin/mainand does not depend on this change. The cause is thathandle_trailercloses the block withend_body: false, so the H1 serializer never writes0\r\n. It is being fixed in its own issue and PR, not here.Docs
lib/src/protocol/kawa_h1/LIFECYCLE.md§2.3: the "H2 frontend does not yet apply" paragraph is replaced with the same-field-set wording, and the covered-by list is extended with the six unit tests and two e2e tests.doc/configure.md:http.trailer.forbidden_field_elidedrow;h2.trailer.forbidden_field_elidedrow, which documents thete: trailersexception;h2.trailers_dropped_content_lengthrow. The counter fires when an accepted trailer block arrives on a message framed byContent-Length(BodySize::Length), whatever fields the block holds. It does not count acontent-lengthtrailer field, which is elided and counted inh2.trailer.forbidden_field_elided.lib/src/metrics/names.rsandeditor.rs: doc comments updated.CHANGELOG.md: Unreleased, Security.doc/h2_mux_internals.md: 10 pinned code blocks now point at the lines this change shifted inpkawa.rs(+3) andh2.rs(+5).Validation
Run on the final head
dcca0e58:cargo +nightly fmt --all --checkcargo +1.93.1 clippy --all-features --all-targets --locked -- -D warningsRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --lockedcargo test -p sozu-lib --lib -- pkawa::tests::test_handle_trailer(7 passed)cargo test -p sozu-e2e --locked -- trailer h2_security_header_injection(27 passed)check_doc_citations.py --self-test/--root ./--root . --base origin/main/--audit --root .Run on the first revision, before these review fixes:
cargo test -p sozu-lib --all-features --locked(1449 unit tests, splice test included)cargo test -p sozu-e2e --locked -- trailer h2_security_header_injection x_real_ip protocol_pair_matrix(60 passed)cd fuzz && cargo +nightly fuzz build