Repository navigation
keep h2 request trailers out of the header in ConsumeHeaders - #3751
Merged
Merged
Conversation
On the server every header block of a stream was decoded into the same HttpHeader, so the trailer section of a request was merged into its header section and trailer pseudo-headers overwrote :path, :method, :authority and :scheme. RFC 9110 section 6.5.1 forbids combining trailer fields into the header section and RFC 9113 section 8.1 forbids pseudo-headers in trailers. A front-end only rewrites the header section, so fields such as X-Forwarded-For, Authorization or Cookie sent as request trailers reached the service as if the proxy had let them through. HTTP/1 chunked trailers are already dropped for this reason. Treat every header block after the first one of a server-side stream as the trailer section: keep decoding it so the HPACK dynamic table stays in sync, drop its fields, and reset the stream with PROTOCOL_ERROR on a pseudo-header. Response trailers are still merged on the client, which reads grpc-status and grpc-message from them.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The regression test does not cover HPACK dynamic-table synchronization because all trailer fields use the default 0x00 representation, leaving the indexed-trailer behavior unverified.
1 open issue
What changed in this PR
This pull request prevents HTTP/2 request trailers from being merged into server-side request headers, preserving HPACK synchronization while rejecting trailer pseudo-headers.
Changes:
- Tracks initial and subsequent server-side header blocks to distinguish request trailers.
- Drops regular trailer fields and resets streams containing trailer pseudo-headers.
- Adds regression coverage for header isolation and pseudo-header trailer handling.
| File | Description |
|---|---|
| test/brpc_http_rpc_protocol_unittest.cpp | Adds HTTP/2 regression tests covering request-trailer filtering and pseudo-header rejection. |
| src/brpc/policy/http2_rpc_protocol.h | Adds per-stream state for tracking the initial header section and subsequent trailers. |
| src/brpc/policy/http2_rpc_protocol.cpp | Classifies server-side trailer sections, decodes their fields without merging them into request headers, and rejects trailer pseudo-headers. |
Review effort: Lite
Comment on lines
+2723
to
+2724
| AppendLiteralHeader(&trailers, "x-forwarded-for", "6.6.6.6"); | ||
| AppendLiteralHeader(&trailers, "authorization", "Bearer smuggled"); |
Contributor
|
LGTM |
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.

What problem does this PR solve?
Issue Number: N/A
Problem Summary:
On the server,
H2StreamContext::ConsumeHeaders()decodes every header block of a stream into the sameHttpHeader. A request that sends HEADERS without END_STREAM, then DATA, then a second HEADERS block with END_STREAM carries a trailer section (RFC 9113 section 8.1), and those fields are merged into the request header like header fields: folded with,onto an existing name or added as a new one. Pseudo-header fields in the trailers go through the same switch as the header section, so a trailer:pathcallsURI::SetH2Path()again and replaces the path the request is routed by. RFC 9110 section 6.5.1 says a trailer field must not be combined into the header section, and RFC 9113 section 8.1 says trailers must not contain pseudo-header fields.#3521 dropped HTTP/1 chunked trailers for this reason: a front-end only filters or rewrites the header section, so a field sent as a trailer gets past it. The HTTP/2 path has the same gap. A front-end that passes request trailers through to an HTTP/2 upstream applies its rules for
X-Forwarded-For, the-http_header_of_user_ipheader,AuthorizationorCookieto the header section only. With-http_header_of_user_ip, if the proxy sets that header, a trailer copy turns it intoreal,forged,str2ip()fails and bRPC falls back tosocket->remote_side(), which is the proxy's own address. If the proxy only strips it, the trailer value becomes the user address handed toAuthenticator::VerifyCredential()andController::remote_side().The new test on master, with frames fed through
ParseH2Message()on a server-sideH2Context. The header section setsx-forwarded-for: 10.0.0.1and the trailers sendx-forwarded-for: 6.6.6.6andauthorization: Bearer smuggled:A second request with
:path: /statusin its trailers is handed on as a complete request whose path is/statusinstead of/EchoService/Echo.What is changed and the side effects?
Changed:
H2StreamContext::OnHeaders()marks every header block after the first one of a server-side stream as the trailer section.ConsumeHeaders()still HPACK-decodes every field so the dynamic table stays in sync with the peer, but drops the field instead of adding it to the request header. A pseudo-header there makes the request malformed and resets only that stream withPROTOCOL_ERROR, through the existing_rejected_errorpath.grpc-statusandgrpc-messageare read from them.HttpTest.h2_request_trailers_not_merged_into_header, which fails on master and passes with the fix.Side effects:
Performance effects: none, one flag test per decoded field.
Breaking backward compatibility: a server-side service no longer sees request trailer fields in
cntl->http_request(). bRPC has no trailer API, gRPC clients do not send request trailers, and the bRPC h2 client never sends them. A request whose trailers carry a pseudo-header is now reset instead of being processed.Check List:
Tests run (CMake Debug + ASan build on macOS, Apple clang):
brpc_http_rpc_protocol_unittest(70 tests, including the new one),brpc_grpc_protocol_unittest,brpc_h2_unsent_message_unittest,brpc_hpack_unittest,brpc_http_message_unittest,brpc_http_parser_unittestandbrpc_alpn_protocol_unittestall pass.brpc_builtin_service_unittestpasses exceptBuiltinServiceTest.dir, which reads/usr/include/errno.hand fails here because macOS has no such file; it does not touch HTTP/2.http2_rpc_protocol.cppcompiles cleanly with-Wall -W -Werror.