Skip to content

Commit a44c779

Browse files
authored
Merge pull request #3711 from airween/v3/msc-pcre-error-tx
fix: flag regex errors of @rx/@rxglobal in TX:MSC_PCRE_ERROR
2 parents 130d375 + 286f87b commit a44c779

8 files changed

Lines changed: 153 additions & 42 deletions

File tree

‎modsecurity.conf-recommended‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ SecPcreMatchLimitRecursion 1000
155155
# All of these are prefixed with "MSC_". The following flags currently exist:
156156
#
157157
# MSC_PCRE_LIMITS_EXCEEDED: PCRE match limits were exceeded.
158+
# MSC_PCRE_ERROR: a regular expression failed to compile or execute.
158159
#
159160
SecRule TX:/^MSC_/ "!@streq 0" \
160161
"id:'200005',phase:2,t:none,log,deny,msg:'ModSecurity internal error flagged: %{MATCHED_VAR_NAME}'"

‎src/operators/operator.cc‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "modsecurity/transaction.h"
2323
#include "src/run_time_string.h"
24+
#include "src/utils/regex.h"
2425
#include "src/utils/string.h"
2526
#include "src/operators/begins_with.h"
2627
#include "src/operators/contains.h"
@@ -70,6 +71,43 @@ namespace modsecurity {
7071
namespace operators {
7172

7273

74+
void Operator::reportRegexCompileError(Transaction *transaction,
75+
const Utils::Regex &re) {
76+
if (transaction == nullptr) {
77+
return;
78+
}
79+
80+
ms_dbg_a(transaction, 1, "Error with regular expression: \"" + re.pattern + "\"");
81+
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
82+
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_ERROR", "1");
83+
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_ERROR to 1");
84+
}
85+
86+
87+
void Operator::reportRegexSearchError(Transaction *transaction,
88+
const Utils::Regex &re, Utils::RegexResult result,
89+
const std::string &opName) {
90+
if (transaction == nullptr) {
91+
return;
92+
}
93+
94+
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
95+
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_ERROR", "1");
96+
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_ERROR to 1");
97+
98+
const bool match_limit = (result == Utils::RegexResult::ErrorMatchLimit);
99+
if (match_limit) {
100+
transaction->m_variableMscPcreLimitsExceeded.set("1", transaction->m_variableOffset);
101+
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_LIMITS_EXCEEDED", "1");
102+
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_LIMITS_EXCEEDED to 1");
103+
}
104+
105+
ms_dbg_a(transaction, 1, opName + ": regex error '"
106+
+ (match_limit ? "MATCH_LIMIT" : "OTHER")
107+
+ "' for pattern '" + re.pattern + "'");
108+
}
109+
110+
73111
bool Operator::evaluateInternal(Transaction *transaction,
74112
RuleWithActions *rule, const std::string& a, RuleMessage &ruleMessage) {
75113
bool res = evaluate(transaction, rule, a, ruleMessage);

‎src/operators/operator.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include "src/run_time_string.h"
2727

2828
namespace modsecurity {
29+
namespace Utils {
30+
class Regex;
31+
enum class RegexResult;
32+
} // namespace Utils
33+
2934
namespace operators {
3035

3136
class Operator {
@@ -140,6 +145,15 @@ class Operator {
140145
std::string m_param;
141146
std::unique_ptr<RunTimeString> m_string;
142147
bool m_couldContainsMacro;
148+
149+
protected:
150+
/* Regex error reporting shared by @rx and @rxGlobal: sets the
151+
* standalone MSC_PCRE_* variables and the TX:MSC_PCRE_* flags. */
152+
static void reportRegexCompileError(Transaction *transaction,
153+
const Utils::Regex &re);
154+
static void reportRegexSearchError(Transaction *transaction,
155+
const Utils::Regex &re, Utils::RegexResult result,
156+
const std::string &opName);
143157
};
144158

145159
} // namespace operators

‎src/operators/rx.cc‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
6565
}
6666

6767
if (re->hasError()) {
68-
if (transaction) {
69-
ms_dbg_a(transaction, 1, "Error with regular expression: \"" + re->pattern + "\"");
70-
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
71-
}
68+
reportRegexCompileError(transaction, *re);
7269
/* A regex failure is neither a match nor a mismatch. Return the
7370
* value that the negation in Operator::evaluateInternal turns
7471
* into "no match", so that the negated form does not fire on
@@ -86,21 +83,8 @@ bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
8683
regex_result = re->searchOneMatch(input, captures);
8784
}
8885

89-
// FIXME: DRY regex error reporting. This logic is currently duplicated in other operators.
9086
if (regex_result != Utils::RegexResult::Ok) {
91-
if (transaction) {
92-
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
93-
94-
std::string regex_error_str = "OTHER";
95-
if (regex_result == Utils::RegexResult::ErrorMatchLimit) {
96-
regex_error_str = "MATCH_LIMIT";
97-
transaction->m_variableMscPcreLimitsExceeded.set("1", transaction->m_variableOffset);
98-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_LIMITS_EXCEEDED", "1");
99-
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_LIMITS_EXCEEDED to 1");
100-
}
101-
102-
ms_dbg_a(transaction, 1, "rx: regex error '" + regex_error_str + "' for pattern '" + re->pattern + "'");
103-
}
87+
reportRegexSearchError(transaction, *re, regex_result, "rx");
10488

10589
/* A regex failure is neither a match nor a mismatch. Return the
10690
* value that the negation in Operator::evaluateInternal turns

‎src/operators/rx_global.cc‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
6565
}
6666

6767
if (re->hasError()) {
68-
if (transaction) {
69-
ms_dbg_a(transaction, 1, "Error with regular expression: \"" + re->pattern + "\"");
70-
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
71-
}
68+
reportRegexCompileError(transaction, *re);
7269
/* A regex failure is neither a match nor a mismatch. Return the
7370
* value that the negation in Operator::evaluateInternal turns
7471
* into "no match", so that the negated form does not fire on
@@ -85,21 +82,8 @@ bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
8582
regex_result = re->searchGlobal(input, captures);
8683
}
8784

88-
// FIXME: DRY regex error reporting. This logic is currently duplicated in other operators.
8985
if (regex_result != Utils::RegexResult::Ok) {
90-
if (transaction) {
91-
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
92-
93-
std::string regex_error_str = "OTHER";
94-
if (regex_result == Utils::RegexResult::ErrorMatchLimit) {
95-
regex_error_str = "MATCH_LIMIT";
96-
transaction->m_variableMscPcreLimitsExceeded.set("1", transaction->m_variableOffset);
97-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_LIMITS_EXCEEDED", "1");
98-
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_LIMITS_EXCEEDED to 1");
99-
}
100-
101-
ms_dbg_a(transaction, 1, "rxGlobal: regex error '" + regex_error_str + "' for pattern '" + re->pattern + "'");
102-
}
86+
reportRegexSearchError(transaction, *re, regex_result, "rxGlobal");
10387

10488
/* A regex failure is neither a match nor a mismatch. Return the
10589
* value that the negation in Operator::evaluateInternal turns

‎test/cppcheck_suppressions.txt‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ shiftNegative:src/utils/msc_tree.cc
2222
//
2323
// ModSecurity v3 code...
2424
//
25-
variableScope:src/operators/rx.cc
26-
variableScope:src/operators/rx_global.cc
27-
2825
noExplicitConstructor:seclang-parser.hh
2926
constParameter:seclang-parser.hh
3027
accessMoved:seclang-parser.hh
@@ -34,9 +31,6 @@ constVariableReference:seclang-parser.hh
3431
uninitMemberVar:seclang-parser.hh
3532

3633

37-
unreadVariable:src/operators/rx.cc
38-
unreadVariable:src/operators/rx_global.cc
39-
4034
noExplicitConstructor:src/collection/backend/collection_data.h
4135
stlIfStrFind:src/collection/backend/collection_data.cc
4236

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,5 +565,53 @@
565565
"SecRule ARGS:subject \"!@rx %{ARGS:patt}\" \"id:4001,phase:2,deny,status:403\"",
566566
"SecRule MSC_PCRE_ERROR \"@streq 1\" \"id:4002,phase:2,pass,log\""
567567
]
568+
},
569+
{
570+
"enabled": 1,
571+
"version_min": 300000,
572+
"title": "Testing Operator :: !@rx with invalid macro-expanded pattern is flagged in TX for TX:/^MSC_/",
573+
"client": {
574+
"ip": "200.249.12.31",
575+
"port": 123
576+
},
577+
"server": {
578+
"ip": "200.249.12.31",
579+
"port": 80
580+
},
581+
"request": {
582+
"headers": {
583+
"Host": "localhost",
584+
"User-Agent": "curl/7.38.0",
585+
"Accept": "*/*",
586+
"Content-Length": "27",
587+
"Content-Type": "application/x-www-form-urlencoded"
588+
},
589+
"uri": "/?patt=%28",
590+
"method": "POST",
591+
"body": [
592+
"subject=wwww"
593+
]
594+
},
595+
"response": {
596+
"headers": {
597+
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
598+
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
599+
"Content-Type": "text/html",
600+
"Content-Length": "8"
601+
},
602+
"body": [
603+
"no need."
604+
]
605+
},
606+
"expected": {
607+
"debug_log": "Set TX.MSC_PCRE_ERROR to 1",
608+
"error_log": "ModSecurity internal error flagged: TX:MSC_PCRE_ERROR",
609+
"http_code": 409
610+
},
611+
"rules": [
612+
"SecRuleEngine On",
613+
"SecRule ARGS:subject \"!@rx %{ARGS:patt}\" \"id:4001,phase:2,deny,status:403\"",
614+
"SecRule TX:/^MSC_/ \"!@streq 0\" \"id:4002,phase:2,t:none,log,deny,status:409,msg:'ModSecurity internal error flagged: %{MATCHED_VAR_NAME}'\""
615+
]
568616
}
569617
]

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,5 +371,53 @@
371371
"SecRule ARGS:subject \"!@rxGlobal %{ARGS:patt}\" \"id:4001,phase:2,deny,status:403\"",
372372
"SecRule MSC_PCRE_ERROR \"@streq 1\" \"id:4002,phase:2,pass,log\""
373373
]
374+
},
375+
{
376+
"enabled": 1,
377+
"version_min": 300000,
378+
"title": "Testing Operator :: !@rxGlobal with invalid macro-expanded pattern is flagged in TX for TX:/^MSC_/",
379+
"client": {
380+
"ip": "200.249.12.31",
381+
"port": 123
382+
},
383+
"server": {
384+
"ip": "200.249.12.31",
385+
"port": 80
386+
},
387+
"request": {
388+
"headers": {
389+
"Host": "localhost",
390+
"User-Agent": "curl/7.38.0",
391+
"Accept": "*/*",
392+
"Content-Length": "27",
393+
"Content-Type": "application/x-www-form-urlencoded"
394+
},
395+
"uri": "/?patt=%28",
396+
"method": "POST",
397+
"body": [
398+
"subject=wwww"
399+
]
400+
},
401+
"response": {
402+
"headers": {
403+
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
404+
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
405+
"Content-Type": "text/html",
406+
"Content-Length": "8"
407+
},
408+
"body": [
409+
"no need."
410+
]
411+
},
412+
"expected": {
413+
"debug_log": "Set TX.MSC_PCRE_ERROR to 1",
414+
"error_log": "ModSecurity internal error flagged: TX:MSC_PCRE_ERROR",
415+
"http_code": 409
416+
},
417+
"rules": [
418+
"SecRuleEngine On",
419+
"SecRule ARGS:subject \"!@rxGlobal %{ARGS:patt}\" \"id:4001,phase:2,deny,status:403\"",
420+
"SecRule TX:/^MSC_/ \"!@streq 0\" \"id:4002,phase:2,t:none,log,deny,status:409,msg:'ModSecurity internal error flagged: %{MATCHED_VAR_NAME}'\""
421+
]
374422
}
375423
]

0 commit comments

Comments
 (0)