Bound the number of http headers and query parameters per message - #3524
Open
chenBright wants to merge 1 commit into
Open
Bound the number of http headers and query parameters per message#3524chenBright wants to merge 1 commit into
chenBright wants to merge 1 commit into
Conversation
chenBright
force-pushed
the
fix_http_header_query
branch
from
September 5, 2026 16:55
b358a46 to
3d0c834
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
It introduces an ABI-breaking public API change (URI::SetH2Path return type) that should be avoided or redesigned to preserve compatibility.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR adds HashDoS mitigations in bRPC’s HTTP parsing path by bounding the number of stored HTTP header entries and the number of query segments processed per message, and it introduces/extends unit tests to validate the new limits for both HTTP/1 and HTTP/2 parsing.
Changes:
- Add
http_max_header_countenforcement during HTTP/1 header parsing and HTTP/2 header consumption. - Add
http_max_query_countenforcement for URL query parsing in bothSetHttpURLandSetH2Path, with new tests for limit/disable behavior. - Add new unit tests covering over-limit cases for headers and queries, plus some minor test robustness adjustments.
File summaries
| File | Description |
|---|---|
| test/brpc_uri_unittest.cpp | Adds unit tests for the new query-parameter count limit and disabled-limit behavior. |
| test/brpc_http_rpc_protocol_unittest.cpp | Adds HTTP/2 tests for header-count and :path query-count limits; minor assert/test robustness tweaks. |
| test/brpc_http_message_unittest.cpp | Adds HTTP/1 parsing tests for header-count limits and folding/non-folding behavior. |
| src/brpc/uri.h | Changes URI::SetH2Path API to return int and document failure via status(). |
| src/brpc/uri.cpp | Introduces http_max_query_count flag and enforces query segment limits in URL parsing. |
| src/brpc/socket.cpp | Initializes _http_request_method in Socket::OnCreated. |
| src/brpc/policy/http2_rpc_protocol.cpp | Enforces header-count limit in HTTP/2 and handles SetH2Path failure. |
| src/brpc/details/http_message.cpp | Introduces http_max_header_count flag and enforces it during HTTP/1 header parsing. |
Review details
- Files reviewed: 8/8 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
100
to
+104
| void SetHostAndPort(const std::string& host_and_optional_port); | ||
| // Set path/query/fragment with the input in form of "path?query#fragment" | ||
| void SetH2Path(const char* h2_path); | ||
| void SetH2Path(const std::string& path) { SetH2Path(path.c_str()); } | ||
| // Returns 0 on success, -1 otherwise and status() is set. | ||
| int SetH2Path(const char* h2_path); | ||
| int SetH2Path(const std::string& path) { return SetH2Path(path.c_str()); } |
Comment on lines
+1947
to
+1955
| void AppendLiteralHeader(butil::IOBuf* out, const std::string& name, | ||
| const std::string& value) { | ||
| char prefix[] = { 0x00, (char)name.size() }; | ||
| out->append(prefix, sizeof(prefix)); | ||
| out->append(name); | ||
| char value_len = (char)value.size(); | ||
| out->append(&value_len, 1); | ||
| out->append(value); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
CaseIgnoredHasher(headers) andDefaultHasher(query map) are both plainresult = result * 101 + cwith no per-process seed, andFlatMappicks abucket by masking the low bits of the hash. An attacker only needs the low
log2(nbucket)bits to match, which is brute-forceable offline in seconds andreusable against every process, since nothing is seeded.
Both maps then degrade to a single chain:
on_header_value()callsGetOrAddHeader()for every field, so each headercosts a linear scan of the chain built so far. The cost is on the lookup side
and is paid unconditionally on the parse hot path.
URI::ParseQueries()inserts throughFlatMap::operator[], which walks thechain to dedup. The cost is on the insert side.
Either one turns a single request into O(n^2) work in the server's IO path.
Nothing bounded
n: h1 had no field-count limit at all, and h2'smax_header_list_sizebounds the decoded bytes of a header block, not thenumber of fields in it.
What is changed and the side effects?
Changed:
Tomcat's
maxHeaderCount, Envoy'smax_request_headers_count, PHP'smax_input_vars, Django'sDATA_UPLOAD_MAX_NUMBER_FIELDSandTomcat 11's
maxParameterCount, all of which were introduced as hashDoSmitigations.
Therefore, two flags are introduced to bound the number of http headers and
query parameters per message:
-http_max_header_count(default 100), checked inHttpMessage::on_header_value()for h1 and inH2StreamContext::ConsumeHeaders()afterAppendHeader()for h2.-http_max_query_count(default 1000), checked inURI::SetHttpURL()for h1and in
URI::SetH2Path()for h2. It counts&separators rather than mapentries: the splitter walks every segment even when the keys repeat, and it is
that walk the limit is meant to bound.
Setting flag to <= 0 lifts the limit.
Side effects:
Performance effects:
Breaking backward compatibility:
Check List: