Skip to content

Commit

Permalink
Fix: float number support
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspatzke committed Mar 15, 2022
1 parent 4167e05 commit 382e1d0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pySigma"
version = "0.4.0"
version = "0.4.1"
license = "LGPL-2.1-only"
description = "Sigma rule processing and conversion tools"
authors = ["Thomas Patzke <[email protected]>"]
Expand Down
6 changes: 3 additions & 3 deletions sigma/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def from_mapping(
cls,
key : Optional[str],
val : Union[
List[Union[int, str]],
Union[int, str],
List[Union[int, float, str]],
Union[int, float, str],
None,
],
source : Optional[SigmaRuleLocation] = None,
Expand Down Expand Up @@ -184,7 +184,7 @@ def from_mapping(
except KeyError as e:
raise sigma_exceptions.SigmaModifierError(f"Unknown modifier {str(e)}", source=source)

if isinstance(val, (int, str)): # value is plain, convert into single element list
if isinstance(val, (int, float, str)): # value is plain, convert into single element list
val = [val]
elif val is None:
val = [None]
Expand Down
9 changes: 7 additions & 2 deletions sigma/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,13 @@ class SigmaNumber(SigmaType):
number : Union[int, float]

def __post_init__(self):
try:
self.number = int(self.number)
try: # Only use float number if it can't be represented as int.
i = int(self.number)
f = float(self.number)
if i == f:
self.number = i
else:
self.number = f
except ValueError as e:
raise SigmaValueError("Invalid number") from e

Expand Down
8 changes: 6 additions & 2 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ def test_sigmadetectionitem_key_value_single_string_modifier_to_plain():
detection_item.apply_modifiers()
assert detection_item.to_plain() == { "key|contains": "value" }

def test_sigmadetectionitem_key_value_single_number():
"""Key-value detection with one value."""
def test_sigmadetectionitem_key_value_single_int():
"""Key-value detection with one integer value."""
assert SigmaDetectionItem.from_mapping("key", 123) == SigmaDetectionItem("key", [], [SigmaNumber(123)])

def test_sigmadetectionitem_key_value_single_float():
"""Key-value detection with one integer value."""
assert SigmaDetectionItem.from_mapping("key", 12.34) == SigmaDetectionItem("key", [], [SigmaNumber(12.34)])

def test_sigmadetectionitem_key_value_none():
"""Key-value detection with none value."""
assert SigmaDetectionItem.from_mapping("key", None) == SigmaDetectionItem("key", [], [SigmaNull()])
Expand Down
6 changes: 6 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ def test_strings_convert_no_singlewildcard():
with pytest.raises(SigmaValueError, match="Single-character wildcard"):
SigmaString("foo?bar").convert(wildcard_single=None)

def test_number_int():
assert SigmaNumber(123).number == 123

def test_number_float():
assert SigmaNumber(12.34).number == 12.34

def test_number_to_plain():
assert SigmaNumber(123).to_plain() == 123

Expand Down

0 comments on commit 382e1d0

Please sign in to comment.