escape regex metachars in StringDetector word matching#1880
Open
bereketDeneke wants to merge 1 commit into
Open
escape regex metachars in StringDetector word matching#1880bereketDeneke wants to merge 1 commit into
bereketDeneke wants to merge 1 commit into
Conversation
Signed-off-by: Bereket Deneke <bereketsiraw123@gmail.com>
09c2b97 to
597b179
Compare
Collaborator
|
please fill in the PR description. marked as draft until ready |
Author
|
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.
What this change does
StringDetector's"word"match mode interpolated each search string directly into a regex (r"\b" + s + r"\b"). When a target string contained regex metacharacters, it was interpreted as a pattern rather than a literal — a real problem because several built-in wordlists (e.g. the Surge profanity list) contain entries likec*nt,s.o.b., andpu$sy.This caused two classes of bugs:
s.o.b.matching benign words likesnobby(.matches any char).pu$sy($acts as an anchor) orc*nt(*makes the precedingcoptional).The fix wraps the search string in
re.escape()so metacharacters are matched literally:Verification
test_stringdetector_word_regex_metacharacterscovering literal matching ofs.o.b.,pu$sy, andc*nt, including the false-positive/false-negative cases above.python -m pytest tests/detectors/test_detectors_base.py