sahvx655-wq opened a new pull request, #3751:
URL: https://github.com/apache/brpc/pull/3751
### What problem does this PR solve?
Issue Number: N/A
Problem Summary:
On the server, `H2StreamContext::ConsumeHeaders()` decodes every header
block of a stream into the same `HttpHeader`. A request that sends HEADERS
without END_STREAM, then DATA, then a second HEADERS block with END_STREAM
carries a trailer section (RFC 9113 section 8.1), and those fields are merged
into the request header like header fields: folded with `,` onto an existing
name or added as a new one. Pseudo-header fields in the trailers go through the
same switch as the header section, so a trailer `:path` calls
`URI::SetH2Path()` again and replaces the path the request is routed by. RFC
9110 section 6.5.1 says a trailer field must not be combined into the header
section, and RFC 9113 section 8.1 says trailers must not contain pseudo-header
fields.
#3521 dropped HTTP/1 chunked trailers for this reason: a front-end only
filters or rewrites the header section, so a field sent as a trailer gets past
it. The HTTP/2 path has the same gap. A front-end that passes request trailers
through to an HTTP/2 upstream applies its rules for `X-Forwarded-For`, the
`-http_header_of_user_ip` header, `Authorization` or `Cookie` to the header
section only. With `-http_header_of_user_ip`, if the proxy sets that header, a
trailer copy turns it into `real,forged`, `str2ip()` fails and bRPC falls back
to `socket->remote_side()`, which is the proxy's own address. If the proxy only
strips it, the trailer value becomes the user address handed to
`Authenticator::VerifyCredential()` and `Controller::remote_side()`.
The new test on master, with frames fed through `ParseH2Message()` on a
server-side `H2Context`. The header section sets `x-forwarded-for: 10.0.0.1`
and the trailers send `x-forwarded-for: 6.6.6.6` and `authorization: Bearer
smuggled`:
```
Expected equality of these values:
"10.0.0.1"
*xff
Which is: "10.0.0.1,6.6.6.6"
Value of: msg->header().GetHeader("authorization") == nullptr
Actual: false
```
A second request with `:path: /status` in its trailers is handed on as a
complete request whose path is `/status` instead of `/EchoService/Echo`.
### What is changed and the side effects?
Changed:
- `H2StreamContext::OnHeaders()` marks every header block after the first
one of a server-side stream as the trailer section.
- In the trailer section, `ConsumeHeaders()` still HPACK-decodes every field
so the dynamic table stays in sync with the peer, but drops the field instead
of adding it to the request header. A pseudo-header there makes the request
malformed and resets only that stream with `PROTOCOL_ERROR`, through the
existing `_rejected_error` path.
- The client side is untouched: response trailers keep being merged, since
`grpc-status` and `grpc-message` are read from them.
- Added `HttpTest.h2_request_trailers_not_merged_into_header`, which fails
on master and passes with the fix.
Side effects:
- Performance effects: none, one flag test per decoded field.
- Breaking backward compatibility: a server-side service no longer sees
request trailer fields in `cntl->http_request()`. bRPC has no trailer API, gRPC
clients do not send request trailers, and the bRPC h2 client never sends them.
A request whose trailers carry a pseudo-header is now reset instead of being
processed.
---
### 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 Debug + ASan build on macOS, Apple clang):
`brpc_http_rpc_protocol_unittest` (70 tests, including the new one),
`brpc_grpc_protocol_unittest`, `brpc_h2_unsent_message_unittest`,
`brpc_hpack_unittest`, `brpc_http_message_unittest`,
`brpc_http_parser_unittest` and `brpc_alpn_protocol_unittest` all pass.
`brpc_builtin_service_unittest` passes except `BuiltinServiceTest.dir`, which
reads `/usr/include/errno.h` and fails here because macOS has no such file; it
does not touch HTTP/2. `http2_rpc_protocol.cpp` compiles cleanly with `-Wall -W
-Werror`.
--
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]