Skip to content

Commit 130d375

Browse files
authored
Merge pull request #3700 from fzipi/v3/issue-3123-negated-rx-regex-error
fix: do not let !@rx and !@rxglobal match on a regex error
2 parents effb6c0 + f6a09f1 commit 130d375

4 files changed

Lines changed: 216 additions & 4 deletions

File tree

‎src/operators/rx.cc‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
6969
ms_dbg_a(transaction, 1, "Error with regular expression: \"" + re->pattern + "\"");
7070
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
7171
}
72-
return false;
72+
/* A regex failure is neither a match nor a mismatch. Return the
73+
* value that the negation in Operator::evaluateInternal turns
74+
* into "no match", so that the negated form does not fire on
75+
* errors either (#3123). */
76+
return m_negation;
7377
}
7478

7579
Utils::RegexResult regex_result;
@@ -98,7 +102,11 @@ bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
98102
ms_dbg_a(transaction, 1, "rx: regex error '" + regex_error_str + "' for pattern '" + re->pattern + "'");
99103
}
100104

101-
return false;
105+
/* A regex failure is neither a match nor a mismatch. Return the
106+
* value that the negation in Operator::evaluateInternal turns
107+
* into "no match", so that the negated form does not fire on
108+
* errors either (#3123). */
109+
return m_negation;
102110
}
103111

104112
if (rule && rule->hasCaptureAction() && transaction) {

‎src/operators/rx_global.cc‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
6969
ms_dbg_a(transaction, 1, "Error with regular expression: \"" + re->pattern + "\"");
7070
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
7171
}
72-
return false;
72+
/* A regex failure is neither a match nor a mismatch. Return the
73+
* value that the negation in Operator::evaluateInternal turns
74+
* into "no match", so that the negated form does not fire on
75+
* errors either (#3123). */
76+
return m_negation;
7377
}
7478

7579
Utils::RegexResult regex_result;
@@ -97,7 +101,11 @@ bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
97101
ms_dbg_a(transaction, 1, "rxGlobal: regex error '" + regex_error_str + "' for pattern '" + re->pattern + "'");
98102
}
99103

100-
return false;
104+
/* A regex failure is neither a match nor a mismatch. Return the
105+
* value that the negation in Operator::evaluateInternal turns
106+
* into "no match", so that the negated form does not fire on
107+
* errors either (#3123). */
108+
return m_negation;
101109
}
102110

103111

‎test/test-cases/regression/operator-rx.json‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,5 +467,103 @@
467467
"SecRuleEngine On",
468468
"SecRule ARGS:subject \"@rx %{ARGS:patt}\" \"id:4001,phase:2,deny\""
469469
]
470+
},
471+
{
472+
"enabled": 1,
473+
"version_min": 300000,
474+
"title": "Testing Operator :: !@rx with PCRE match limits exceeded does not match (issue #3123)",
475+
"client": {
476+
"ip": "200.249.12.31",
477+
"port": 123
478+
},
479+
"server": {
480+
"ip": "200.249.12.31",
481+
"port": 80
482+
},
483+
"request": {
484+
"headers": {
485+
"Host": "localhost",
486+
"User-Agent": "curl/7.38.0",
487+
"Accept": "*/*",
488+
"Content-Length": "0",
489+
"Content-Type": "application/x-www-form-urlencoded"
490+
},
491+
"uri": "/?rxtest=wwwwwwwwwwwwwwwwwwwwwowwwwwwwwwww",
492+
"method": "HEAD",
493+
"body": [
494+
""
495+
]
496+
},
497+
"response": {
498+
"headers": {
499+
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
500+
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
501+
"Content-Type": "text/html",
502+
"Content-Length": "8"
503+
},
504+
"body": [
505+
"no need."
506+
]
507+
},
508+
"expected": {
509+
"debug_log": "rx: regex error 'MATCH_LIMIT' for pattern",
510+
"error_log": "Matched \"Operator `StrEq' with parameter `1' against variable `MSC_PCRE_LIMITS_EXCEEDED'",
511+
"http_code": 200
512+
},
513+
"rules": [
514+
"SecRuleEngine On",
515+
"SecPcreMatchLimit 2",
516+
"SecRule ARGS:rxtest \"!@rx (w+)+$\" \"id:1,phase:1,deny,status:403,t:trim\"",
517+
"SecRule MSC_PCRE_LIMITS_EXCEEDED \"@streq 1\" \"id:2,phase:1,pass,t:trim,block\""
518+
]
519+
}
520+
,
521+
{
522+
"enabled": 1,
523+
"version_min": 300000,
524+
"title": "Testing Operator :: !@rx with invalid macro-expanded pattern does not match (issue #3123)",
525+
"client": {
526+
"ip": "200.249.12.31",
527+
"port": 123
528+
},
529+
"server": {
530+
"ip": "200.249.12.31",
531+
"port": 80
532+
},
533+
"request": {
534+
"headers": {
535+
"Host": "localhost",
536+
"User-Agent": "curl/7.38.0",
537+
"Accept": "*/*",
538+
"Content-Length": "27",
539+
"Content-Type": "application/x-www-form-urlencoded"
540+
},
541+
"uri": "/?patt=%28",
542+
"method": "POST",
543+
"body": [
544+
"subject=wwww"
545+
]
546+
},
547+
"response": {
548+
"headers": {
549+
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
550+
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
551+
"Content-Type": "text/html",
552+
"Content-Length": "8"
553+
},
554+
"body": [
555+
"no need."
556+
]
557+
},
558+
"expected": {
559+
"debug_log": "Error with regular expression: \"\\(\"",
560+
"error_log": "Matched \"Operator `StrEq' with parameter `1' against variable `MSC_PCRE_ERROR'",
561+
"http_code": 200
562+
},
563+
"rules": [
564+
"SecRuleEngine On",
565+
"SecRule ARGS:subject \"!@rx %{ARGS:patt}\" \"id:4001,phase:2,deny,status:403\"",
566+
"SecRule MSC_PCRE_ERROR \"@streq 1\" \"id:4002,phase:2,pass,log\""
567+
]
470568
}
471569
]

