Skip to content
Open
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
4 changes: 1 addition & 3 deletions src/secrules_parsing/model/secrules.tx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Operator:
'gsbLookup' gsblookup=INT | 'gt' gt=Macro | 'inspectFile' inspectfile=URI | 'ipMatch' ipmatch+=IPCIDR[','] |
'ipMatchF' ipmatchf=STRING | 'ipMatchFromFile' ipmatchfromfile=STRING | 'le' le=INT | 'lt' lt=Macro |
nomatch ?= 'noMatch' | 'pmf' pmf=StringWithSpacesOrPath | 'pmFromFile' pmfromfile=StringWithSpacesOrPath | 'pm' pm=PatternMatch |
'rbl' rbl=Hostname | 'rsub' rsub=AnythingBetweenSlashes | 'rx' rx=RegExp | 'streq' streq=StringWithMacros |
'rbl' rbl=Hostname | 'rsub' rsub=AnythingBetweenSlashes | 'rx' rx=RegExp | 'streq' streq=PatternMatch |
'strmatch' strnmatch=STRING | unconditionalmatch ?= 'unconditionalMatch' |
'validateByteRange' validatebyterange=ByteRangeList | 'validateDTD' validatedtd=PathNameValue |
'validateHash' validatehash=RegExp | 'validateSchema' validateschema=PathNameValue |
Expand Down Expand Up @@ -183,8 +183,6 @@ IPCIDR: /[0-9a-f\.\:]*:[0-9a-f\.\:]*(?:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9]))?|[0

// Single Quote delimited STRING
SQDelimitedSTRING: "'" msg=MessageValue? ':'? macro=MacroVar? "'";
// This string has macros inside, but their value isn't stored in a variable for now.
StringWithMacros: "'" ExtendedMacroWithComma "'";
// String with spaces: when using streq, there are places where a request line is tested
StringWithSpaces: /[A-Za-z0-9\-\/\ \._]+/;

Expand Down
32 changes: 32 additions & 0 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Note: Some operators may have parsing issues or may require specific syntax.
Tests are designed to validate the current parser behavior.
"""
import pytest

from secrules_parsing import parser


Expand Down Expand Up @@ -118,6 +120,36 @@ def test_operator_streq() -> None:
assert matches == 1


# Regression for issue #163: these were silently parsed as an implicit @rx
STREQ_VALUES = [
"POST",
"'POST'",
"phpinfo()",
"'phpinfo()'",
"a[b]",
"'a[b]'",
"'a{b}'",
"%{tx.a}()",
"'%{tx.a}()'",
"HTTP/1.1",
"GET /index.html HTTP/1.1",
]


@pytest.mark.parametrize("value", STREQ_VALUES)
def test_operator_streq_values(value: str) -> None:
"""@streq keeps its value, quoted or not, with brackets and macros"""
rule_text = f"""
SecRule ARGS "@streq {value}" \\
"id:1006,phase:2,deny"
"""
parsed_rule = parser.process_from_str(rule_text)
assert not isinstance(parsed_rule, dict), parsed_rule
operator = parsed_rule.rules[0].operator
assert operator.streq == value
assert not operator.rx


def test_operator_strmatch() -> None:
"""Test @strmatch operator - Note: may have parsing issues"""
rule_text = """
Expand Down