Skip to content

Commit

Permalink
fix: remove references to regexp.original, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dh4t committed Feb 18, 2025
1 parent 37e0f3e commit 00ecba3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
6 changes: 3 additions & 3 deletions sigma/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def modify(
if not val.endswith(SpecialChars.WILDCARD_MULTI):
val += SpecialChars.WILDCARD_MULTI
elif isinstance(val, SigmaRegularExpression):
regexp_str = val.regexp.convert()
regexp_str = str(val.regexp)
if regexp_str[:2] != ".*" and regexp_str[0] != "^":
val.regexp = SigmaString(".") + SpecialChars.WILDCARD_MULTI + val.regexp
if regexp_str[-2:] != ".*" and regexp_str[-1] != "$":
Expand All @@ -150,7 +150,7 @@ def modify(
if not val.endswith(SpecialChars.WILDCARD_MULTI):
val += SpecialChars.WILDCARD_MULTI
elif isinstance(val, SigmaRegularExpression):
regexp_str = val.regexp.convert()
regexp_str = str(val.regexp)
if regexp_str[-2:] != ".*" and regexp_str[-1] != "$":
val.regexp += SigmaString(".") + SpecialChars.WILDCARD_MULTI
val.compile()
Expand All @@ -169,7 +169,7 @@ def modify(
if not val.startswith(SpecialChars.WILDCARD_MULTI):
val = SpecialChars.WILDCARD_MULTI + val
elif isinstance(val, SigmaRegularExpression):
regexp_str = val.regexp.convert()
regexp_str = str(val.regexp)
if regexp_str[:2] != ".*" and regexp_str[0] != "^":
val.regexp = SigmaString(".") + SpecialChars.WILDCARD_MULTI + val.regexp
val.compile()
Expand Down
11 changes: 6 additions & 5 deletions sigma/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,10 @@ def compile(self):
flags = 0
for flag in self.flags:
flags |= self.sigma_to_python_flags[flag]
re.compile(self.regexp.original, flags)
re.compile(self.escape(), flags)
except re.error as e:
raise SigmaRegularExpressionError(
f"Regular expression '{self.regexp.original}' is invalid: {str(e)}"
f"Regular expression '{self.escape()}' is invalid: {str(e)}"
) from e

def escape(
Expand All @@ -757,9 +757,10 @@ def escape(
if e is not None
]
)
regexp_str = str(self.regexp)
pos = (
[ # determine positions of matches in regular expression
m.start() for m in re.finditer(r, self.regexp.original)
m.start() for m in re.finditer(r, regexp_str)
]
if r != ""
else []
Expand All @@ -774,7 +775,7 @@ def escape(
else:
prefix = ""

return prefix + escape_char.join([self.regexp.original[i:j] for i, j in ranges])
return prefix + escape_char.join([regexp_str[i:j] for i, j in ranges])

def contains_placeholder(
self, include: Optional[List[str]] = None, exclude: Optional[List[str]] = None
Expand All @@ -796,7 +797,7 @@ def replace_placeholders(
Replace all occurrences of string part matching regular expression with placeholder.
"""
return [
SigmaRegularExpression(regexp=sigmastr.convert(), flags=self.flags)
SigmaRegularExpression(regexp=str(sigmastr), flags=self.flags)
for sigmastr in self.regexp.replace_placeholders(callback)
]

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


def test_convert_value_regex_value_list_endswith():
pipeline = ProcessingPipeline(
[ProcessingItem(ValueListPlaceholderTransformation(["test"]))],
vars={"test": ["pat.*tern/foobar", "pat.*te\\rn/foobar"]},
)
backend = TextQueryTestBackend(pipeline)
assert (
backend.convert(
SigmaCollection.from_yaml(
"""
title: Test
status: test
logsource:
category: test_category
product: test_product
detection:
sel:
field|re|expand|endswith: "%test%"
condition: sel
"""
)
)
== ["field=/.*pat.*tern\\/foo\\bar/ or field=/.*pat.*te\\\\rn\\/foo\\bar/"]
)


def test_convert_value_cidr_wildcard_native_ipv4(test_backend):
assert (
test_backend.convert(
Expand Down

0 comments on commit 00ecba3

Please sign in to comment.