Skip to content

reject h2 frames that interrupt a header block in Consume - #3772

Open
sahvx655-wq wants to merge 2 commits into
apache:masterfrom
sahvx655-wq:h2-header-block-continuity
Open

sahvx655-wq wants to merge 2 commits into
apache:masterfrom
sahvx655-wq:h2-header-block-continuity

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: N/A

Problem Summary:

H2Context::Consume() reads a frame head and hands the frame straight to its handler. Nothing records that a header block is open, so RFC 9113 section 6.10 is not enforced: a HEADERS or CONTINUATION frame without END_HEADERS may be followed by any frame at all, and a CONTINUATION frame is accepted even when no header block is pending.

On the server that is a header injection vector. H2StreamContext::OnContinuation() appends the fragment to _remaining_header_fragment and runs ConsumeHeaders() over it, and _in_trailer_section is only ever set by OnHeaders(), so a CONTINUATION arriving after END_HEADERS is decoded into the header section that was already complete. #3751 closed this for a second HEADERS block (the trailer section), but a bare CONTINUATION never goes through OnHeaders() and bypasses that check.

Frames fed through ParseH2Message() on a server-side H2Context on master, with HEADERS carrying END_HEADERS and x-forwarded-for: 10.0.0.1, then a bare CONTINUATION carrying authorization: Bearer smuggled and x-forwarded-for: 6.6.6.6, then DATA with END_STREAM:

is_ok=1 err=0
path=/EchoService/Echo authorization=[Bearer smuggled] x-forwarded-for=[10.0.0.1,6.6.6.6] body={"message":"hello"}

The request is handed to the service with both fields in cntl->http_request(). A front-end rewrites or strips X-Forwarded-For, the -http_header_of_user_ip header, Authorization and Cookie in the header section of the request it forwards, which is the reason #3521 and #3751 exist; a field smuggled in behind a stray CONTINUATION gets past it the same way, and the folded real,forged value makes str2ip() fail so the user address falls back to the proxy's own address.

The same gap lets the header blocks of two streams be interleaved: HEADERS on stream 1 without END_HEADERS, HEADERS on stream 3 without END_HEADERS, then their CONTINUATION frames in either order. A header block is one byte stream for the connection-wide HPACK decoder, so decoding two of them out of order desynchronises the dynamic table from the peer's and leaves _remaining_header_fragment holding a partial field that belongs to a different block.

What is changed and the side effects?

Changed:

  • H2Context tracks the stream whose header block is open in _expect_continuation_stream_id, set from the END_HEADERS flag of every HEADERS and CONTINUATION frame.
  • H2Context::CheckHeaderBlockContinuity() runs before the frame handler. While a block is open only a CONTINUATION of that same stream is accepted; a CONTINUATION outside a block is refused. Both are connection errors (H2_PROTOCOL_ERROR, so GOAWAY), as section 6.10 requires.
  • A header block legitimately split over HEADERS + CONTINUATION frames is unaffected, which is all PackH2Headers() ever emits.
  • HttpTest.http2_header_after_data built its first HEADERS frame without END_HEADERS while meaning to send a complete header section followed by a trailer section, so the sequence it fed the parser was invalid. Setting END_HEADERS there keeps it testing the header merging it is about.
  • Added HttpTest.h2_continuation_must_continue_a_header_block, covering the split block that must still work, the stray CONTINUATION and a DATA frame inside an open block.

Side effects:

  • Performance effects: none, one integer comparison per frame.

  • Breaking backward compatibility: a peer that sends a CONTINUATION outside a header block, or interleaves other frames inside one, now gets GOAWAY instead of having those fields merged into the request. Such a peer is already malformed under RFC 9113 section 6.10, and nghttp2, Go's net/http2 and the bRPC client itself all keep a header block contiguous.


Check List:

Tests run (CMake Release build on macOS arm64, Apple clang, protobuf 3.21.12): brpc_http_rpc_protocol_unittest (74 tests, including the new one), brpc_h2_unsent_message_unittest, brpc_grpc_protocol_unittest, brpc_http_message_unittest, brpc_http_parser_unittest and brpc_http_status_code_unittest all pass, and http2_rpc_protocol.cpp compiles without new warnings. The new test on master fails on the stray CONTINUATION:

Expected equality of these values:
  brpc::PARSE_ERROR_ABSOLUTELY_WRONG
    Which is: 5
  pr.error()
    Which is: 0

H2Context::Consume() dispatched every frame to its handler without
tracking whether a header block was open, so RFC 9113 section 6.10 was
not enforced. A CONTINUATION frame was accepted when no block was
pending, and frames of other types were accepted in the middle of one.

OnContinuation() appends the fragment to _remaining_header_fragment and
decodes it with ConsumeHeaders(), and _in_trailer_section is only set by
OnHeaders(), so a CONTINUATION sent after END_HEADERS added its fields
to the request header section of a server-side stream. That is the same
smuggling primitive apache#3751 closed for a second HEADERS block, reached
through a frame type that cannot legally be there. Interleaving also let
the header blocks of two streams be decoded out of order against the
connection-wide HPACK decoder.

Record the stream whose header block is open and refuse, as a connection
error, any frame other than a CONTINUATION of that stream while a block
is open, and any CONTINUATION when no block is open.

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 change still mishandles HTTP/2 header-block continuity by missing required GOAWAY handling for unknown frame types and retaining continuation state after a refused stream.

2 open issues
What changed in this PR

This PR enforces HTTP/2 header-block continuity in H2Context::Consume(), preventing stray or interleaved frames from being decoded into an already-completed or different header block.

Changes:

  • Tracks the stream ID of an open HEADERS/CONTINUATION block and rejects invalid frame sequences with H2_PROTOCOL_ERROR.
  • Adds coverage for valid split header blocks, stray CONTINUATION frames, and frames interleaved within an open block.
  • Corrects an existing test fixture to mark its complete initial header block with END_HEADERS.
File Description
test/​brpc_http_rpc_protocol_unittest.cpp Adds HTTP/2 header-block continuity regression tests and corrects an existing test sequence to use END_HEADERS.
src/​brpc/​policy/​http2_rpc_protocol.h Declares header-block continuity validation and stores the connection state for the expected continuation stream.
src/​brpc/​policy/​http2_rpc_protocol.cpp Implements HTTP/2 frame-order validation and tracks open header blocks during frame consumption.

Review effort: Lite

Comment thread src/brpc/policy/http2_rpc_protocol.cpp Resolved
Comment thread src/brpc/policy/http2_rpc_protocol.cpp Resolved
An unknown frame type has no handler, so it returned a plain parse error
from the handler == nullptr path before CheckHeaderBlockContinuity() was
reached. Inside an open header block that skipped the
GOAWAY(PROTOCOL_ERROR) that RFC 9113 section 6.10 requires. Run the
check right after the frame head is parsed so it covers frame types this
side does not implement as well, and assert the GOAWAY in the test.
@sahvx655-wq

Copy link
Copy Markdown
Contributor Author

Pushed 6186cca for the frame-handler ordering: the continuity check now runs right after the frame head is parsed, so an unknown frame type inside an open header block gets GOAWAY(PROTOCOL_ERROR) instead of falling through the handler == nullptr path with nothing written, and the test asserts that GOAWAY off the wire.

The other note I've answered in the thread rather than changed. Publishing the open block before the handler runs is intentional: section 6.10 still applies to a stream we refused, and for the one sequence that can trigger it (REFUSED_STREAM at max_concurrent_streams on a HEADERS without END_HEADERS) master already loses the connection, since that branch returns without forwarding past the fragment and the following CONTINUATION fails to find its stream.

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.

🟢 Approval recommended

No active review issues remain, and the changes address HTTP/2 header-block continuity with corresponding regression coverage for invalid frame sequencing.

No open issues 🎉

2 resolved since last review

Review effort: Lite

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