Skip to content

Commit

Permalink
Merge pull request #197 from nzedler/main
Browse files Browse the repository at this point in the history
Fix missing fields in to_dict() methods
  • Loading branch information
thomaspatzke authored Mar 17, 2024
2 parents 4965899 + 32b7e9e commit 1e2f076
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion sigma/correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def from_dict(
return cls(op=cond_op, count=cond_count, fieldref=cond_field, source=source)

def to_dict(self) -> dict:
return {self.op.name.lower(): self.count}
if not self.fieldref:
return {self.op.name.lower(): self.count}
return {self.op.name.lower(): self.count, "field": self.fieldref}


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion sigma/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ def to_dict(self) -> dict:
"title": self.title,
}
# Convert to string where possible
for field in ("id", "status", "level", "author", "description"):
for field in ("id", "status", "level", "author", "description", "name"):
if (s := self.__getattribute__(field)) is not None:
d[field] = str(s)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ def test_correlation_condition_with_field():
assert cond.fieldref == "test"


def test_correlation_condition_with_field_to_dict():
assert SigmaCorrelationCondition(
op=SigmaCorrelationConditionOperator.GTE, count=10, fieldref="test"
).to_dict() == {"field": "test", "gte": 10}


def test_correlation_condition_invalid_multicond():
with pytest.raises(
SigmaCorrelationConditionError,
Expand Down
1 change: 1 addition & 0 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@ def test_sigmarule_to_dict(sigma_rule: SigmaRule):
assert sigma_rule.to_dict() == {
"title": "Test",
"id": "9a6cafa7-1481-4e64-89a1-1f69ed08618c",
"name": "test",
"status": "test",
"description": "This is a test",
"references": [
Expand Down

0 comments on commit 1e2f076

Please sign in to comment.