Repository navigation
fix: OR/AND precedence inversion widens emitted queries in SPL and SPL2 backends - #77
Merged
thomaspatzke merged 2 commits intoSep 19, 2026
Merged
Conversation
… query
The backend declared operator precedence as (NOT, OR, AND) while
emitting AND as juxtaposition (' '), which in SPL binds tighter than
an explicit OR. Because the declared precedence was inverted relative
to the emitted language, compare_precedence omitted the parentheses
an OR needs when nested inside an AND, so the AND-ed conjuncts only
applied to the first disjunct and every later disjunct matched
unscoped.
Reordering the precedence tuple to (NOT, AND, OR) fixes the grouping.
Several existing tests encoded the same bug in their expected output
(an ungrouped OR nested in an AND) and are updated to the correct,
parenthesized form; a couple of others gained harmless (but no longer
necessary) grouping around already self-contained IN(...) expressions.
spl2.py declared the same inverted precedence as splunk.py (OR before AND), but SPL2's AND/OR tokens follow standard boolean precedence where AND binds tighter than OR. An OR nested inside an AND was missing its required parentheses for the same reason as SigmaHQ#73.
thomaspatzke
pushed a commit
that referenced
this pull request
Sep 27, 2026
The Splunk search command evaluates OR before AND (parentheses, NOT, OR, AND), unlike eval/where. Group every AND nested in an OR (rule and extended correlation conditions) so the emitted query is correct under either precedence reading, keeping the OR-under-AND grouping from #77. Only hoist leading terms in front of the rex/eval pipeline when the search expression has no top-level OR. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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 #73.
Both the classic SPL backend (
splunk.py) and the SPL2 backend (spl2.py) declared their operator precedence as(ConditionNOT, ConditionOR, ConditionAND)— i.e. OR binds tighter than AND. But:splunk.pyemits AND as juxtaposition (and_token = " "), which in SPL binds tighter than an explicit OR.spl2.pyemits explicitAND/ORtokens, which follow standard boolean precedence (AND binds tighter than OR).In both cases the declared precedence was inverted relative to the language actually being emitted, so
compare_precedenceconcluded that an OR nested inside an AND does not needgroup_expressionparentheses, and omitted them. The resulting query is broader than the rule: the AND-ed conjuncts apply only to the first disjunct, and every later disjunct matches unscoped.Before:
Image="*\\net.exe" OR OriginalFileName="net.exe" CommandLine="* localgroup*"— SPL parses this as(Image=...) OR (OriginalFileName=... AND CommandLine=...), so a bareImagematch fires with noCommandLinerequirement at all.After:
(Image="*\\net.exe" OR OriginalFileName="net.exe") CommandLine="* localgroup*"— matches the rule's condition. The SPL2 backend had the identical bug for the same underlying reason and is fixed the same way.Fix
Reorder the precedence tuple to
(ConditionNOT, ConditionAND, ConditionOR)in bothsplunk.pyandspl2.py, matching the actual precedence of the language each backend emits.A few existing tests encoded the same bug in their expected output (an ungrouped OR nested in an AND, e.g. the CIDR-expansion and data-model tests) — those are updated to the correct, parenthesized form. A couple of others (the multi-valued-field
IN(...)cases, and an AND nested in OR that no longer needs its parens) gain or lose harmless-but-no-longer-necessary grouping; updated their expected output too.Test plan
test_splunk_or_nested_in_and_expression, reproducing the issue's example directly against the SPL backend.pytest— full suite passes (128 passed).