This is an automated email from the ASF dual-hosted git repository.

chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 49bcd3184 Keep h2 request trailers out of the header in ConsumeHeaders 
(#3751)
49bcd3184 is described below

commit 49bcd3184192cc515374a5e59422c84d71d0cff0
Author: sahvx655-wq <[email protected]>
AuthorDate: Tue Nov 17 17:48:32 2026 +0530

    Keep h2 request trailers out of the header in ConsumeHeaders (#3751)
    
    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.
---
 src/brpc/policy/http2_rpc_protocol.cpp   | 24 ++++++++++
 src/brpc/policy/http2_rpc_protocol.h     |  5 +++
 test/brpc_http_rpc_protocol_unittest.cpp | 76 ++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+)

diff --git a/src/brpc/policy/http2_rpc_protocol.cpp 
b/src/brpc/policy/http2_rpc_protocol.cpp
index 2768cb37e..73e7cbc1a 100644
--- a/src/brpc/policy/http2_rpc_protocol.cpp
+++ b/src/brpc/policy/http2_rpc_protocol.cpp
@@ -775,6 +775,12 @@ H2ParseResult H2StreamContext::OnHeaders(
     // counter; it stays cumulative across the CONTINUATION frames that finish
     // this same block.
     _decoded_header_list_size = 0;
+    // Responses keep merging their trailers, the client reads grpc-status
+    // and grpc-message from there.
+    if (_conn_ctx->is_server_side()) {
+        _in_trailer_section = _header_section_received;
+        _header_section_received = true;
+    }
     butil::IOBufBytesIterator it2(it, frag_size);
     if (ConsumeHeaders(it2) < 0) {
         LOG(ERROR) << "Invalid header, frag_size=" << frag_size
@@ -1409,6 +1415,8 @@ H2StreamContext::H2StreamContext(bool 
read_body_progressively)
     , _remote_ended(false)
     , _response_committed(false)
     , _rst_received(false)
+    , _header_section_received(false)
+    , _in_trailer_section(false)
     , _rst_error(H2_NO_ERROR)
     , _remote_window_left(0)
     , _deferred_window_update(0)
@@ -1502,6 +1510,22 @@ int 
H2StreamContext::ConsumeHeaders(butil::IOBufBytesIterator& it) {
             // escalates to a connection error as it has to.
             continue;
         }
+        if (_in_trailer_section) {
+            // RFC 9110 section 6.5.1: a trailer field must not be combined
+            // into the header section. A front-end only rewrites the header
+            // section of a request, so a field merged from the trailers
+            // (Authorization, X-Forwarded-For, Cookie...) would be smuggled
+            // past it. Request trailers have no reader in bRPC, drop them as
+            // HttpMessage does for HTTP/1 chunked trailers.
+            if (pair.name[0] == ':') {
+                // RFC 9113 section 8.1: trailers must not include
+                // pseudo-header fields.
+                LOG(ERROR) << "Pseudo-header=`" << pair.name
+                           << "' in trailers, stream_id=" << _stream_id;
+                _rejected_error = H2_PROTOCOL_ERROR;
+            }
+            continue;
+        }
         const char* const name = pair.name.c_str();
         bool matched = false;
         if (name[0] == ':') { // reserved names
diff --git a/src/brpc/policy/http2_rpc_protocol.h 
b/src/brpc/policy/http2_rpc_protocol.h
index 6c0ed8fe4..7d5f577f0 100644
--- a/src/brpc/policy/http2_rpc_protocol.h
+++ b/src/brpc/policy/http2_rpc_protocol.h
@@ -301,6 +301,11 @@ friend class H2Context;
     bool _remote_ended;
     bool _response_committed;
     bool _rst_received;
+    // Server side only. The first header block of a request is its header
+    // section, any later one is the trailer section (RFC 9113 section 8.1),
+    // which ConsumeHeaders() keeps out of the request header.
+    bool _header_section_received;
+    bool _in_trailer_section;
     H2Error _rst_error;
     butil::atomic<int64_t> _remote_window_left;
     butil::atomic<int64_t> _deferred_window_update;
diff --git a/test/brpc_http_rpc_protocol_unittest.cpp 
b/test/brpc_http_rpc_protocol_unittest.cpp
index 96e2f73d7..2c1b93313 100644
--- a/test/brpc_http_rpc_protocol_unittest.cpp
+++ b/test/brpc_http_rpc_protocol_unittest.cpp
@@ -2834,6 +2834,82 @@ TEST_F(HttpTest, 
h2_oversized_single_headers_block_rejected) {
     delete sctx;
 }
 
+void AppendH2Frame(butil::IOBuf* out, brpc::policy::H2FrameType type,
+                   uint8_t flags, int stream_id, const butil::IOBuf& payload) {
+    char headbuf[brpc::policy::FRAME_HEAD_SIZE];
+    brpc::policy::SerializeFrameHead(headbuf, payload.size(), type, flags,
+                                     stream_id);
+    out->append(headbuf, sizeof(headbuf));
+    out->append(payload);
+}
+
+// HEADERS without END_STREAM, then DATA, then the trailer section in a
+// HEADERS frame with END_STREAM.
+void MakeH2RequestWithTrailers(butil::IOBuf* out, int stream_id,
+                               const butil::IOBuf& trailers) {
+    const uint8_t END_STREAM = 0x1;
+    const uint8_t END_HEADERS = 0x4;
+    butil::IOBuf headers;
+    AppendLiteralHeader(&headers, ":method", "POST");
+    AppendLiteralHeader(&headers, ":scheme", "http");
+    AppendLiteralHeader(&headers, ":path", "/EchoService/Echo");
+    AppendLiteralHeader(&headers, ":authority", "127.0.0.1");
+    AppendLiteralHeader(&headers, "content-type", "application/json");
+    AppendLiteralHeader(&headers, "x-forwarded-for", "10.0.0.1");
+    AppendH2Frame(out, brpc::policy::H2_FRAME_HEADERS, END_HEADERS,
+                  stream_id, headers);
+    butil::IOBuf body;
+    body.append("{\"message\":\"hello\"}");
+    AppendH2Frame(out, brpc::policy::H2_FRAME_DATA, 0, stream_id, body);
+    AppendH2Frame(out, brpc::policy::H2_FRAME_HEADERS,
+                  END_HEADERS | END_STREAM, stream_id, trailers);
+}
+
+// RFC 9110 section 6.5.1: a trailer field must not be merged into the header
+// section. A front-end only rewrites the header section of a request, so a
+// field taken from the trailers would be smuggled past it, the same way as
+// HTTP/1 chunked trailers before they were dropped. RFC 9113 section 8.1:
+// trailers must not carry pseudo-header fields.
+TEST_F(HttpTest, h2_request_trailers_not_merged_into_header) {
+    brpc::policy::H2Context* ctx =
+        new brpc::policy::H2Context(_socket.get(), &_server);
+    ASSERT_EQ(0, ctx->Init());
+    _socket->initialize_parsing_context(&ctx);
+    ASSERT_TRUE(ctx->is_server_side());
+    ctx->_conn_state = brpc::policy::H2_CONNECTION_READY;
+
+    butil::IOBuf trailers;
+    AppendLiteralHeader(&trailers, "x-forwarded-for", "6.6.6.6");
+    AppendLiteralHeader(&trailers, "authorization", "Bearer smuggled");
+    butil::IOBuf frames;
+    MakeH2RequestWithTrailers(&frames, 1, trailers);
+    brpc::ParseResult pr = brpc::policy::ParseH2Message(
+        &frames, _socket.get(), false, &_server);
+    ASSERT_TRUE(pr.is_ok());
+    ASSERT_TRUE(frames.empty());
+    brpc::policy::H2StreamContext* msg =
+        static_cast<brpc::policy::H2StreamContext*>(pr.message());
+    ASSERT_TRUE(msg != nullptr);
+    const std::string* xff = msg->header().GetHeader("x-forwarded-for");
+    ASSERT_TRUE(xff != nullptr);
+    EXPECT_EQ("10.0.0.1", *xff);
+    EXPECT_TRUE(msg->header().GetHeader("authorization") == nullptr);
+    EXPECT_EQ("/EchoService/Echo", msg->header().uri().path());
+    EXPECT_EQ("{\"message\":\"hello\"}", msg->body().to_string());
+    msg->Destroy();
+
+    // A pseudo-header in the trailers makes the request malformed. It must
+    // not be applied to the request either.
+    trailers.clear();
+    AppendLiteralHeader(&trailers, ":path", "/status");
+    frames.clear();
+    MakeH2RequestWithTrailers(&frames, 3, trailers);
+    pr = brpc::policy::ParseH2Message(&frames, _socket.get(), false, &_server);
+    ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error());
+    ASSERT_TRUE(frames.empty());
+    ASSERT_TRUE(ctx->FindStream(3) == nullptr);
+}
+
 TEST_F(HttpTest, http2_server_enforces_max_concurrent_streams) {
     // The server advertises SETTINGS_MAX_CONCURRENT_STREAMS but never
     // enforced it: every odd stream id was accepted, so a peer could pin an


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to