Skip to content

Commit 29cbe82

Browse files
committed
Strict detection item checks
1 parent 464fc23 commit 29cbe82

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

sigma/rule/detection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class SigmaDetectionItem(ProcessingItemTrackingMixin, ParentChainMixin):
6868
auto_modifiers: bool = dataclasses.field(default=True, compare=False, repr=False)
6969

7070
def __post_init__(self) -> None:
71+
if not isinstance(self.value, list) or not all(
72+
isinstance(val, SigmaType) for val in self.value
73+
):
74+
raise sigma_exceptions.SigmaTypeError(
75+
"Value must be a list of SigmaType instances", source=self.source
76+
)
7177
self.original_value = self.value.copy() # Create a copy of original values
7278
if self.auto_modifiers:
7379
self.apply_modifiers()

tests/test_modifiers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
@pytest.fixture
5858
def dummy_detection_item():
59-
return SigmaDetectionItem(None, [], ["foobar"])
59+
return SigmaDetectionItem(None, [], [SigmaString("foobar")])
6060

6161

6262
@pytest.fixture

tests/test_rule.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,6 @@ def test_sigmadetectionitem_keyword_single():
238238
)
239239

240240

241-
def test_sigmadetectionitem_value_cleanup_multi():
242-
"""Multiple value cleanup."""
243-
assert SigmaDetectionItem(None, [], ["value", 123]) == SigmaDetectionItem(
244-
None, [], [SigmaString("value"), SigmaNumber(123)]
245-
)
246-
247-
248241
def test_sigmadetectionitem_keyword_single_to_plain():
249242
"""Single keyword detection."""
250243
assert SigmaDetectionItem(None, [], [SigmaString("value*")]).to_plain() == "value*"
@@ -446,6 +439,22 @@ def test_sigmadetectionitem_processing_item_tracking(processing_item):
446439
assert detection_item.was_processed_by("test")
447440

448441

442+
def test_sigmadetectionitem_invalid_value_type():
443+
"""Test that SigmaDetectionItem raises an error if value is not a list."""
444+
with pytest.raises(
445+
sigma_exceptions.SigmaTypeError, match="Value must be a list of SigmaType instances"
446+
):
447+
SigmaDetectionItem(field="key", modifiers=[], value="invalid_value")
448+
449+
450+
def test_sigmadetectionitem_invalid_value_elements():
451+
"""Test that SigmaDetectionItem raises an error if value contains non-SigmaType elements."""
452+
with pytest.raises(
453+
sigma_exceptions.SigmaTypeError, match="Value must be a list of SigmaType instances"
454+
):
455+
SigmaDetectionItem(field="key", modifiers=[], value=["valid", 123, object()])
456+
457+
449458
### SigmaDetection tests ###
450459

451460

0 commit comments

Comments
 (0)