Skip to content

fix(h1): drop spoofed forwarding headers from chunked request trailers - #1695

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-trailer-spoof-headers
Sep 29, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-trailer-spoof-headers

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

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_headers never sees them. On main (3ed6e8a6) the new e2e test shows the backend receiving them verbatim:

TRAILER-SPOOF: backend received "POST /api HTTP/1.1\r\n...X-Forwarded-For: 127.0.0.1\r\n...\r\n\r\n5\r\nHello\r\n0\r\nX-Forwarded-For: 6.6.6.6\r\nForwarded: for=6.6.6.6\r\nX-Real-IP: 6.6.6.6\r\nX-Request-Id: 6.6.6.6\r\nX-Forwarded-Proto: 6.6.6.6\r\nX-Forwarded-Port: 6.6.6.6\r\nX-Forwarded-Host: 6.6.6.6\r\nGrpc-Status: 0\r\n\r\n"

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_trailer reads it instead of the inline seven-name matches! that feat(listener): make forwarded headers configurable per listener #1685 (merged as 5471bfba) added, so H1 and H2 cannot drift. H2 behaviour is unchanged. This branch is rebased on origin/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. The editor.rs conflict 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 the pkawa.rs comment keeps feat(http): count malformed client Forwarded lines removed by Sōzu #1699's mode wording while naming the shared constant. The pkawa.rs conflict was resolved in favour of the shared constant, keeping feat(listener): make forwarded headers configurable per listener #1685's forwarded_headers-mode wording in the doc comments, the test rationale and the doc/configure.md paragraph (which now also names the H1 path).
  • elide_request_trailer_spoof_vectors (same file) runs after both frontend kawa::h1::parse sites of ConnectionH1: readable, and the keep-alive branch of writable that parses a pipelined request. Each call site reads kawa.blocks.len() just before the parse and passes it as first_new_block. The walk goes back only over the blocks that parse appended (closing Flags + trailer fields), stopping at the end_body marker or at first_new_block. It elides matching names in any case with Pair::elide.
  • Linear cost (review item): a parse only appends blocks and prepare only 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.
  • Framing: kawa's H1 and H2 converters skip elided pairs. The last chunk and the closing empty line are still written, so the framing stays valid even when every field is dropped. The e2e test asserts ...0\r\nGrpc-Status: 0\r\n\r\n on the wire.
  • Cost: the function returns on the kind / body_size == Chunked / parsing_phase ∈ {Trailers, Terminated} checks before reading any block. Requests without trailers pay three field compares.
  • Responses are not filtered. A response trailer goes to the client, which does not take client attribution from it, so the spoofing risk this issue describes does not apply.
  • New counter http.trailer.spoof_vector_elided (one increment per elided H1 field), documented in doc/configure.md.
  • Docs: 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 are test_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-For in one write, the rest in the next) and test_h1_pipelined_trailer_spoof_headers_dropped. handle_trailer_drops_each_spoof_header_individually now iterates the shared list.

RED, e2e on unfixed code:

TRAILER-SPOOF: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped ... FAILED
TRAILER-SPOOF-SPLIT: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split ... FAILED
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 504 filtered out

RED, unit tests: elide_request_trailer_spoof_vectors stubbed to return 0, and pkawa restored to its old four-name matches!:

test protocol::mux::pkawa::tests::handle_trailer_drops_each_spoof_header_individually ... FAILED
test protocol::kawa_h1::editor::tests::a_trailer_section_parsed_after_the_last_chunk_was_forwarded_is_filtered ... FAILED
test protocol::kawa_h1::editor::tests::a_chunked_request_trailer_section_loses_its_spoof_vector_fields ... FAILED
test protocol::kawa_h1::editor::tests::trailer_elision_leaves_headers_and_responses_alone ... ok
trailer with only x-forwarded-proto should leave no surviving block
test result: FAILED. 1 passed; 3 failed; 0 ignored; 0 measured; 1348 filtered out

RED, pipelined path: only the keep-alive call site in mux/h1.rs disabled:

TRAILER-SPOOF-PIPELINED: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_pipelined_trailer_spoof_headers_dropped ... FAILED

RED, review items after the rebase:

Walk ignoring first_new_block (range_mut(0..)). The function's own post-condition debug_assert! ("only blocks appended by the last parse are elided") fires before the test's assertion:

test protocol::kawa_h1::editor::tests::a_trailer_section_split_across_reads_is_filtered_in_each_part ... ok
test protocol::kawa_h1::editor::tests::a_trailer_walk_stops_at_the_blocks_queued_before_the_parse ... FAILED
test result: FAILED. 1 passed; 1 failed

Elision disabled (return 0):

test protocol::kawa_h1::editor::tests::a_trailer_section_split_across_reads_is_filtered_in_each_part ... FAILED
assertion `left == right` failed: the first trailer part is filtered on its own
  left: 0
 right: 1
TRAILER-SPOOF-SPLIT-INSIDE: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split_inside_trailers ... FAILED

GREEN:

test protocol::mux::pkawa::tests::handle_trailer_drops_each_spoof_header_individually ... ok
test protocol::kawa_h1::editor::tests::a_chunked_request_trailer_section_loses_its_spoof_vector_fields ... ok
test protocol::kawa_h1::editor::tests::a_trailer_section_parsed_after_the_last_chunk_was_forwarded_is_filtered ... ok
test protocol::kawa_h1::editor::tests::trailer_elision_leaves_headers_and_responses_alone ... ok
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped ... ok
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split ... ok
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split_inside_trailers ... ok
test protocol::kawa_h1::editor::tests::a_trailer_section_split_across_reads_is_filtered_in_each_part ... ok
test protocol::kawa_h1::editor::tests::a_trailer_walk_stops_at_the_blocks_queued_before_the_parse ... ok
test tests::h1_security_tests::test_h1_pipelined_trailer_spoof_headers_dropped ... ok

Validation (each by its own exit code)

  • cargo build --all-features: 0
  • cargo clippy --all-features --all-targets -- -D warnings: 0
  • cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings (MSRV): 0
  • cargo +nightly fmt --all -- --check: 0
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked: 0
  • cargo test -p sozu-lib: 0 (1381 passed)
  • cargo test -p sozu: 0
  • cargo test -p sozu-command-lib: 0
  • cargo 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

  • The advisory Trailer: request header still lists the dropped names. It is informational only, and this PR leaves it as sent.
  • An H2-frontend request forwarded to an H1 backend goes through pkawa::handle_trailer, not this walk. It uses the same list.
  • The bounded walk assumes kawa 0.7.2's invariants: parsing only appends blocks, and draining happens only from the front between parses. A debug post-condition guards this.

@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/h1-trailer-spoof-headers branch from fb82cb3 to d7da4e6 Compare September 29, 2026 19:43
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
FlorentinDUBOIS force-pushed the fix/h1-trailer-spoof-headers branch from d7da4e6 to 0f87745 Compare September 29, 2026 21:04
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 67e5bf2 into main Sep 29, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h1-trailer-spoof-headers branch September 29, 2026 22:09
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]>
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: chunked trailers can carry spoofed forwarding headers

1 participant