Skip to content

max_recursion_depth` has no effect on Struct/Value/ListValue JSON parsing #26736

@JohannesLks

Description

@JohannesLks

What version of protobuf and what language are you using?*

protobuf 7.34.1 (Python runtime), also reproduced on main

Language: Python

What supported operating system are you using?

Linux (Ubuntu 24.04)

What supported runtime / compiler version are you using?

Python 3.12

What did you do?

json_format.Parse() with max_recursion_depth set does not limit recursion for google.protobuf.Struct, Value, or ListValue messages.

ConvertMessage() tracks depth correctly for regular messages, but Struct/Value/ListValue dispatch through _WKTJSONMETHODS into _ConvertValueMessage / _ConvertStructMessage / _ConvertListOrTupleValueMessage. These three methods call each other directly without re-entering ConvertMessage(), so recursion_depth is never incremented past the initial entry.

from google.protobuf import json_format, struct_pb2
import json

nested = {"v": "leaf"}
for _ in range(200):
    nested = {"k": nested}

msg = struct_pb2.Struct()
json_format.Parse(json.dumps(nested), msg, max_recursion_depth=5)
# Expected: ParseError ("Message too deep")
# Actual: parses successfully, depth limit ignored

This is the same pattern that was fixed for Any messages (where nested Any parsing was routed back through ConvertMessage()). The Struct/Value/ListValue cycle was not updated at that time.

What did you expect to see

ParseError with message "Message too deep" when nesting exceeds max_recursion_depth.

What did you see instead?

Message parses successfully with no error. The depth limit is completely ignored for Struct/Value/ListValue nesting.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions