Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/brpc/policy/http2_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1405,6 +1411,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)
Expand Down Expand Up @@ -1498,6 +1506,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
Expand Down
5 changes: 5 additions & 0 deletions src/brpc/policy/http2_rpc_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 76 additions & 0 deletions test/brpc_http_rpc_protocol_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2675,6 +2675,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");
Comment on lines +2723 to +2724
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
Expand Down
Loading