Fix(pattern_match): prevent false positive when valid regex doesn't match#274
Open
Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Open
Fix(pattern_match): prevent false positive when valid regex doesn't match#274Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Conversation
…atch Root cause: _matches_pattern lacks early return for valid regex with no match, causing fallthrough to literal substring check. If text contains the raw regex pattern as literal characters, it incorrectly returns True. Solution: Add `return False, None` after regex search attempt when no match is found. This ensures valid regex with no match returns False without falling back to literal matching. Invalid regex still falls back to literal (existing behavior). Impact: Fixes false positive for case where literal regex string appears in text. No breaking changes, minimal diff, preserves existing behavior for all other cases. Signed-off-by: JEAN REGIS <240509606@firat.edu.tr>
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
Fixes #129
Adds an early return for the case where a valid regex finds no match, preventing fallthrough to a literal substring check that could cause false positives.
Problem
When
is_regex=Trueand the regex is valid but finds no match,_matches_patternproceeds to a literal substring search using the raw regex string. If the text contains that raw string (e.g., the pattern appears literally), the function incorrectly returns(True, matched_text).Root Cause
In
_matches_pattern, after the regex search block (try/except), there is no explicit return for the “valid regex, no match” case. Execution falls through to the literal search logic, which treats the pattern as a plain substring.Solution
Added
return False, Noneinside thetryblock after checking for a match. This ensures that a valid regex with no matches returns immediately, bypassing the literal fallback. Invalid regex still falls through to literal matching (existing behavior).Impact
Testing