Skip to content

Add condition existence check in post-init method #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sigma/rule/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ def __post_init__(self):
raise sigma_exceptions.SigmaDetectionError(
"No detections defined in Sigma rule", source=self.source
)
if self.condition == [] or self.condition is None:
raise sigma_exceptions.SigmaConditionError(
"Sigma rule must contain at least one condition", source=self.source
)
self.parsed_condition = [SigmaCondition(cond, self, self.source) for cond in self.condition]

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions tests/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def sigma_simple_detections():
]
),
},
list(),
condition=["any of them"],
)


Expand Down Expand Up @@ -142,7 +142,7 @@ def sigma_detections():
]
),
},
list(),
condition=["any of them"],
)


Expand All @@ -156,7 +156,7 @@ def sigma_invalid_detections():
]
),
},
list(),
condition=["any of them"],
)


Expand All @@ -180,7 +180,7 @@ def sigma_underscore_detections():
]
),
},
list(),
condition=["any of them"],
)


Expand Down
26 changes: 26 additions & 0 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,32 @@ def test_sigmadetections_fromdict_no_condition():
)


def test_sigmadetections_empty_condition_list():
with pytest.raises(
sigma_exceptions.SigmaConditionError, match="at least one condition.*test.yml"
):
SigmaDetections(
detections={
"selection": SigmaDetection([SigmaDetectionItem("key", [], [SigmaString("value")])])
},
condition=[],
source=sigma_exceptions.SigmaRuleLocation("test.yml"),
)


def test_sigmadetections_none_condition():
with pytest.raises(
sigma_exceptions.SigmaConditionError, match="at least one condition.*test.yml"
):
SigmaDetections(
detections={
"selection": SigmaDetection([SigmaDetectionItem("key", [], [SigmaString("value")])])
},
condition=None,
source=sigma_exceptions.SigmaRuleLocation("test.yml"),
)


def test_detectionitem_all_modified_key_plain_values_postprocess():
"""
Test if postprocessed condition result of an all-modified field-bound value list results in an
Expand Down