Skip to content

Commit 286f87b

Browse files
committed
refactor: share the regex error reporting of @rx and @rxglobal
Move the duplicated error handling of both operators into two static helpers in Operator: reportRegexCompileError() for an invalid pattern, and reportRegexSearchError() for a failed search. They set the standalone MSC_PCRE_* variables and the TX:MSC_PCRE_* flags, and keep the debug log messages unchanged. This resolves the "DRY regex error reporting" FIXME. The helpers are kept out of Utils::Regex, which doesn't know about transactions and is also used outside of operators, and out of Transaction, which is part of the public API. No behavior change. The log message is now built without a temporary variable, so the cppcheck suppressions for rx.cc and rx_global.cc, which only covered that variable in NO_LOGS builds, are no longer needed.
1 parent 4096c56 commit 286f87b

5 files changed

Lines changed: 56 additions & 50 deletions

File tree

‎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 & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +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-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_ERROR", "1");
72-
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_ERROR to 1");
73-
}
68+
reportRegexCompileError(transaction, *re);
7469
/* A regex failure is neither a match nor a mismatch. Return the
7570
* value that the negation in Operator::evaluateInternal turns
7671
* into "no match", so that the negated form does not fire on
@@ -88,23 +83,8 @@ bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
8883
regex_result = re->searchOneMatch(input, captures);
8984
}
9085

91-
// FIXME: DRY regex error reporting. This logic is currently duplicated in other operators.
9286
if (regex_result != Utils::RegexResult::Ok) {
93-
if (transaction) {
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-
std::string regex_error_str = "OTHER";
99-
if (regex_result == Utils::RegexResult::ErrorMatchLimit) {
100-
regex_error_str = "MATCH_LIMIT";
101-
transaction->m_variableMscPcreLimitsExceeded.set("1", transaction->m_variableOffset);
102-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_LIMITS_EXCEEDED", "1");
103-
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_LIMITS_EXCEEDED to 1");
104-
}
105-
106-
ms_dbg_a(transaction, 1, "rx: regex error '" + regex_error_str + "' for pattern '" + re->pattern + "'");
107-
}
87+
reportRegexSearchError(transaction, *re, regex_result, "rx");
10888

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

‎src/operators/rx_global.cc‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +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-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_ERROR", "1");
72-
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_ERROR to 1");
73-
}
68+
reportRegexCompileError(transaction, *re);
7469
/* A regex failure is neither a match nor a mismatch. Return the
7570
* value that the negation in Operator::evaluateInternal turns
7671
* into "no match", so that the negated form does not fire on
@@ -87,23 +82,8 @@ bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
8782
regex_result = re->searchGlobal(input, captures);
8883
}
8984

90-
// FIXME: DRY regex error reporting. This logic is currently duplicated in other operators.
9185
if (regex_result != Utils::RegexResult::Ok) {
92-
if (transaction) {
93-
transaction->m_variableMscPcreError.set("1", transaction->m_variableOffset);
94-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("MSC_PCRE_ERROR", "1");
95-
ms_dbg_a(transaction, 7, "Set TX.MSC_PCRE_ERROR to 1");
96-
97-
std::string regex_error_str = "OTHER";
98-
if (regex_result == Utils::RegexResult::ErrorMatchLimit) {
99-
regex_error_str = "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, "rxGlobal: regex error '" + regex_error_str + "' for pattern '" + re->pattern + "'");
106-
}
86+
reportRegexSearchError(transaction, *re, regex_result, "rxGlobal");
10787

10888
/* A regex failure is neither a match nor a mismatch. Return the
10989
* 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

0 commit comments

Comments
 (0)