‎test/test-cases/regression/operator-rxGlobal.json‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,5 +273,103 @@
273273
"SecRuleEngine On",
274274
"SecRule ARGS:subject \"@rxGlobal %{ARGS:patt}\" \"id:4001,phase:2,deny\""
275275
]
276+
},
277+
{
278+
"enabled": 1,
279+
"version_min": 300000,
280+
"title": "Testing Operator :: !@rxGlobal with PCRE match limits exceeded does not match (issue #3123)",
281+
"client": {
282+
"ip": "200.249.12.31",
283+
"port": 123
284+
},
285+
"server": {
286+
"ip": "200.249.12.31",
287+
"port": 80
288+
},
289+
"request": {
290+
"headers": {
291+
"Host": "localhost",
292+
"User-Agent": "curl/7.38.0",
293+
"Accept": "*/*",
294+
"Content-Length": "0",
295+
"Content-Type": "application/x-www-form-urlencoded"
296+
},
297+
"uri": "/?rxtest=wwwwwwwwwwwwwwwwwwwwwowwwwwwwwwww",
298+
"method": "HEAD",
299+
"body": [
300+
""
301+
]
302+
},
303+
"response": {
304+
"headers": {
305+
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
306+
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
307+
"Content-Type": "text/html",
308+
"Content-Length": "8"
309+
},
310+
"body": [
311+
"no need."
312+
]
313+
},
314+
"expected": {
315+
"debug_log": "rxGlobal: regex error 'MATCH_LIMIT' for pattern",
316+
"error_log": "Matched \"Operator `StrEq' with parameter `1' against variable `MSC_PCRE_LIMITS_EXCEEDED'",
317+
"http_code": 200
318+
},
319+
"rules": [
320+
"SecRuleEngine On",
321+
"SecPcreMatchLimit 2",
322+
"SecRule ARGS:rxtest \"!@rxGlobal (w+)+$\" \"id:1,phase:1,deny,status:403,t:trim\"",
323+
"SecRule MSC_PCRE_LIMITS_EXCEEDED \"@streq 1\" \"id:2,phase:1,pass,t:trim,block\""
324+
]
325+
}
326+
,
327+
{
328+
"enabled": 1,
329+
"version_min": 300000,
330+
"title": "Testing Operator :: !@rxGlobal with invalid macro-expanded pattern does not match (issue #3123)",
331+
"client": {
332+
"ip": "200.249.12.31",
333+
"port": 123
334+
},
335+
"server": {
336+
"ip": "200.249.12.31",
337+
"port": 80
338+
},
339+
"request": {
340+
"headers": {
341+
"Host": "localhost",
342+
"User-Agent": "curl/7.38.0",
343+
"Accept": "*/*",
344+
"Content-Length": "27",
345+
"Content-Type": "application/x-www-form-urlencoded"
346+
},
347+
"uri": "/?patt=%28",
348+
"method": "POST",
349+
"body": [
350+
"subject=wwww"
351+
]
352+
},
353+
"response": {
354+
"headers": {
355+
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
356+
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
357+
"Content-Type": "text/html",
358+
"Content-Length": "8"
359+
},
360+
"body": [
361+
"no need."
362+
]
363+
},
364+
"expected": {
365+
"debug_log": "Error with regular expression: \"\\(\"",
366+
"error_log": "Matched \"Operator `StrEq' with parameter `1' against variable `MSC_PCRE_ERROR'",
367+
"http_code": 200
368+
},
369+
"rules": [
370+
"SecRuleEngine On",
371+
"SecRule ARGS:subject \"!@rxGlobal %{ARGS:patt}\" \"id:4001,phase:2,deny,status:403\"",
372+
"SecRule MSC_PCRE_ERROR \"@streq 1\" \"id:4002,phase:2,pass,log\""
373+
]
276374
}
277375
]

0 commit comments

Comments
 (0)