Skip to content

fix(http2): strip TE request header when any value is not trailers - #4194

Open
Dev-next-gen wants to merge 2 commits into
hyperium:masterfrom
Dev-next-gen:fix/h2-te-repeated-values
Open

Dev-next-gen wants to merge 2 commits into
hyperium:masterfrom
Dev-next-gen:fix/h2-te-repeated-values

Conversation

@Dev-next-gen

Copy link
Copy Markdown
Contributor

After #4152 made the HTTP/1 side look at every TE line and every token, I checked the HTTP/2 path and found that strip_connection_headers still only reads the first TE value before sending a request. With TE: trailers followed by a second line like TE: gzip, the first value passes, nothing gets stripped, and h2's own send check also only looks at the first value, so both lines go out. The receiving side validates each field on its own, so the peer resets the stream with PROTOCOL_ERROR. Between two hyper peers, send_request returns Reset(StreamId(1), PROTOCOL_ERROR, Remote).

The fix checks every TE value, and if any of them is not trailers the header is removed, which is what already happens when a single TE value is not trailers. Requests that worked before (no TE, or one TE: trailers) behave the same. The only requests that change are the ones that were always reset.

I added an integration test with a hyper h2 client and server. It fails on master with the reset above and passes with the change. The integration, lib, client and server test suites pass locally with --features full, as do cargo fmt --check and cargo clippy --features full.

AI tools used

Only the first TE value was checked before sending an HTTP/2 request, so a
request with `TE: trailers` followed by another TE line kept both values
and the peer reset the stream with PROTOCOL_ERROR. Check every value, as
the HTTP/1 side already does since hyperium#4152.

@BTF-Kabir-2020 BTF-Kabir-2020 left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug is real — get only looks at the first field line, so TE: trailers followed by TE: gzip reaches the peer as an HTTP/2-invalid TE (RFC 9113 §8.2.2). Switching to get_all(...).iter().any(...) is the right fix.

Two non-blocking things:

  1. The comparison is still byte-wise against "trailers". TE is a list field (RFC 9110 §10.1.4) and trailers is a case-insensitive literal, so TE: Trailers and TE: trailers, trailers are both valid but currently stripped. Splitting each field line on commas, trimming OWS and comparing each member case-insensitively would cover both. We already do that on the HTTP/1 side in #4152.

  2. Can we add a positive test? Two TE: trailers field lines should survive — that's the exact case this PR adds, and the current test only covers the invalid one.

Otherwise looks good to me.

Cover the case the previous commit allows: two `TE: trailers` lines must
both reach the peer, not be stripped.
@Dev-next-gen

Copy link
Copy Markdown
Contributor Author

The positive case was missing, you're right. de8bd47 moves the h2 client/server setup into a small helper and adds h2_keeps_te_header_with_repeated_trailers, which sends two TE: trailers lines and checks the server receives both (x-te-count: 2). The integration suite passes with it (16 passed), and cargo fmt --check is clean.

On the first point, I tried your change before answering, and I don't think hyper can keep those values. h2 compares TE byte-for-byte against trailers itself, on send (check_headers in src/proto/streams/send.rs, first value only) and on receive (src/frame/headers.rs, every field), and that's still the case in v0.4.20. With the comma-split, case-insensitive check swapped into strip_connection_headers, TE: Trailers and TE: trailers, trailers are no longer stripped, and send_request fails with hyper::Error(Http2, Error { kind: User(MalformedHeaders) }). Today those requests go through with TE removed. So I left the exact comparison as it is.

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.5.

@BTF-Kabir-2020

Copy link
Copy Markdown

Thanks for testing this against h2 — that makes sense. I hadn't accounted for its byte-wise validation on both the send and receive paths, so keeping the exact comparison is the right call with the current implementation.

And thanks for adding the positive test for repeated TE: trailers fields. That covers the case I wanted to make sure we didn't miss.

This branch has not been deployed

No deployments
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.

2 participants