Skip to content

keep h2 request trailers out of the header in ConsumeHeaders - #3751

Merged
chenBright merged 1 commit into
apache:masterfrom
sahvx655-wq:h2-request-trailers
Nov 17, 2026
Merged

chenBright merged 1 commit into
apache:masterfrom
sahvx655-wq:h2-request-trailers

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

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 same HttpHeader. 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 :path calls URI::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_ip header, Authorization or Cookie to the header section only. With -http_header_of_user_ip, if the proxy sets that header, a trailer copy turns it into real,forged, str2ip() fails and bRPC falls back to socket->remote_side(), which is the proxy's own address. If the proxy only strips it, the trailer value becomes the user address handed to Authenticator::VerifyCredential() and Controller::remote_side().

The new test on master, with frames fed through ParseH2Message() on a server-side H2Context. The header section sets x-forwarded-for: 10.0.0.1 and the trailers send x-forwarded-for: 6.6.6.6 and authorization: Bearer smuggled:

Expected equality of these values:
  "10.0.0.1"
  *xff
    Which is: "10.0.0.1,6.6.6.6"
Value of: msg->header().GetHeader("authorization") == nullptr
  Actual: false

A second request with :path: /status in its trailers is handed on as a complete request whose path is /status instead 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.
  • In 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 with PROTOCOL_ERROR, through the existing _rejected_error path.
  • The client side is untouched: response trailers keep being merged, since grpc-status and grpc-message are read from them.
  • Added 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_unittest and brpc_alpn_protocol_unittest all pass. brpc_builtin_service_unittest passes except BuiltinServiceTest.dir, which reads /usr/include/errno.h and fails here because macOS has no such file; it does not touch HTTP/2. http2_rpc_protocol.cpp compiles cleanly with -Wall -W -Werror.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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");
@wwbmmm

wwbmmm commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

LGTM

@chenBright chenBright left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@chenBright
chenBright merged commit 49bcd31 into apache:master Nov 17, 2026
29 checks passed
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.

4 participants