Bound Any expansion depth in TextFormat::Printer - #29527
Open
unpredictable21 wants to merge 1 commit into
Open
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
Friendly ping for review. Patch summary:
Verification (local, on Linux x86_64, g++ 15.2.0):
Backward compatibility:
Related: |
TextFormat::Printer::PrintAny() recurses into nested google.protobuf.Any messages without a depth bound. A chain of Any messages whose type_url is type.googleapis.com/google.protobuf.Any drives PrintAny into unbounded recursion, which crashes the process with SIGSEGV on small stacks and produces an exponentially-growing output on larger ones. This change adds a configurable recursion budget (default 100). When the budget is exhausted, PrintAny returns false and the caller falls back to printing the Any as a regular field (showing type_url and value literally) instead of recursing further. New API on TextFormat::Printer: void SetAnyExpansionDepth(int depth); // default 100 int any_expansion_depth() const; Includes a regression test (text_format_any_expansion_test.cc) covering: - depth 1000 does not stack-overflow; output < 10 MB - brace count balances in fallback output - SetAnyExpansionDepth(0) disables expansion entirely - SetAnyExpansionDepth(5) produces smaller output than the default Bug class: CWE-674 (Uncontrolled Recursion), CWE-770 (Allocation of Resources Without Limits or Throttling).
unpredictable21
force-pushed
the
fix-text-format-any-recursion
branch
from
August 31, 2026 10:36
6f475b1 to
c428ec2
Compare
Author
|
Updated — squashed history and added the regression test. Changes in the latest push:
Run with: |
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.
Bound Any expansion depth in
TextFormat::Printer::PrintAnySummary
TextFormat::Printer::PrintAny()recurses into nestedgoogle.protobuf.Anymessages without a depth bound. A chain of Any messages whose
type_urlis
type.googleapis.com/google.protobuf.AnydrivesPrintAnyintounbounded recursion, which crashes the process with SIGSEGV on small stacks
and produces an exponentially-growing string on larger ones.
Reproduction (unpatched)
Build a chain of Any-of-Any and print it with
SetExpandAny(true):The amplification factor is exponential because each level of expansion
re-serializes the inner Any's wire form into the textual output.
Fix
Add a configurable recursion budget (default 100). When the budget is
exhausted,
PrintAnyreturnsfalseand the caller falls back to printingthe Any as a regular field (
type_url: "..."value: "...") instead ofrecursing further. This matches the existing fallback when
expand_any_isfalse.
New API on
TextFormat::Printer:Tests
The included regression test (
text_format_any_expansion_test.cc) covers:{=})SetAnyExpansionDepth(0)type_urlliteralSetAnyExpansionDepth(5)(depth=50)Output sizes after the fix:
These are all bounded by
O(depth)rather thanO(2^depth).Bug class
Related