Skip to content

Commit 42b02c6

Browse files
authored
Merge pull request #3709 from airween/v2/issue-3123-negated-rx-regex-error
fix: negated @rx must not match when the regex fails
2 parents 41721c1 + 93a5998 commit 42b02c6

3 files changed

Lines changed: 176 additions & 2 deletions

File tree

‎apache2/re_operators.c‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,22 @@ static int msre_op_rx_param_init(msre_rule *rule, char **error_msg) {
10501050
return 1; /* OK */
10511051
}
10521052

1053+
/* Flags a regex error in TX:MSC_PCRE_ERROR, where TX:/^MSC_/ rules see it. */
1054+
static int msre_op_rx_set_error_flag(modsec_rec *msr) {
1055+
msc_string *s = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
1056+
1057+
if (s == NULL) return -1;
1058+
s->name = apr_pstrdup(msr->mp, "MSC_PCRE_ERROR");
1059+
if (s->name == NULL) return -1;
1060+
s->name_len = strlen(s->name);
1061+
s->value = apr_pstrdup(msr->mp, "1");
1062+
if (s->value == NULL) return -1;
1063+
s->value_len = 1;
1064+
apr_table_setn(msr->tx_vars, s->name, (void *)s);
1065+
1066+
return 1;
1067+
}
1068+
10531069
static int msre_op_rx_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, char **error_msg) {
10541070
assert(msr != NULL);
10551071
assert(rule != NULL);
@@ -1114,7 +1130,12 @@ static int msre_op_rx_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, c
11141130
if (regex == NULL) {
11151131
*error_msg = apr_psprintf(msr->mp, "Error compiling pattern (offset %d): %s",
11161132
erroffset, errptr);
1117-
return 0;
1133+
msr_log(msr, 3, "%s.", *error_msg);
1134+
if (msre_op_rx_set_error_flag(msr) < 0) return -1;
1135+
1136+
/* A regex error is neither a match nor a mismatch: return
1137+
* what the negation in re.c turns into "no match". */
1138+
return rule->op_negated;
11181139
}
11191140

11201141
#ifdef WITH_PCRE_STUDY
@@ -1214,11 +1235,14 @@ static int msre_op_rx_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, c
12141235

12151236
msr_log(msr, 3, "%s.", *error_msg);
12161237

1217-
return 0; /* No match. */
1238+
/* A regex error is neither a match nor a mismatch: return
1239+
* what the negation in re.c turns into "no match". */
1240+
return rule->op_negated;
12181241
}
12191242
else if (rc < -1) {
12201243
*error_msg = apr_psprintf(msr->mp, "Regex execution failed (%d): %s",
12211244
rc, my_error_msg);
1245+
msre_op_rx_set_error_flag(msr);
12221246
return -1;
12231247
}
12241248

