Skip to content

Enforce body size limits in protocol parsers#3382

Merged
chenBright merged 2 commits into
apache:masterfrom
wwbmmm:enforce-body-size-limits
Jul 12, 2026
Merged

Enforce body size limits in protocol parsers#3382
chenBright merged 2 commits into
apache:masterfrom
wwbmmm:enforce-body-size-limits

Conversation

@wwbmmm

@wwbmmm wwbmmm commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: null

Problem Summary:

Some HTTP, memcache, and couchbase parsing paths did not consistently enforce
the global max_body_size setting. Non-progressive HTTP messages could buffer
more data than configured, while memcache and couchbase parsers could wait for
an oversized body declared in a response header.

What is changed and the side effects?

Changed:

  • Track the accumulated body size of non-progressive HTTP messages and reject
    data before appending beyond max_body_size.
  • Return HTTP 413 for oversized HTTP/1 requests and close the connection after
    writing the response.
  • Reset only the oversized HTTP/2 stream with ENHANCE_YOUR_CALM.
  • Reject oversized memcache and couchbase responses immediately after parsing
    their fixed-size headers.
  • Add regression tests for fixed-length HTTP, chunked HTTP, memcache, and
    couchbase messages.

Side effects:

  • Performance effects: Each non-progressive HTTP body block adds one
    overflow-safe size comparison and addition. Memcache and couchbase add one
    comparison per response. No additional scan, allocation, or copy is added.
  • Breaking backward compatibility: Messages larger than max_body_size are
    now rejected as documented. Progressive HTTP reading remains unchanged so
    existing bounded-memory streaming transfers can exceed this setting.

Check List:

  • The changes are compilable.
  • Regression tests are included.
  • The affected test suites pass.

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.

Pull request overview

This PR enforces the global max_body_size limit across several protocol parsers (HTTP/1, HTTP/2, memcache, couchbase) to prevent buffering or waiting on oversized message bodies, and adds regression coverage for the new enforcement paths.

Changes:

  • Enforce max_body_size during non-progressive HTTP body parsing; return HTTP 413 for oversized HTTP/1 requests and reset oversized HTTP/2 streams with ENHANCE_YOUR_CALM.
  • Reject oversized memcache and couchbase responses immediately after parsing their fixed-size headers.
  • Add new unit tests covering fixed-length/chunked HTTP and oversized memcache/couchbase responses.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/brpc_memcache_unittest.cpp Adds regression test ensuring oversized memcache responses are rejected before buffering the body.
test/brpc_http_rpc_protocol_unittest.cpp Adds MaxBodySizeGuard and HTTP/1 tests verifying 413 behavior for oversized fixed-length and chunked bodies.
test/brpc_couchbase_unittest.cpp Adds regression test ensuring oversized couchbase responses are rejected before buffering the body.
src/brpc/policy/memcache_binary_protocol.cpp Enforces FLAGS_max_body_size against memcache total_body_length immediately after header parse.
src/brpc/policy/http2_rpc_protocol.cpp Returns H2_ENHANCE_YOUR_CALM for oversized HTTP/2 stream bodies instead of a generic protocol error.
src/brpc/policy/http_rpc_protocol.cpp Detects HttpMessage oversize condition and writes HTTP 413 (server-side) or returns TOO_BIG_DATA (client-side).
src/brpc/policy/couchbase_protocol.cpp Enforces FLAGS_max_body_size against couchbase total_body_length immediately after header parse.
src/brpc/details/http_message.h Adds body_too_large() and tracking fields to support non-progressive body size enforcement.
src/brpc/details/http_message.cpp Tracks accumulated non-progressive body size and fails early when appending would exceed max_body_size.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/brpc_memcache_unittest.cpp Outdated
Comment thread test/brpc_couchbase_unittest.cpp Outdated

@chenBright chenBright 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.

LGTM

@chenBright chenBright merged commit f03649d into apache:master Jul 12, 2026
15 checks passed
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.

3 participants