Skip to content

Commit f94438a

Browse files
committed
Added condition existence check to post-init method
1 parent 1fbea68 commit f94438a

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Diff for: sigma/rule/detection.py

+4
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ def __post_init__(self):
454454
raise sigma_exceptions.SigmaDetectionError(
455455
"No detections defined in Sigma rule", source=self.source
456456
)
457+
if self.condition == [] or self.condition is None:
458+
raise sigma_exceptions.SigmaConditionError(
459+
"Sigma rule must contain at least one condition", source=self.source
460+
)
457461
self.parsed_condition = [SigmaCondition(cond, self, self.source) for cond in self.condition]
458462

459463
@classmethod

Diff for: tests/test_conditions.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def sigma_simple_detections():
4545
]
4646
),
4747
},
48-
list(),
48+
condition=["any of them"],
4949
)
5050

5151

@@ -142,7 +142,7 @@ def sigma_detections():
142142
]
143143
),
144144
},
145-
list(),
145+
condition=["any of them"],
146146
)
147147

148148

@@ -156,7 +156,7 @@ def sigma_invalid_detections():
156156
]
157157
),
158158
},
159-
list(),
159+
condition=["any of them"],
160160
)
161161

162162

@@ -180,7 +180,7 @@ def sigma_underscore_detections():
180180
]
181181
),
182182
},
183-
list(),
183+
condition=["any of them"],
184184
)
185185

186186

Diff for: tests/test_rule.py

+26
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,32 @@ def test_sigmadetections_fromdict_no_condition():
622622
)
623623

624624

625+
def test_sigmadetections_empty_condition_list():
626+
with pytest.raises(
627+
sigma_exceptions.SigmaConditionError, match="at least one condition.*test.yml"
628+
):
629+
SigmaDetections(
630+
detections={
631+
"selection": SigmaDetection([SigmaDetectionItem("key", [], [SigmaString("value")])])
632+
},
633+
condition=[],
634+
source=sigma_exceptions.SigmaRuleLocation("test.yml"),
635+
)
636+
637+
638+
def test_sigmadetections_none_condition():
639+
with pytest.raises(
640+
sigma_exceptions.SigmaConditionError, match="at least one condition.*test.yml"
641+
):
642+
SigmaDetections(
643+
detections={
644+
"selection": SigmaDetection([SigmaDetectionItem("key", [], [SigmaString("value")])])
645+
},
646+
condition=None,
647+
source=sigma_exceptions.SigmaRuleLocation("test.yml"),
648+
)
649+
650+
625651
def test_detectionitem_all_modified_key_plain_values_postprocess():
626652
"""
627653
Test if postprocessed condition result of an all-modified field-bound value list results in an

0 commit comments

Comments
 (0)