Skip to content

fix(buffa): bound recursive extension expansion depth in JSON and textproto - #849

Open
dev-burnern wants to merge 1 commit into
anthropics:mainfrom
dev-burnern:fix/bound-extension-expansion-depth
Open

dev-burnern wants to merge 1 commit into
anthropics:mainfrom
dev-burnern:fix/bound-extension-expansion-depth

Conversation

@dev-burnern

Copy link
Copy Markdown

Summary

Bound recursive message-typed extension expansion depth during JSON serialization and textproto encoding, matching the existing MAX_ANY_EXPANSION_DEPTH guard used for google.protobuf.Any.

Background

Message-typed protobuf extensions are stored in UnknownFields as opaque UnknownFieldData::LengthDelimited(Vec<u8>) during wire decoding (Message::decode), so the binary decoder's RECURSION_LIMIT does not bound deeply nested self-referential or mutually recursive message extensions. While google.protobuf.Any already guards against unbounded expansion at serialize time via enter_any_expansion() (JSON) and TextEncoder::any_depth (textproto), extension expansion had no equivalent depth check in either path.

(Submitted as a stability hardening PR per Anthropic security triage recommendation.)

Changes

  • JSON serialization (buffa/src/extension_registry.rs): Acquire crate::type_registry::enter_any_expansion() in serialize_extensions before invoking (entry.to_json)(...), returning a serde::ser::Error when MAX_ANY_EXPANSION_DEPTH is exceeded. Sharing the guard with Any also bounds alternating Any ↔ extension nesting.
  • Textproto encoding (buffa/src/text/encoder.rs): Share TextEncoder::any_depth in write_extension_fields to stop expanding past MAX_ANY_EXPANSION_DEPTH (keeping output valid textproto and preserving the Ok contract for String sinks). Also save/restore self.ext_name across recursive calls so nested extensions do not clobber an outer repeated extension's name.
  • Tests (buffa/src/extension_registry.rs, buffa/src/type_registry.rs):
    • serialize_extensions_caps_recursive_depth: verifies shallow recursive extensions succeed and MAX_ANY_EXPANSION_DEPTH + 5 levels fail cleanly with an error.
    • write_extension_fields_caps_recursive_depth: verifies MAX_ANY_EXPANSION_DEPTH + 5 levels terminate cleanly at MAX_ANY_EXPANSION_DEPTH nested blocks.

Verification

  • cargo test -p buffa --features json,text --lib (1,136 passed, 0 failed)
  • cargo clippy -p buffa --features json,text --all-targets -- -D warnings
  • cargo check -p buffa --no-default-features --features json,text
  • cargo fmt --check
  • cargo doc -p buffa --features json,text --no-deps

@github-actions

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

This branch has not been deployed

No deployments
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.

1 participant