Repository navigation
fix(h1): drop forbidden fields from chunked request trailers and bound their count - #1707
Merged
Merged
Conversation
…d their count Closes #1701 After #1689 the H1 frontend still forwarded every chunked request trailer field other than the seven spoof vectors, including the framing, routing and authentication fields RFC 9110 section 6.5.1 keeps out of trailers (Content-Length, Transfer-Encoding, Host, Authorization, ...), and nothing but the buffer bounded the number of trailer fields, while H2 caps a trailer block at h2_max_header_fields. The trailer filter becomes HttpContext::filter_request_trailers. It still walks only the blocks the last parse appended, and returns before reading a block for a request without a trailer section. - It also elides the 25 names of the new TRAILER_FORBIDDEN_FIELDS: the examples RFC 7230 section 4.1.2 gave for each category RFC 9110 section 6.5.1 lists (framing, routing, request controls and conditionals, authentication, content processing), plus the connection-specific fields of RFC 9110 section 7.6.1. RFC 7230 section 4.1.2 has a recipient ignore them and RFC 9112 section 7.1.2 lets it discard any trailer field, so the request is forwarded without them rather than refused. New counter http.trailer.forbidden_field_elided. - Every trailer field, elided or not, counts against the listener's h2_max_header_fields, as pkawa::handle_trailer counts an H2 trailer block. The running count lives in HttpContext (reset per pipelined request) because earlier fields may already be forwarded. The field over the bound marks the request in error, so the existing parse-error path answers 400, or cuts a response already started; that field is never forwarded. New counter http.trailer.field_limit_exceeded. - The advisory Trailer request header is forwarded as sent: RFC 9110 section 6.6.2 makes it a hint the sender need not follow through. The H2 frontend is unchanged. Tests: four editor unit tests and seven e2e tests in h1_security_tests (forbidden fields dropped whole, split after the last chunk, split inside the trailers, pipelined; 128 fields forwarded; 129 answered 400, in one write and after the body was forwarded). Documented in lib/src/protocol/kawa_h1/LIFECYCLE.md section 2.3, doc/configure.md and CHANGELOG.md. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 29, 2026 23:54
This was referenced Sep 30, 2026
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 30, 2026
… them (#1720) ## 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 only `TRAILER_SPOOF_VECTOR_HEADERS`. It now also elides every request trailer field named in the shared `TRAILER_FORBIDDEN_FIELDS` (`lib/src/protocol/kawa_h1/editor.rs`). Each elided field increments a new `h2.trailer.forbidden_field_elided` counter, the H2 counterpart of `http.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-Type` and the other forbidden fields as trailers, and they reached: - **an H1 backend**, in the chunked trailer section; - **an H2 backend**, in the trailer HEADERS frame. `H2BlockConverter` removed only `host` and `trailer` on 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_trailer` decode closure: - **After `classify_invalid_h2_header`.** The connection-specific names both lists hold (`connection`, `keep-alive`, `proxy-connection`, `transfer-encoding`, `upgrade`, and a `te` other than `trailers`) still reset the stream, as RFC 9113 §8.2.2 requires. They are not silently elided. - **`te: trailers` is elided.** It is the one `te` value §8.2.2 allows, so it passes that check. As a request trailer it is then elided and counted like any other forbidden name. - **After the field count.** An elided field still counts against `h2_max_header_fields`, as on H1. - **Requests only** (`Kind::Request`). Response trailers are left alone, as `HttpContext::filter_request_trailers` leaves them on H1. The name comparison is a plain byte comparison, because `classify_invalid_h2_header` has 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 | What it checks | Red under | |---|---|---| | `test_handle_trailer_elides_forbidden_request_fields` | All 19 forbidden names that are not connection-specific are elided and counted once each; `x-checksum` and `grpc-status` are kept. | forbidden check disabled | | `test_handle_trailer_still_refuses_connection_specific_fields` | The 6 connection-specific names still give `ProtocolError` and are not counted. | forbidden check moved above the field count | | `test_handle_trailer_elides_te_trailers` | `te: trailers` passes the connection check, then is elided and counted. | forbidden check disabled (`te` kept) | | `test_handle_trailer_elided_fields_count_against_the_bound` | 3 fields, 2 of them forbidden, bound 2 → `EnhanceYourCalm`. | forbidden check moved above the `field_count` increment (`Ok(())`) | | `test_handle_trailer_elides_a_forbidden_name_from_the_dynamic_table` | `authorization`, named by HPACK dynamic index 62 from an earlier block, is still elided and counted. | forbidden check disabled (`authorization` kept) | | `test_handle_trailer_keeps_forbidden_names_in_responses` | Response trailers are unchanged. | (guards the requests-only scope) | **E2E tests, in `e2e/src/tests/h2_security_header_injection.rs`:** - `test_h2_trailer_forbidden_fields_dropped_h1_backend` reads 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 missing `0\r\n` last-chunk line described below, which is being fixed separately. - `test_h2_trailer_forbidden_fields_dropped_h2_backend` reads the trailers the H2 backend decoded. RED: `grpc-status`, `content-length`, `cache-control`, `if-match`, `authorization`, `cookie` and `content-type` all arrived. `host` was already removed by `H2BlockConverter`. The `H2Backend` mock gains an opt-in `H2Backend::start_recording_trailers`. `H2Backend::start` is 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**: ``` 5\r\nhello\r\ngrpc-status: 0\r\n\r\n ``` This already happens on `origin/main` and does not depend on this change. The cause is that `handle_trailer` closes the block with `end_body: false`, so the H1 serializer never writes `0\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`:** - the trailer prose, with the same-field-set wording; - the `http.trailer.forbidden_field_elided` row; - a new `h2.trailer.forbidden_field_elided` row, which documents the `te: trailers` exception; - a corrected `h2.trailers_dropped_content_length` row. The counter fires when an accepted trailer block arrives on a message framed by `Content-Length` (`BodySize::Length`), whatever fields the block holds. It does not count a `content-length` trailer field, which is elided and counted in `h2.trailer.forbidden_field_elided`. - **`lib/src/metrics/names.rs` and `editor.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 in `pkawa.rs` (+3) and `h2.rs` (+5). ## Validation Run on the final head `dcca0e58`: | Command | Exit | |---|---| | `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 --lib -- pkawa::tests::test_handle_trailer` (7 passed) | 0 | | `cargo test -p sozu-e2e --locked -- trailer h2_security_header_injection` (27 passed) | 0 | | `check_doc_citations.py --self-test` / `--root .` / `--root . --base origin/main` / `--audit --root .` | 0 / 0 / 0 / 0 | Run on the first revision, before these review fixes: | Command | Exit | |---|---| | `cargo test -p sozu-lib --all-features --locked` (1449 unit tests, splice test included) | 0 | | `cargo test -p sozu-e2e --locked -- trailer h2_security_header_injection x_real_ip protocol_pair_matrix` (60 passed) | 0 | | `cd fuzz && cargo +nightly fuzz build` | 0 | Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 30, 2026
) Closes #1716 ## Problem The H1 frontend can reject a request after its stream is linked to a backend. A chunked request whose trailer section goes over `h2_max_header_fields` (#1707) is one case, when the trailer section arrives after the head and body were already forwarded. In that case, the `Position::Server` arms of the parse-error branch in `ConnectionH1::readable` (`lib/src/protocol/mux/h1.rs`) answer the client: - 400 when the response has not started; - a cut response when it has. Neither arm ends the backend stream. `set_default_answer` and `forcefully_terminate_answer` (`lib/src/protocol/mux/answers.rs`) only mark the stream `Unlinked`. As a result: - The backend `ConnectionH1` keeps the request, cut before its trailer section, and stays `Connected`. - It stays attached to the stream slot that the next keep-alive request reuses. - `backend_streams` still lists it. In debug builds, which include e2e, this panics `Mux::ready_inner`: `backend_streams index key count mismatch: expected={}, actual={Token(5): [0]}`. In release builds, the backend connection stays open until the client session closes. #1707's split test did not catch this. Its request and the builtin 400 both carry `Connection: close`, so the session closed in the same pass. The bug shows once a listener 400 answer omits `Connection: close`, which `set_default_answer` explicitly supports. ## Fix Both `Position::Server` arms now end the backend stream first, with `endpoint.end_stream(token, ...)`, as the `Position::Client` arm and the timeout paths in `mod.rs` already do. `unlink_stream` removes the stream from the reverse index. - **H1 backend:** the connection force-disconnects. It is never returned to the keep-alive pool, even when its response was already complete: - the 400 arm clears the response first (the 400 replaces it anyway); - the cut-response arm clears `keep_alive_backend` (that arm closes the client connection regardless). - **H2 backend:** only that stream ends. `ConnectionH2::end_stream` sends `RST_STREAM(CANCEL)` once the stream's request reached the wire, and the shared connection stays open for its other streams. What the client sees does not change. The client connection is at most as persistent as the 400 answer's `Connection` header: `writable` keeps it only when both `keep_alive_frontend` and `keep_alive_backend` hold, so a client `Connection: close`, or a backend response that closes, still closes it. This covers every frontend parse error that lands after linking, not only the trailer bound. I checked the other `set_default_answer` sites: - the pipelined parse in `writable` and the malformed-request check in `readable` run before linking; - the routing errors run in `Link`; - the timeout paths already call `unlink_stream`. Docs: `lib/src/protocol/kawa_h1/LIFECYCLE.md` §2.3 and `CHANGELOG.md` (Unreleased, Fixed). ## Test `test_h1_trailer_field_limit_exceeded_split_closes_backend` (`e2e/src/tests/h1_security_tests.rs`) uses a listener whose 400 answer keeps the client connection alive. It sends a keep-alive chunked request in two segments: 1. the head and body, up to `0\r\n`, which the backend receives; 2. 129 trailer fields. It then checks that: - the client gets 400; - the backend connection closes while the client connection stays open; - a follow-up request on the same client connection gets a 200 through a fresh backend connection. Red, with the fix reverted: ``` thread '<unnamed>' panicked at lib/src/protocol/mux/mod.rs:3322:13: assertion `left == right` failed: backend_streams index key count mismatch: expected={}, actual={Token(5): [0]} TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 295 bytes ending "...\r\n\r\n5\r\nHello\r\n0\r\n", client got Some("HTTP/1.1 400 Bad Request"), backend_closed=false, follow-up got "" test result: FAILED. 0 passed; 1 failed ``` Green: ``` TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 295 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 "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nSozu-Id: ...\r\n\r\npong" test result: ok. 1 passed; 0 failed ``` Unit test `a_request_error_after_a_linked_response_started_ends_the_backend_stream` (`lib/src/protocol/mux/h1.rs`) covers the cut-response arm. It links a pooled mock H1 backend, reads its complete response, marks it started on the client's wire, then sends a malformed chunk. It asserts: - the session closes; - the stream leaves `backend_streams`; - the backend no longer carries the stream and is `Disconnecting`, not pooled. It was seen red under two mutations of that arm: - whole block removed: `the rejected stream must leave the backend reverse index`; - `keep_alive_backend = false` removed: `a backend holding part of a rejected request must be disconnected, not pooled`. It is green with the arm restored. The 400 arm's response clear (before `end_stream`) still has no dedicated test. It needs the backend's full response and the trailer overflow to land in the same pass, before any response byte is written. ## Validation Each check was judged by its own exit code: | 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 --no-deps --all-features --locked` | 0 | | | `cargo test -p sozu-lib --locked` | 0 | 1424 unit tests passed after rebasing on `ac2789f3`; integration tests passed | | `cargo test -p sozu-e2e --locked h1_security_tests`, 3 runs | 0, 0, 0 | 39 passed each time | | `cargo test -p sozu-e2e --locked` (full suite) | 0 | 540 passed | | `cd fuzz && cargo +nightly fuzz build` | 0 | | | `check_doc_citations.py --self-test` | 0 | | | `check_doc_citations.py --root .` | 0 | | | `check_doc_citations.py --root . --base origin/main` | 0 | | | `check_doc_citations.py --audit --root .` | 0 | findings only in untouched `proxy_protocol/LIFECYCLE.md` | `cargo test -p sozu-lib` runs without the `splice` feature. `splice_readable_eof_drains_kernel_pipe_bytes_before_closing` (`lib/src/protocol/pipe.rs`) is gated on `#[cfg(all(target_os = "linux", feature = "splice"))]`, so it was not compiled and never ran in any of the runs above. An earlier version of this description said it passed; that was wrong. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 1, 2026
…1767) Follow-up to #1707. `a_chunked_request_trailer_section_loses_its_forbidden_fields` rejected any serialized request containing the bare substring `6666`. The random `traceparent` ids synthesised under the `opentelemetry` feature, and the random request ids, can carry it, so the test failed about once in 1,500 runs (seen in the `fips` CI cell). The assertion now rejects the case-insensitive `content-length: 6666` field line, alongside the existing `6.6.6.6` check. Test only; CHANGELOG updated. ## Test - 5000 runs with `opentelemetry` enabled: 0 failures (unfixed test: 3–4 failures per 5000, each with `6666` in the trace or span id). - With the filter changed to forward the `Content-Length` trailer, the anchored assertion fails. - `cargo +nightly fmt --check`, `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings`, `cargo doc --all-features --no-deps` with `-D warnings`, `cargo test -p sozu-lib --all-features --lib`, `cargo +nightly fuzz build`, and the four `check_doc_citations.py` modes pass. 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 #1701
Problem
After #1695 the H1 frontend still forwarded every chunked request trailer field other than the seven spoof vectors, including the framing, routing and authentication fields RFC 9110 §6.5.1 keeps out of trailers (
Content-Length,Transfer-Encoding,Host,Authorization, …). A backend that merges trailers into its header view could act on fields the client could not set in the header section. Nothing but the buffer bounded the number of trailer fields, while H2 caps a trailer block ath2_max_header_fields.Fix
The trailer filter becomes
HttpContext::filter_request_trailers(lib/src/protocol/kawa_h1/editor.rs), still called after both frontend parse sites inlib/src/protocol/mux/h1.rsand still walking only the blocks the last parse appended.TRAILER_FORBIDDEN_FIELDStakes the 25 names RFC 7230 §4.1.2 gave for each category on the request side: framing (Content-Length,Transfer-Encoding), routing (Host), the controls and conditionals of RFC 7231 §5.1–§5.2 (Cache-Control,Expect,Max-Forwards,Pragma,Range,TE,If-*), authentication (Authorization,Proxy-Authorization,Cookie), content processing (Content-Encoding,Content-Type,Content-Range,Trailer), plus the connection-specificConnection,Keep-Alive,Proxy-Connection,Upgrade(RFC 9110 §7.6.1).Accept-*and response-control fields are left out: no spec sentence names them for request trailers.http.trailer.forbidden_field_elided.h2_max_header_fields(default 128), copied intoHttpContext::max_trailer_fieldsbyContext::create_stream.pkawa::handle_trailercounts an H2 trailer block the same way. The running count lives inHttpContext::trailer_fields, because earlier fields may already be forwarded, andresetzeroes it per pipelined request. The field over the bound marks the kawa in error, so the existing parse-error branch answers 400, or cuts a response the backend already started. That field is never forwarded. H2 refuses the stream withENHANCE_YOUR_CALM. The H1 analogue is a refusal, and 400 is used because no 431 template exists and every other H1 framing violation gets 400. New counterhttp.trailer.field_limit_exceeded.Trailer:header not pruned. RFC 9110 §6.6.2 makes it a hint ("there is no guarantee that a sender of Trailer will always follow through"; it can show what an intermediary discarded). The header block is usually forwarded before the trailers are read.Tests (red, then green)
editor.rs):a_chunked_request_trailer_section_loses_its_forbidden_fields,trailer_forbidden_fields_are_matched_without_case_and_disjoint_from_spoof_vectors,a_trailer_section_over_the_field_bound_is_an_error,the_trailer_field_bound_spans_reads_and_resets_per_request. Each went red with its mechanism reverted (forbidden branch removed; bound check removed; per-walk count instead of the running count).h1_security_tests.rs):test_h1_trailer_forbidden_fields_dropped(whole,_split,_split_inside_trailers),test_h1_pipelined_trailer_forbidden_fields_dropped,test_h1_trailer_field_limit_exceeded_rejected(+_split) andtest_h1_trailer_field_limit_at_bound_forwarded. With the fix disabled, 6 of the 7 failed. The at-bound test (128 fields) stayed green: it is a non-regression guard. The existing spoof-vector e2e helpers now take the trailer section as a parameter.Validation (each by its own exit code, after rebasing on 080351b)
cargo build --all-features→ 0cargo +stable clippy --all-features --all-targets --locked -- -D warnings→ 0;cargo +1.93.1 clippy …→ 0cargo +nightly fmt --all -- --check→ 0RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked→ 0cargo test -p sozu-lib→ 1391 passed;-p sozu→ 0;-p sozu-command-lib→ 0cargo test -p sozu-e2e -- trailer→ 16 passed;-- h1_security→ 37 passed;-- mux_tests→ 38 passedcd fuzz && cargo +nightly fuzz build→ 0check_doc_citations.py: plain → 0,--base origin/main→ 0,--self-test→ 0, fixture tree → 1 with "do not resolve" (expected)Docs:
lib/src/protocol/kawa_h1/LIFECYCLE.md§2.3,doc/configure.md(trailer paragraph,h2_max_header_fieldsrows, two metric rows),lib/src/metrics/names.rs,CHANGELOG.md(Unreleased / Security). The unreleased #1689 entry now names the renamed method.Residual risks
pkawa::handle_trailerstill forwardsContent-Length,Host,Authorizationand the like in an H2 request trailer, including to an H1 backend; it rejects only connection-specific fields (RFC 9113 §8.2.2). This is documented in LIFECYCLE §2.3 and is a candidate follow-up issue.0\r\nand nothing more while the client gets 400. The backend-side EOF is not asserted.back.consumed) is documented but not covered by e2e for the trailer bound.h2_max_header_fieldsreaches H1 at the next connection, likeforwarded_headers.Context::create_streamnow buildsget_h2_flood_config()for every stream, H2 included. It does not allocate; the value could be cached onContextif preferred.