Skip to content

Commit a189552

Browse files
committed
Fix: escape escpae characters in strings
1 parent bcb8ebc commit a189552

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

sigma/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ def convert(
572572
of these characters in a string will raise a SigmaValueError.
573573
"""
574574
s = ""
575-
escaped_chars = frozenset((wildcard_multi or "") + (wildcard_single or "") + add_escaped)
575+
escaped_chars = frozenset(
576+
(wildcard_multi or "") + (wildcard_single or "") + escape_char + add_escaped
577+
)
576578

577579
for c in iter(self):
578580
if isinstance(c, str): # c is plain character

tests/test_conversion_base.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,31 @@ def test_convert_value_str_startswith_trailing_backslash(test_backend):
426426
"""
427427
)
428428
)
429-
== ['mappedA startswith "foobar\\"']
429+
== ['mappedA startswith "foobar\\\\"']
430+
)
431+
432+
433+
def test_convert_value_str_startswith_trailing_backslash_no_startswith_expression(
434+
test_backend, monkeypatch
435+
):
436+
monkeypatch.setattr(test_backend, "startswith_expression", None)
437+
assert (
438+
test_backend.convert(
439+
SigmaCollection.from_yaml(
440+
"""
441+
title: Test
442+
status: test
443+
logsource:
444+
category: test_category
445+
product: test_product
446+
detection:
447+
sel:
448+
fieldA|startswith: "foobar\\\\"
449+
condition: sel
450+
"""
451+
)
452+
)
453+
== ['mappedA match "foobar\\\\*"']
430454
)
431455

432456

@@ -753,7 +777,31 @@ def test_convert_value_str_contains_trailing_backslash(test_backend):
753777
"""
754778
)
755779
)
756-
== ['mappedA contains "foobar\\"']
780+
== ['mappedA contains "foobar\\\\"']
781+
)
782+
783+
784+
def test_convert_value_str_contains_trailing_backslash_no_contains_expression(
785+
test_backend, monkeypatch
786+
):
787+
monkeypatch.setattr(test_backend, "contains_expression", None)
788+
assert (
789+
test_backend.convert(
790+
SigmaCollection.from_yaml(
791+
"""
792+
title: Test
793+
status: test
794+
logsource:
795+
category: test_category
796+
product: test_product
797+
detection:
798+
sel:
799+
fieldA|contains: "foobar\\\\"
800+
condition: sel
801+
"""
802+
)
803+
)
804+
== ['mappedA match "*foobar\\\\*"']
757805
)
758806

759807

0 commit comments

Comments
 (0)