Python: Fix false positive for unmatchable dollar/caret#20495
Merged
Conversation
Our previous modelling did not account for the fact that a lookahead can potentially extend all the way to the end of the input (and similarly, that a lookbehind can extend all the way to the beginning). To fix this, I extended `firstPart` and `lastPart` to handle lookbehinds and lookaheads correctly, and added some test cases (all of which yield no new results). Fixes #20429.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes false positives in CodeQL queries that check for unmatchable $ and ^ anchors in regular expressions by improving the handling of lookahead and lookbehind assertions. The fix ensures that anchors inside these assertions are correctly analyzed for their potential to match the beginning or end of input.
Key changes:
- Extended lookahead/lookbehind predicate signatures to include content boundaries
- Updated
firstPartandlastPartlogic to handle assertions that can reach string boundaries - Added test cases covering various lookahead and lookbehind scenarios
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| python/ql/lib/semmle/python/regexp/internal/ParseRegExp.qll | Core fix: extended assertion predicates and added logic for handling assertions in first/last part detection |
| python/ql/lib/semmle/python/regexp/RegexTreeView.qll | Updated calls to assertion predicates to match new signatures |
| python/ql/test/query-tests/Expressions/Regex/test.py | Added test cases for lookahead and lookbehind assertions |
| python/ql/test/library-tests/regex/FirstLast.expected | Updated expected test results reflecting the improved analysis |
| python/ql/src/change-notes/2025-09-19-fix-unmatchable-dollar-and-caret-in-assertions.md | Added release notes documenting the fix |
Contributor
Author
|
DCA looks uneventful. Merging. |
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.
Our previous modelling did not account for the fact that a lookahead can potentially extend all the way to the end of the input (and similarly, that a lookbehind can extend all the way to the beginning).
To fix this, I extended
firstPartandlastPartto handle lookbehinds and lookaheads correctly, and added some test cases (all of which yield no new results).Fixes #20429.