Skip to content

fix(mux-h2): drop forbidden fields from H2 request trailers and count them - #1720

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-trailer-forbidden-fields
Sep 30, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-trailer-forbidden-fields

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

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

… 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
FlorentinDUBOIS force-pushed the fix/h2-trailer-forbidden-fields branch from a00b896 to dcca0e5 Compare September 30, 2026 12:24
@FlorentinDUBOIS
FlorentinDUBOIS merged commit ccf0384 into main Sep 30, 2026
50 of 51 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h2-trailer-forbidden-fields branch September 30, 2026 14:07
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]>
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: request trailers still forward the fields TRAILER_FORBIDDEN_FIELDS drops on H1 (Content-Length, Host, Authorization, Cookie, ...)

1 participant