THRIFT-6043: Limit struct read/write recursion depth in Python library#3592
Open
Jens-G wants to merge 1 commit into
Open
THRIFT-6043: Limit struct read/write recursion depth in Python library#3592Jens-G wants to merge 1 commit into
Jens-G wants to merge 1 commit into
Conversation
Client: py Adds a configurable recursion depth limit (default 64) to struct and exception read/write in the Python library. Both the pure-Python path and the fastbinary C extension are guarded. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
Adds a configurable recursion depth limit (default 64) to struct and exception read/write in the Python library.
What is changed:
compiler/cpp/src/thrift/generate/t_py_generator.cc: generatedread()/write()methods now wrap the body inincrement_recursion_depth()/try/finally: decrement_recursion_depth().lib/py/src/protocol/TProtocol.py:TProtocolBasegains_recursion_depth,DEFAULT_RECURSION_DEPTH = 64,increment_recursion_depth()(raisesTProtocolException.DEPTH_LIMITwhen exceeded), anddecrement_recursion_depth().lib/py/src/ext/protocol.h/protocol.tcc: The fastbinary C extension (TBinaryProtocolAccelerated,TCompactProtocolAccelerated) is also guarded.ProtocolBasegains arecursionDepth_counter;readStruct()andencodeValue(T_STRUCT)use a RAIIRecursionGuardthat raisesTProtocolException(DEPTH_LIMIT, …)via the Python C API when the limit is exceeded.lib/py/test/test_recursion_depth.py+lib/py/Makefile.am: Generated-code round-trip tests overtest/Recursive.thrift(RecTreestruct,CoErrorexception) forTBinaryProtocol,TBinaryProtocolAccelerated,TCompactProtocol,TCompactProtocolAccelerated, andTJSONProtocol.Note: The guard is placed after the fast-path early return in generated
read(), soTBinaryProtocol(non-accelerated) andTJSONProtocolare protected via the Python path;TBinaryProtocolAcceleratedandTCompactProtocolAcceleratedare protected via the C extension guard. The limit (64) is hardcoded in the C extension; making it read fromTConfigurationcan be a follow-up.skip()remains independently bounded viaTProtocolBase.skip(max_depth=64)(unchanged).Test plan
make -C lib/py checkpasses (Python 3.10–3.14, with and withoutskip-build-ext)test_recursion_depth.pypass locally (Dockerthrift:jammy)RecTree.read()/write()causes 8 over-limit assertions to fail🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com