Repository navigation
reject h2 frames that interrupt a header block in Consume - #3772
sahvx655-wq wants to merge 2 commits into
Conversation
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.
There was a problem hiding this comment.
🟡 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
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.
|
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. |


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_fragmentand runsConsumeHeaders()over it, and_in_trailer_sectionis only ever set byOnHeaders(), 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 throughOnHeaders()and bypasses that check.Frames fed through
ParseH2Message()on a server-sideH2Contexton master, with HEADERS carrying END_HEADERS andx-forwarded-for: 10.0.0.1, then a bare CONTINUATION carryingauthorization: Bearer smuggledandx-forwarded-for: 6.6.6.6, then DATA with END_STREAM:The request is handed to the service with both fields in
cntl->http_request(). A front-end rewrites or stripsX-Forwarded-For, the-http_header_of_user_ipheader,AuthorizationandCookiein 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 foldedreal,forgedvalue makesstr2ip()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_fragmentholding a partial field that belongs to a different block.What is changed and the side effects?
Changed:
H2Contexttracks 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.PackH2Headers()ever emits.HttpTest.http2_header_after_databuilt 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.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_unittestandbrpc_http_status_code_unittestall pass, andhttp2_rpc_protocol.cppcompiles without new warnings. The new test on master fails on the stray CONTINUATION: