Repository navigation
fix(h1): drop spoofed forwarding headers from chunked request trailers - #1695
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 29, 2026 17:27
FlorentinDUBOIS
force-pushed
the
fix/h1-trailer-spoof-headers
branch
from
September 29, 2026 19:43
fb82cb3 to
d7da4e6
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 29, 2026
…1699) Closes #1692 ## What Follow-up to #1685. In the `both` (default) and `rfc7239` `forwarded_headers` modes, Sōzu removes a client `Forwarded` line that is not a well-formed RFC 7239 §4 list (`is_valid_forwarded`), including common non-conforming values such as `for=1.2.3.4:80` or `for=2001:db8::1`. The only trace was a `debug!` line. - New proxy counter `http.forwarded_malformed_elided` (`names::http::FORWARDED_MALFORMED_ELIDED`), incremented once per removed line in `HttpContext::on_request_headers`, on the elision branch only. No allocation added; a well-formed line pays nothing. `x_forwarded` and `none` pass the line through and count nothing. - Fixed the comment above the H2 trailer drop list in `pkawa::handle_trailer`: it claimed each managed header is "either synthesised or removed" on the HEADERS pass, which is untrue in `none` mode and for `Forwarded` in `x_forwarded` mode. Kept the edit to the inline comment only, to keep the conflict surface with #1695 small; the `///` docstring above `handle_trailer` and the `handle_trailer_drops_each_spoof_header_individually` docstring also omit `none` and are left for #1695 / a follow-up. - Docs: `doc/configure.md` (forwarded_headers section + "Other" metrics table), `doc/observability.md` (new subsection), `lib/src/protocol/kawa_h1/LIFECYCLE.md`, CHANGELOG (Unreleased / Added). ## Test `a_malformed_client_forwarded_elision_is_counted_once_per_line` (editor.rs): for `both`/`rfc7239`, a valid chain counts 0, one malformed line counts 1, two malformed lines around a valid one count 2; for `x_forwarded`/`none` nothing is counted. RED (test added, `incr!` absent): ``` test protocol::kawa_h1::editor::tests::a_malformed_client_forwarded_elision_is_counted_once_per_line ... FAILED thread '...a_malformed_client_forwarded_elision_is_counted_once_per_line' panicked at lib/src/protocol/kawa_h1/editor.rs:3785:13: assertion `left == right` failed: Both: one malformed line left: 0 right: 1 test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 1370 filtered out; finished in 0.01s ``` GREEN (with the `incr!`): ``` test protocol::kawa_h1::editor::tests::a_malformed_client_forwarded_elision_is_counted_once_per_line ... ok test protocol::kawa_h1::editor::tests::a_malformed_client_forwarded_is_elided_where_the_chain_is_extended ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1370 filtered out; finished in 0.06s ``` ## Validation (each by its own exit code, all 0) - `cargo build --all-features` - `cargo clippy --all-features --all-targets -- -D warnings` - `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` - `cargo +nightly fmt --all -- --check` - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked` - `cargo test -p sozu-lib` (lib: 1372 passed, incl. `lib/tests/metric_names.rs`), `cargo test -p sozu`, `cargo test -p sozu-command-lib` - `cargo test -p sozu-e2e forward` (23 passed) - `python3 .github/scripts/check_doc_citations.py` `--self-test`, `--root .`, `--base origin/main`, `--audit --root .` Signed-off-by: Florentin Dubois <[email protected]>
kawa's H1 parser pushes a chunked request's trailer fields after its last chunk without calling back, so HttpContext::on_request_headers never saw them and they reached the backend verbatim: a client could append X-Forwarded-For, Forwarded or X-Real-IP as trailer fields and hand a forged client address to a backend that merges trailers into its header view (RFC 9110 section 6.5.1, CWE-348). Add TRAILER_SPOOF_VECTOR_HEADERS in kawa_h1/editor.rs as the single list shared by both frontends (X-Real-IP, X-Forwarded-For, Forwarded, X-Request-Id, X-Forwarded-Proto, X-Forwarded-Port, X-Forwarded-Host). pkawa's handle_trailer reads it instead of its own name-by-name match, so H2 behaviour is unchanged and the two frontends cannot drift. Add elide_request_trailer_spoof_vectors, called after both frontend kawa::h1::parse sites of ConnectionH1 (readable, and the keep-alive pipelined re-parse) with the block count read before the parse. It walks back over the trailer fields that parse appended only, so a client trickling one trailer line per segment costs a linear walk in total, elides matching names in any case, keeps the chunk framing valid, and returns before reading a block for a request that is not chunked or not yet in its trailer section. Response trailers are unchanged. Each elided H1 field increments http.trailer.spoof_vector_elided. Closes #1689 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h1-trailer-spoof-headers
branch
from
September 29, 2026 21:04
d7da4e6 to
0f87745
Compare
This was referenced Sep 29, 2026
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 30, 2026
…d their count (#1707) 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 at `h2_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 in `lib/src/protocol/mux/h1.rs` and still walking only the blocks the last parse appended. - **Forbidden fields are elided.** RFC 9110 §6.5.1 names only categories; `TRAILER_FORBIDDEN_FIELDS` takes 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-specific `Connection`, `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. - **Elide vs 400.** RFC 7230 §4.1.2: "A recipient MUST ignore (or consider as an error) any fields that are forbidden to be sent in a trailer"; RFC 9112 §7.1.2: a recipient "MAY selectively retain or discard the received trailer fields". The backend usually already holds the head and body, so the request is forwarded without them. This matches how #1695 and the H2 path drop spoof vectors. New counter `http.trailer.forbidden_field_elided`. - **Field bound.** Every trailer field, elided or not, counts against the listener's `h2_max_header_fields` (default 128), copied into `HttpContext::max_trailer_fields` by `Context::create_stream`. `pkawa::handle_trailer` counts an H2 trailer block the same way. The running count lives in `HttpContext::trailer_fields`, because earlier fields may already be forwarded, and `reset` zeroes 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 with `ENHANCE_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 counter `http.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. - **Cost.** A request without a trailer section returns before reading a block and allocates nothing. ## Tests (red, then green) - Unit (`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). - e2e (`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`) and `test_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` → 0 - `cargo +stable clippy --all-features --all-targets --locked -- -D warnings` → 0; `cargo +1.93.1 clippy …` → 0 - `cargo +nightly fmt --all -- --check` → 0 - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked` → 0 - `cargo test -p sozu-lib` → 1391 passed; `-p sozu` → 0; `-p sozu-command-lib` → 0 - `cargo test -p sozu-e2e -- trailer` → 16 passed; `-- h1_security` → 37 passed; `-- mux_tests` → 38 passed - `cd fuzz && cargo +nightly fuzz build` → 0 - `check_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_fields` rows, two metric rows), `lib/src/metrics/names.rs`, `CHANGELOG.md` (Unreleased / Security). The unreleased #1689 entry now names the renamed method. ## Residual risks - **H2 trailer gap:** `pkawa::handle_trailer` still forwards `Content-Length`, `Host`, `Authorization` and 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. - Over-limit after the body was forwarded (split case): observed, but not asserted, that the backend receives head + body + `0\r\n` and nothing more while the client gets 400. The backend-side EOF is not asserted. - The cut path when the response already started (`back.consumed`) is documented but not covered by e2e for the trailer bound. - A hot patch of `h2_max_header_fields` reaches H1 at the next connection, like `forwarded_headers`. - `Context::create_stream` now builds `get_h2_flood_config()` for every stream, H2 included. It does not allocate; the value could be cached on `Context` if preferred. 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 #1689
Problem (reproduced)
kawa's H1 parser pushes a chunked request's trailer fields after its last chunk with no callback, so
HttpContext::on_request_headersnever sees them. Onmain(3ed6e8a6) the new e2e test shows the backend receiving them verbatim:A backend that merges trailers into its header view then sees a forged client address (RFC 9110 §6.5.1, CWE-348).
Fix
TRAILER_SPOOF_VECTOR_HEADERS(lib/src/protocol/kawa_h1/editor.rs) is now the single list for both frontends:x-real-ip,x-forwarded-for,forwarded,x-request-id,x-forwarded-proto,x-forwarded-port,x-forwarded-host.pkawa::handle_trailerreads it instead of the inline seven-namematches!that feat(listener): make forwarded headers configurable per listener #1685 (merged as5471bfba) added, so H1 and H2 cannot drift. H2 behaviour is unchanged. This branch is rebased onorigin/main(a6419607), which includes feat(listener): make forwarded headers configurable per listener #1685, fix(h1): keep the Connection options while shutting down #1696, fix(listener): resolve HSTS inheritance against each listener's own default #1697 and feat(http): count malformed client Forwarded lines removed by Sōzu #1699. Theeditor.rsconflict with fix(h1): keep the Connection options while shutting down #1696 (both appended tests at the end of the test module) keeps both sets of tests, and thepkawa.rscomment keeps feat(http): count malformed client Forwarded lines removed by Sōzu #1699's mode wording while naming the shared constant. Thepkawa.rsconflict was resolved in favour of the shared constant, keeping feat(listener): make forwarded headers configurable per listener #1685'sforwarded_headers-mode wording in the doc comments, the test rationale and thedoc/configure.mdparagraph (which now also names the H1 path).elide_request_trailer_spoof_vectors(same file) runs after both frontendkawa::h1::parsesites ofConnectionH1:readable, and the keep-alive branch ofwritablethat parses a pipelined request. Each call site readskawa.blocks.len()just before the parse and passes it asfirst_new_block. The walk goes back only over the blocks that parse appended (closingFlags+ trailer fields), stopping at theend_bodymarker or atfirst_new_block. It elides matching names in any case withPair::elide.prepareonly drains from the front between parses, so every older field was already examined by the call after the parse that queued it. A client that trickles one trailer line per segment while the backend isn't writable costs one block per line, not an O(n²) re-walk. This holds when earlier trailer fields were already drained to the backend....0\r\nGrpc-Status: 0\r\n\r\non the wire.kind/body_size == Chunked/parsing_phase ∈ {Trailers, Terminated}checks before reading any block. Requests without trailers pay three field compares.http.trailer.spoof_vector_elided(one increment per elided H1 field), documented indoc/configure.md.doc/configure.md(anti-spoof paragraph + metric row),lib/src/protocol/kawa_h1/LIFECYCLE.md§2.3,CHANGELOG.md(Unreleased / Security).Tests (red, then green)
New tests:
a_chunked_request_trailer_section_loses_its_spoof_vector_fields,a_trailer_section_parsed_after_the_last_chunk_was_forwarded_is_filtered,a_trailer_section_split_across_reads_is_filtered_in_each_part,a_trailer_walk_stops_at_the_blocks_queued_before_the_parse,trailer_elision_leaves_headers_and_responses_alone(editor unit tests). The e2e tests aretest_h1_trailer_spoof_headers_dropped,test_h1_trailer_spoof_headers_dropped_split(trailers in a separate write),test_h1_trailer_spoof_headers_dropped_split_inside_trailers(X-Forwarded-Forin one write, the rest in the next) andtest_h1_pipelined_trailer_spoof_headers_dropped.handle_trailer_drops_each_spoof_header_individuallynow iterates the shared list.RED, e2e on unfixed code:
RED, unit tests:
elide_request_trailer_spoof_vectorsstubbed toreturn 0, and pkawa restored to its old four-namematches!:RED, pipelined path: only the keep-alive call site in
mux/h1.rsdisabled:RED, review items after the rebase:
Walk ignoring
first_new_block(range_mut(0..)). The function's own post-conditiondebug_assert!("only blocks appended by the last parse are elided") fires before the test's assertion:Elision disabled (
return 0):GREEN:
Validation (each by its own exit code)
cargo build --all-features: 0cargo clippy --all-features --all-targets -- -D warnings: 0cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings(MSRV): 0cargo +nightly fmt --all -- --check: 0RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked: 0cargo test -p sozu-lib: 0 (1381 passed)cargo test -p sozu: 0cargo test -p sozu-command-lib: 0cargo test -p sozu-e2e -- trailer h1_security mux_tests forward connection: 0 (104 passed)check_doc_citations.py--self-test/--root ./--base origin/main/--audit --root .: all 0 (the audit's three advisory findings are in files this PR does not touch)Residual risks
Trailer:request header still lists the dropped names. It is informational only, and this PR leaves it as sent.pkawa::handle_trailer, not this walk. It uses the same list.