Repository navigation
[Python] enforce read bounds in Table - #9281
Open
ManoharPaturi wants to merge 1 commit into
Open
ManoharPaturi wants to merge 1 commit into
ManoharPaturi wants to merge 1 commit into
Conversation
Offsets come from the buffer itself when reading untrusted data. struct.unpack_from silently accepts negative offsets (reading relative to the buffer end) and slicing clamps out-of-range ranges, so malformed buffers previously produced wrong values instead of errors. Adds a _CheckOffset guard to Offset, Indirect, String, and Vector, and rejects huge unsigned string lengths, raising IndexError with a clear message instead. Mirrors the read-bounds enforcement the Swift port adopted (google#9277).
This branch has not been deployed
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.
Problem
The Python
Tableaccessors trust offsets that come from the buffer itself when reading untrusted data:struct.unpack_fromsilently accepts negative offsets, reading relative to the end of the buffer, soIndirect/Offsetwith a negative computed target returns data from the wrong position with no error,StringslicesBytes[start : start + length], and Python slicing clamps out-of-range ranges, so a malformed length silently returns a truncated string (or an empty one for huge unsigned lengths like0xffffffff),Vectorreturns a start position beyond the buffer without complaint.Change
Adds a
_CheckOffset(buf, offset, size, what)guard:Offsetvalidates the resolved vtable position (negative soffset no longer wraps),Indirectvalidates the computed target,Stringvalidatesstart + lengthagainst the buffer size before slicing,Vectorvalidates the computed start (an empty vector may start exactly at the end).Malformed buffers now raise
IndexErrorwith a clear message instead of silently producing wrong values. Mirrors the read-bounds enforcement direction the Swift port has adopted (#9277), applied to the Python port (companion to the TS-port change in #9280).Test plan
tests/py_table_bounds_test.py: negative/out-of-range indirect and vtable targets, clamped and huge-length strings, vector start past the end, plus valid-target sanity cases — 10/10 passmaster, the same tests fail with wrong values instead of raising — confirming the silent misparse behaviortests/py_flexbuffers_test.pyunchanged and passing (60/60) after the changepy_test.pyrequires flatc-generated code and runs in CI)