Skip to content

Fix SOFA PBRPC parser not limiting metadata size - #3449

Merged
chenBright merged 1 commit into
apache:masterfrom
wwbmmm:limit-sofa-pbrpc-metadata-size
Aug 15, 2026
Merged

Fix SOFA PBRPC parser not limiting metadata size#3449
chenBright merged 1 commit into
apache:masterfrom
wwbmmm:limit-sofa-pbrpc-metadata-size

Conversation

@wwbmmm

@wwbmmm wwbmmm commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: null

Problem Summary:

The SOFA PBRPC parser (ParseSofaMessage) enforces FLAGS_max_body_size
only on the frame's body_size field. The meta_size field, and
therefore the whole frame size, is not bounded. A peer can send a frame
with a large meta_size and body_size = 0, which passes the current
check, so the connection keeps reading and buffering the declared frame
until the metadata is finally parsed (and rejected). This makes the
configured per-message limit ineffective and can consume much more memory
than expected.

What is changed and the side effects?

Changed:

  • In ParseSofaMessage, reject frames whose meta_size exceeds
    FLAGS_max_body_size, in addition to the existing body_size check.
  • Add unit tests in test/brpc_sofa_pbrpc_protocol_unittest.cpp covering
    an oversized body and an oversized metadata block.

Side effects:

  • Performance effects: none.
  • Breaking backward compatibility: SOFA frames whose metadata exceeds
    max_body_size (previously buffered and then rejected at protobuf
    parsing time) are now rejected earlier with a "too big data" error and
    the connection is closed.

Check List:

Tests run:

  • test/brpc_sofa_pbrpc_protocol_unittest (all cases passed)

ParseSofaMessage only checked body_size against max_body_size, while
meta_size and the total frame size were left unbounded. A frame with a
large meta_size and zero body_size passed the body_size check and made
the connection keep buffering far beyond the configured limit before the
invalid metadata was rejected. Bound meta_size by max_body_size as well,
consistent with other protocols such as baidu_std and hulu_pbrpc.

Add unit tests covering oversized body and oversized metadata.

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 hardens the SOFA PBRPC protocol parser (src/brpc/policy/sofa_pbrpc_protocol.cpp) to better enforce FLAGS_max_body_size so that a peer can’t cause excessive buffering by declaring a large metadata block.

Changes:

  • Add an early rejection path in ParseSofaMessage when declared sizes exceed the configured limit.
  • Add unit tests that exercise oversized SOFA body and metadata headers.

Reviewed changes

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

File Description
src/brpc/policy/sofa_pbrpc_protocol.cpp Adds additional size validation during SOFA frame parsing to fail early on oversized frames.
test/brpc_sofa_pbrpc_protocol_unittest.cpp Adds helper to craft SOFA headers and new test cases for oversized body/meta.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/brpc/policy/sofa_pbrpc_protocol.cpp
Comment thread test/brpc_sofa_pbrpc_protocol_unittest.cpp

@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 552bfd6 into apache:master Aug 15, 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