Repository navigation
Python: Model bytearray construction and take_bytes
#22746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+106
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
python/ql/lib/change-notes/2026-09-22-python315-take-bytes.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added taint-flow modeling for `bytearray` construction and Python 3.15's `bytearray.take_bytes` method. |
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
88 changes: 88 additions & 0 deletions
88
...est/library-tests/dataflow/tainttracking/defaultAdditionalTaintStep-py3/test_bytearray.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Add taintlib to PATH so it can be imported during runtime without any hassle | ||
| import sys; import os; sys.path.append(os.path.dirname(os.path.dirname((__file__)))) | ||
| from taintlib import TAINTED_BYTES, TAINTED_STRING, ensure_tainted, ensure_not_tainted, taint | ||
|
|
||
|
|
||
| def constructors(): | ||
| import builtins | ||
| from builtins import bytearray as make_buffer | ||
|
|
||
| ensure_tainted( | ||
| bytearray(TAINTED_BYTES), # $ tainted | ||
| bytearray(source=TAINTED_BYTES), # $ tainted | ||
| bytearray(TAINTED_STRING, "utf-8"), # $ tainted | ||
| bytearray(source=TAINTED_STRING, encoding="utf-8"), # $ tainted | ||
| builtins.bytearray(TAINTED_BYTES), # $ tainted | ||
| make_buffer(TAINTED_BYTES), # $ tainted | ||
| ) | ||
| ensure_not_tainted(bytearray(b"safe")) | ||
|
|
||
|
|
||
| def shadowed_constructor(): | ||
| def bytearray(source): | ||
| return b"safe" | ||
|
|
||
| ensure_not_tainted(bytearray(TAINTED_BYTES)) | ||
|
|
||
|
|
||
| def take_bytes(): | ||
| ensure_tainted( | ||
| bytearray(TAINTED_BYTES).take_bytes(), # $ tainted | ||
| bytearray(TAINTED_BYTES).take_bytes(None), # $ tainted | ||
| bytearray(source=TAINTED_STRING, encoding="utf-8").take_bytes().decode(), # $ tainted | ||
| bytearray.take_bytes(bytearray(TAINTED_BYTES)), # $ tainted | ||
| ) | ||
|
|
||
| buffer = bytearray(TAINTED_BYTES) | ||
| take = buffer.take_bytes | ||
| ensure_tainted(take()) # $ tainted | ||
|
|
||
| ensure_not_tainted(bytearray(b"safe").take_bytes()) | ||
| ensure_not_tainted(bytearray().take_bytes()) | ||
|
|
||
|
|
||
| def take_partial_bytes(): | ||
| buffer = bytearray(TAINTED_BYTES) | ||
| ensure_tainted(buffer.take_bytes(1)) # $ tainted | ||
| ensure_tainted(buffer) # $ tainted | ||
| ensure_tainted(buffer.take_bytes(-1)) # $ tainted | ||
| ensure_tainted(buffer) # $ tainted | ||
|
|
||
| size = 1 | ||
| taint(size) | ||
| ensure_not_tainted(bytearray(b"safe").take_bytes(size)) | ||
|
|
||
|
|
||
| def empty_results(): | ||
| buffer = bytearray(TAINTED_BYTES) | ||
| # Whole-buffer taint does not distinguish empty slices. | ||
| ensure_not_tainted(buffer[:0]) # $ SPURIOUS: tainted | ||
| ensure_not_tainted(buffer.take_bytes(0)) # $ SPURIOUS: tainted | ||
|
|
||
|
|
||
| def consumed_buffer(): | ||
| buffer = bytearray(b"abc") | ||
| taint(buffer) | ||
| result = buffer.take_bytes() | ||
| ensure_tainted(result) # $ tainted | ||
|
|
||
| # Whole-buffer taint is not removed when the buffer is emptied. | ||
| ensure_not_tainted(buffer) # $ SPURIOUS: tainted | ||
| ensure_not_tainted(buffer.take_bytes()) # $ SPURIOUS: tainted | ||
|
|
||
|
|
||
| def cleared_buffer(): | ||
| buffer = bytearray(b"abc") | ||
| taint(buffer) | ||
| buffer.clear() | ||
| ensure_not_tainted(buffer) # $ SPURIOUS: tainted | ||
|
|
||
|
|
||
| constructors() | ||
| shadowed_constructor() | ||
| cleared_buffer() | ||
| if sys.version_info >= (3, 15): | ||
| take_bytes() | ||
| take_partial_bytes() | ||
| empty_results() | ||
| consumed_buffer() | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be fixed easily with a barrier model, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right. I'll add this as a follow-up PR.