‎modsecurity.conf-recommended‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ SecPcreMatchLimitRecursion 1000
108108
# All of these are prefixed with "MSC_". The following flags currently exist:
109109
#
110110
# MSC_PCRE_LIMITS_EXCEEDED: PCRE match limits were exceeded.
111+
# MSC_PCRE_ERROR: a regular expression failed to compile or execute.
111112
#
112113
SecRule TX:/^MSC_/ "!@streq 0" \
113114
"id:'200005',phase:2,t:none,log,deny,msg:'ModSecurity internal error flagged: %{MATCHED_VAR_NAME}'"
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
### Regex errors with negated @rx
2+
3+
# Neither @rx nor !@rx must match when the regex fails. A failure must
4+
# be visible through the TX:MSC_* flags instead.
5+
6+
{
7+
type => "misc",
8+
comment => "PCRE limits exceeded - negated rx does not match",
9+
conf => qq(
10+
SecRuleEngine On
11+
SecDebugLog $ENV{DEBUG_LOG}
12+
SecDebugLogLevel 9
13+
14+
SecRequestBodyAccess On
15+
SecRequestBodyLimit 1000000
16+
SecRequestBodyInMemoryLimit 1000000
17+
18+
SecPcreMatchLimit 100
19+
SecPcreMatchLimitRecursion 100
20+
21+
SecRule ARGS "!\@rx (?:(.{2,})\\1{32,})" "phase:2,deny,id:500060"
22+
SecRule TX:MSC_PCRE_LIMITS_EXCEEDED "\@streq 1" "phase:2,pass,log,msg:'PCRE limits flagged',id:500061"
23+
),
24+
match_log => {
25+
debug => [ qr/PCRE limits exceeded/, 1 ],
26+
error => [ qr/PCRE limits flagged/, 1 ],
27+
-error => [ qr/Access denied/, 1 ],
28+
},
29+
match_response => {
30+
status => qr/^200$/,
31+
},
32+
request => new HTTP::Request(
33+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/index.html",
34+
[
35+
"Content-Type" => "application/x-www-form-urlencoded",
36+
],
37+
"test=" . (("a" x 64) . "b") x 64 . "d",
38+
),
39+
},
40+
{
41+
type => "misc",
42+
comment => "PCRE limits exceeded - negated rx still checks the remaining targets",
43+
conf => qq(
44+
SecRuleEngine On
45+
SecDebugLog $ENV{DEBUG_LOG}
46+
SecDebugLogLevel 9
47+
48+
SecRequestBodyAccess On
49+
SecRequestBodyLimit 1000000
50+
SecRequestBodyInMemoryLimit 1000000
51+
52+
SecPcreMatchLimit 100
53+
SecPcreMatchLimitRecursion 100
54+
55+
SecRule ARGS "!\@rx (?:(.{2,})\\1{32,})" "phase:2,deny,log,id:500062"
56+
),
57+
match_log => {
58+
debug => [ qr/PCRE limits exceeded/, 1 ],
59+
error => [ qr/against "ARGS:other" required/, 1 ],
60+
-error => [ qr/against "ARGS:test" required/, 1 ],
61+
},
62+
match_response => {
63+
status => qr/^403$/,
64+
},
65+
request => new HTTP::Request(
66+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/index.html",
67+
[
68+
"Content-Type" => "application/x-www-form-urlencoded",
69+
],
70+
"test=" . (("a" x 64) . "b") x 64 . "d" . "&other=x",
71+
),
72+
},
73+
{
74+
type => "misc",
75+
comment => "PCRE limits exceeded - rx does not match",
76+
conf => qq(
77+
SecRuleEngine On
78+
SecDebugLog $ENV{DEBUG_LOG}
79+
SecDebugLogLevel 9
80+
81+
SecRequestBodyAccess On
82+
SecRequestBodyLimit 1000000
83+
SecRequestBodyInMemoryLimit 1000000
84+
85+
SecPcreMatchLimit 100
86+
SecPcreMatchLimitRecursion 100
87+
88+
SecRule ARGS "\@rx (?:(.{2,})\\1{32,})" "phase:2,deny,id:500063"
89+
),
90+
match_log => {
91+
debug => [ qr/PCRE limits exceeded/, 1 ],
92+
-error => [ qr/Access denied/, 1 ],
93+
},
94+
match_response => {
95+
status => qr/^200$/,
96+
},
97+
request => new HTTP::Request(
98+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/index.html",
99+
[
100+
"Content-Type" => "application/x-www-form-urlencoded",
101+
],
102+
"test=" . (("a" x 64) . "b") x 64 . "d",
103+
),
104+
},
105+
{
106+
type => "misc",
107+
comment => "Invalid expanded pattern - negated rx does not match, MSC_PCRE_ERROR is set",
108+
conf => qq(
109+
SecRuleEngine On
110+
SecDebugLog $ENV{DEBUG_LOG}
111+
SecDebugLogLevel 9
112+
113+
SecRule ARGS:x "!\@rx %{ARGS.patt}" "phase:2,deny,id:500064"
114+
SecRule TX:MSC_PCRE_ERROR "\@streq 1" "phase:2,pass,log,msg:'PCRE error flagged',id:500065"
115+
),
116+
match_log => {
117+
debug => [ qr/Error compiling pattern/, 1 ],
118+
error => [ qr/PCRE error flagged/, 1 ],
119+
-error => [ qr/Access denied/, 1 ],
120+
},
121+
match_response => {
122+
status => qr/^200$/,
123+
},
124+
request => new HTTP::Request(
125+
GET => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/index.html?x=a&patt=x%28",
126+
),
127+
},
128+
{
129+
type => "misc",
130+
comment => "Invalid expanded pattern - TX:/^MSC_/ rule blocks the request",
131+
conf => qq(
132+
SecRuleEngine On
133+
SecDebugLog $ENV{DEBUG_LOG}
134+
SecDebugLogLevel 9
135+
136+
SecRule ARGS:x "!\@rx %{ARGS.patt}" "phase:2,deny,id:500066"
137+
SecRule TX:/^MSC_/ "!\@streq 0" "phase:2,t:none,log,deny,msg:'ModSecurity internal error flagged: %{MATCHED_VAR_NAME}',id:500067"
138+
),
139+
match_log => {
140+
error => [ qr/internal error flagged: TX:MSC_PCRE_ERROR/, 1 ],
141+
-error => [ qr/against "ARGS:x" required/, 1 ],
142+
},
143+
match_response => {
144+
status => qr/^403$/,
145+
},
146+
request => new HTTP::Request(
147+
GET => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/index.html?x=a&patt=x%28",
148+
),
149+
},

0 commit comments

Comments
 (0)