fix(analyzer): stop triple-quote parity from suppressing YARA matches - #837
Open
Sanjays2402 wants to merge 1 commit into
Open
fix(analyzer): stop triple-quote parity from suppressing YARA matches#837Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
_is_in_multiline_comment decided whether a Python YARA hit sat inside a docstring by counting """ and ''' occurrences in the preceding window and treating an odd count as "inside a comment". A triple quote that is merely a string value (banner = '"""') or that appears in a # comment also increments that count, so parity flipped and every YARA match in the following 4096 bytes was silently discarded as a comment match. Replace the count with a single forward pass that tracks the open quote state, honours escapes, and does not treat single-quoted or commented triple quotes as docstring delimiters. Adds two regression tests to tests/core/test_sourcecode_analyzer.py covering the string-value and hash-comment cases; both fail without the fix.
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.
Closes #831
_is_in_multiline_commentcounted"""/'''occurrences in the preceding window and treated an odd count as "inside a docstring". A triple quote that is only a string value (banner = '"""'), or one inside a#comment, also increments that count, so parity flipped andanalyze_yaradiscarded every match in the next 4096 bytes as a comment match — a false-negative source in normal scanning and a trivial suppression primitive under attack.This takes the cheaper of the two directions suggested on the issue: a single forward pass over the window that tracks the open quote, honours escapes, and only toggles when a triple quote actually opens or closes a literal. Full tokenising would be more exact but heavier on this path.
Two regression tests in
tests/core/test_sourcecode_analyzer.pycover the string-value and hash-comment cases; both fail on the parity implementation and pass with the fix. The existing multiline-comment tests are unchanged and still pass (50 passed in that file, 161 intests/core— the twotest_sandbox.pyfailures are pre-existing on a clean checkout here).This change was prepared with AI assistance; the regression test was run locally and fails without the fix.