Fix deferred expressions in correlation rules - #64
Merged
Conversation
…s for table output - Update finish_query, finalize_query_default, and finalize_query_savedsearches type hints to accept Union[SigmaRule, SigmaCorrelationRule] - Guard | table addition in finalize_query_default and finalize_query_savedsearches to only apply for SigmaRule instances (not correlation rules) - Add tests for correlation rules with regex deferred expressions - Add tests for correlation rules with regex OR deferred expressions - Add test verifying sub-rule fields (| table) are excluded from correlation output Co-authored-by: thomaspatzke <1845601+thomaspatzke@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix deferred expressions not applied in correlation rules
Fix deferred expressions in correlation rules
Mar 15, 2026
thomaspatzke
approved these changes
Mar 15, 2026
thomaspatzke
marked this pull request as ready for review
March 15, 2026 00:44
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.
Deferred expressions (
| regex,| rex) in correlation rule sub-queries were dropped from output because finalization and deferred handling were conflated. pySigma 1.2.0 split these intofinish_query()(deferred, called for all rules) andfinalize_query()(output formatting, skipped for sub-rules). The Splunk backend needs to align with this split.Changes
| tablefor correlation rules —finalize_query_defaultandfinalize_query_savedsearchesnow only append| tableforSigmaRuleinstances, preventing invalid output in correlation queries (Splunk rejects non-streaming ops in multisearch subsearches)finish_query,finalize_query_default,finalize_query_savedsearchesnow acceptUnion[SigmaRule, SigmaCorrelationRule]matching the pySigma base classBefore
After
Original prompt
This section details on the original issue you should resolve
<issue_title>deferred expressions not applied in correlation rules</issue_title>
<issue_description>## Description
When a correlation rule has, in one of its "sub rule" or "referenced rule" a deferred expression, like a regex (converted to
| regex) or an regex oring, these are removed from the query.Example :
will currently be converted into:
Explanation
This is because of this line commited following this issue. The thing is, we need to get deferred expressions in the search part.
And as deferred are handled in the
finalize_querystep, in my opinion, we need to reactivate the finalization for rules that are part of a correlation rule, and let the responsibility of the finalization to the backends (ie, let them select which part need to be in the search part of the query, for instance, here, we want deferred expressions to be in the search part, but we dont want this for fields which are converted to| table field1, field2 ...).So, in the splunk backend this could be done by handling this in the
finalize_query_default:I opened this issue as I was working on the #51, as I need to create new deferred expressions which are missing in the correlation rules.
Summary
So, in a nutshell, I propose:
ifcondition, allowing all rules to be finalized, even those which are part of a correlation rule.finalize_query_default, as mentioned previously.</issue_description><agent_instructions>Implement a fix for this issue by utilizing the query finalization feature from pySigma.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@thomaspatzke After thinking about this issue I believe that this is a structural issue in pySigma. Currently, generation of deferred query parts and output formats is handled in one finalization step that can be disabled as a whole. This was irrelevant before correlation rules were introduced, but isn't anymore after their introduction, as issues similar to this shown.My proposal is to split the finalization step into two:
| fields ....<comment_new>@thomaspatzke
Just implemented it in pySigma branch finalization-split and a test for correlation rules with deferred query expressions passes. The handling of deferred query parts is now located in
finish_query()that is also called for correlation rules while the finalization isn't. Implementing a similar split in the Splun...🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.