Skip to content

fix(analyzer): stop triple-quote parity from suppressing YARA matches - #837

Open
Sanjays2402 wants to merge 1 commit into
DataDog:v3from
Sanjays2402:fix/triple-quote-parity-false-negative
Open

fix(analyzer): stop triple-quote parity from suppressing YARA matches#837
Sanjays2402 wants to merge 1 commit into
DataDog:v3from
Sanjays2402:fix/triple-quote-parity-false-negative

Conversation

@Sanjays2402

Copy link
Copy Markdown

Closes #831

_is_in_multiline_comment counted """/''' 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 and analyze_yara discarded 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.py cover 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 in tests/core — the two test_sandbox.py failures 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.

_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.
@Sanjays2402
Sanjays2402 requested a review from a team as a code owner July 31, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Triple-quote parity heuristic in _is_in_multiline_comment suppresses YARA matches (false negatives)

1 participant