Skip to content
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

Fixed the type transformation when the given value is a SigmaExpansion #329

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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dist/
docs/_build
coverage.xml
.python-version
.idea
.idea
.venv
17 changes: 16 additions & 1 deletion sigma/processing/transformations/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from sigma.types import (
Placeholder,
SigmaBool,
SigmaExpansion,
SigmaNull,
SigmaNumber,
SigmaRegularExpression,
Expand Down Expand Up @@ -361,11 +362,25 @@ class ConvertTypeTransformation(ValueTransformation):

target_type: Literal["str", "num"]

def apply_value(self, field: str, val: SigmaType) -> Optional[Union[SigmaString, SigmaNumber]]:
def apply_value(
self, field: str, val: SigmaType
) -> Optional[Union[SigmaString, SigmaNumber, SigmaExpansion]]:
if self.target_type == "str":
if isinstance(val, SigmaExpansion):
for i, entry in enumerate(val.values):
val.values[i] = SigmaString(str(entry))

return val

return SigmaString(str(val))
elif self.target_type == "num":
try:
if isinstance(val, SigmaExpansion):
for i, entry in enumerate(val.values):
val.values[i] = SigmaNumber(str(entry))

return val

return SigmaNumber(str(val))
except SigmaValueError:
raise SigmaValueError(f"Value '{val}' can't be converted to number for {str(self)}")
Expand Down
22 changes: 22 additions & 0 deletions tests/test_processing_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
from sigma.types import (
Placeholder,
SigmaBool,
SigmaExpansion,
SigmaNull,
SigmaNumber,
SigmaQueryExpression,
Expand Down Expand Up @@ -1800,6 +1801,27 @@ def test_convert_type_transformation_str_to_num_no_number():
transformation.apply_detection_item(detection_item)


def test_convert_type_transformation_expansion_num_to_str():
transformation = ConvertTypeTransformation("str")
detection_item = SigmaDetectionItem("field", [], [SigmaExpansion(values=[SigmaNumber(123)])])
transformation.apply_detection_item(detection_item)
assert detection_item.value[0] == SigmaExpansion(values=[SigmaString("123")])


def test_convert_type_transformation_expansion_str_to_num():
transformation = ConvertTypeTransformation("num")
detection_item = SigmaDetectionItem("field", [], [SigmaExpansion(values=[SigmaString("123")])])
transformation.apply_detection_item(detection_item)
assert detection_item.value[0] == SigmaExpansion(values=[SigmaNumber(123)])


def test_convert_type_transformation_expansion_str_to_num_no_number():
transformation = ConvertTypeTransformation("num")
detection_item = SigmaDetectionItem("field", [], [SigmaExpansion(values=[SigmaString("abc")])])
with pytest.raises(SigmaValueError, match="can't be converted to number"):
transformation.apply_detection_item(detection_item)


def test_set_state(dummy_pipeline, sigma_rule: SigmaRule):
transformation = SetStateTransformation("testkey", "testvalue")
transformation.set_processing_item(
Expand Down