Skip to content

Commit 130d27c

Browse files
authored
Merge pull request #405 from SigmaHQ/copilot/fix-condition-transformation-issue
Fix AddConditionTransformation to handle empty conditions
2 parents 7a0a066 + c7def4c commit 130d27c

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

sigma/processing/transformations/condition.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ def apply(self, rule: Union[SigmaRule, SigmaCorrelationRule]) -> None:
7070
super().apply(rule)
7171

7272
def apply_condition(self, cond: SigmaCondition) -> None:
73-
cond.condition = ("not " if self.negated else "") + f"{self.name} and ({cond.condition})"
73+
if cond.condition: # If condition is not empty
74+
cond.condition = (
75+
"not " if self.negated else ""
76+
) + f"{self.name} and ({cond.condition})"
77+
else: # If condition is empty, just use the added condition name
78+
cond.condition = ("not " if self.negated else "") + self.name

tests/test_processing_transformations.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,66 @@ def test_addconditiontransformation_negated(dummy_pipeline, sigma_rule: SigmaRul
15161516
)
15171517

15181518

1519+
def test_addconditiontransformation_empty_condition(dummy_pipeline, sigma_rule: SigmaRule):
1520+
"""Test that AddConditionTransformation correctly handles empty conditions (e.g., when all query terms are deferred)"""
1521+
# Manually set condition to empty string to simulate deferred query scenario
1522+
sigma_rule.detection.parsed_condition[0].condition = ""
1523+
1524+
transformation = AddConditionTransformation(
1525+
{
1526+
"newfield1": "test",
1527+
},
1528+
"additional",
1529+
)
1530+
transformation.set_processing_item(
1531+
ProcessingItem(
1532+
transformation,
1533+
identifier="test",
1534+
)
1535+
)
1536+
transformation.set_pipeline(dummy_pipeline)
1537+
transformation.apply(sigma_rule)
1538+
1539+
# When condition is empty, should result in just "additional", not "additional and ()"
1540+
assert (
1541+
sigma_rule.detection.parsed_condition[0].condition == "additional"
1542+
and sigma_rule.detection.detections["additional"]
1543+
== SigmaDetection(
1544+
[
1545+
SigmaDetectionItem("newfield1", [], [SigmaString("test")]),
1546+
]
1547+
)
1548+
and sigma_rule.was_processed_by("test")
1549+
)
1550+
1551+
1552+
def test_addconditiontransformation_empty_condition_negated(dummy_pipeline, sigma_rule: SigmaRule):
1553+
"""Test that AddConditionTransformation correctly handles empty conditions with negation"""
1554+
# Manually set condition to empty string to simulate deferred query scenario
1555+
sigma_rule.detection.parsed_condition[0].condition = ""
1556+
1557+
transformation = AddConditionTransformation(
1558+
{
1559+
"newfield1": "test",
1560+
},
1561+
"additional",
1562+
negated=True,
1563+
)
1564+
transformation.set_processing_item(
1565+
ProcessingItem(
1566+
transformation,
1567+
identifier="test",
1568+
)
1569+
)
1570+
transformation.set_pipeline(dummy_pipeline)
1571+
transformation.apply(sigma_rule)
1572+
1573+
# When condition is empty and negated, should result in just "not additional", not "not additional and ()"
1574+
assert sigma_rule.detection.parsed_condition[
1575+
0
1576+
].condition == "not additional" and sigma_rule.was_processed_by("test")
1577+
1578+
15191579
### ChangeLogsourceTransformation ###
15201580
def test_changelogsource(dummy_pipeline, sigma_rule: SigmaRule):
15211581
processing_item = ProcessingItem(

0 commit comments

Comments
 (0)