sahvx655-wq opened a new pull request, #3772:
URL: https://github.com/apache/brpc/pull/3772
### 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:
- Please make sure your changes are compilable.
- When providing us with a new feature, it is best to add related tests.
- Please follow [Contributor Covenant Code of
Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
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
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]