Python: Handle guards being compared to boolean literals#21296
Python: Handle guards being compared to boolean literals#21296yoff wants to merge 3 commits intogithub:mainfrom
Conversation
a80fc8c to
9c2ce6a
Compare
There was a problem hiding this comment.
Pull request overview
This PR extends Python’s guard-node handling in the dataflow/tainttracking library so that guard calls (e.g., is_safe(x)) are recognized even when compared against boolean literals using ==, !=, is, and is not.
Changes:
- Extend guard-node recursion to treat comparisons against boolean literals as guard nodes with appropriate polarity.
- Add taint-tracking tests covering
==/!=andis/is notcomparisons toTrue/False, and update expected results. - Add a change note documenting the enhancement.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/ql/test/library-tests/dataflow/tainttracking/customSanitizer/test_logical.py | Adds a new test function covering boolean-literal comparisons for guards. |
| python/ql/test/library-tests/dataflow/tainttracking/customSanitizer/InlineTaintTest.expected | Updates expected sanitizer/guard nodes for the new test cases. |
| python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPublic.qll | Extends guard-node logic to recurse through boolean-literal comparisons. |
| python/ql/lib/change-notes/2026-02-08-guards-compared-to-boolean-literals.md | Adds a release note for the new guard-comparison handling. |
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * When a guard such as `isSafe(x)` is defined, we now also automatically handle `isSafe(x) == true` and `isSafe(x) != false`. |
There was a problem hiding this comment.
The change note currently documents only == true / != false, but the implementation and PR description also cover is / is not. Also, for Python-facing examples the boolean literals should be True/False (capitalized) to match Python syntax.
| * When a guard such as `isSafe(x)` is defined, we now also automatically handle `isSafe(x) == true` and `isSafe(x) != false`. | |
| * When a guard such as `isSafe(x)` is defined, we now also automatically handle comparisons to boolean literals such as `isSafe(x) is True`, `isSafe(x) == True`, `isSafe(x) is not False`, and `isSafe(x) != False`. |
| if is_safe(s) is not False: | ||
| ensure_not_tainted(s) | ||
| else: | ||
| ensure_tainted(s) # $ tainted |
There was a problem hiding this comment.
Line has trailing whitespace after the # $ tainted marker; please remove it to keep test files clean and avoid noisy diffs/linters.
| ensure_tainted(s) # $ tainted | |
| ensure_tainted(s) # $ tainted |
Inspired by https://github.com/github/codeql/pull/21288/changes#diff-502cb70dbb74ee46cda83c2b3c626120ae120bcd444e146be09bc46599cf981eR194-R215
While we wait for adoption of the shared guards library (which will handle much more than this), this PR is an easy addition to the guards handling in Python.
When a guard such as
isSafe(x)is defined, we now also automatically handleisSafe(x) == trueandisSafe(x) != false(as well asisandis not).