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
10 changes: 4 additions & 6 deletions src/operators/rx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ bool Rx::init(const std::string &arg, std::string *error) {

bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
const std::string& input, RuleMessage &ruleMessage) {
Regex *re;
const Regex *re;
std::unique_ptr<Regex> reOwned;

if (m_param.empty() && !m_string->m_containsMacro) {
return true;
}

if (m_string->m_containsMacro) {
std::string eparam(m_string->evaluate(transaction));
re = new Regex(eparam);
reOwned.reset(new Regex(eparam));
re = reOwned.get();
} else {
re = m_re;
}
Expand Down Expand Up @@ -100,10 +102,6 @@ bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
logOffset(ruleMessage, capture.m_offset, capture.m_length);
}

if (m_string->m_containsMacro) {
delete re;
}

if (!captures.empty()) {
return true;
}
Expand Down
10 changes: 4 additions & 6 deletions src/operators/rx_global.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ bool RxGlobal::init(const std::string &arg, std::string *error) {

bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
const std::string& input, RuleMessage &ruleMessage) {
Regex *re;
const Regex *re;
std::unique_ptr<Regex> reOwned;

if (m_param.empty() && !m_string->m_containsMacro) {
return true;
}

if (m_string->m_containsMacro) {
std::string eparam(m_string->evaluate(transaction));
re = new Regex(eparam);
reOwned.reset(new Regex(eparam));
re = reOwned.get();
} else {
re = m_re;
}
Expand Down Expand Up @@ -95,10 +97,6 @@ bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
logOffset(ruleMessage, capture.m_offset, capture.m_length);
}

if (m_string->m_containsMacro) {
delete re;
}

if (captures.size() > 0) {
return true;
}
Expand Down
97 changes: 97 additions & 0 deletions test/test-cases/regression/operator-rx.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,102 @@
"SecRule ARGS:rxtest \"@rx (w+)+$\" \"id:1,phase:1,pass,t:trim,block\"",
"SecRule TX:MSC_PCRE_LIMITS_EXCEEDED \"@streq 1\" \"id:2,phase:1,pass,t:trim,block\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "Testing Operator :: @rx with a macro that expands to a non-compiling pattern",
"client": {
"ip": "200.249.12.31",
"port": 123
},
"server": {
"ip": "200.249.12.31",
"port": 80
},
"request": {
"headers": {
"Host": "localhost",
"User-Agent": "curl/7.38.0",
"Accept": "*/*",
"Content-Length": "0",
"Content-Type": "application/x-www-form-urlencoded"
},
"uri": "/?rxtest=value1",
"method": "HEAD",
"body": [
""
]
},
"response": {
"headers": {
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
"Content-Type": "text/html",
"Content-Length": "8"
},
"body": [
"no need."
]
},
"expected": {
"debug_log": "Error with regular expression",
"http_code": 200
},
"rules": [
"SecRuleEngine On",
"SecAction \"id:1,phase:1,pass,nolog,setvar:tx.pattern=(\"",
"SecRule ARGS:rxtest \"@rx %{tx.pattern}\" \"id:2,phase:1,pass,t:trim,block\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "Testing Operator :: @rx with a macro pattern and PCRE match limits exceeded",
"client": {
"ip": "200.249.12.31",
"port": 123
},
"server": {
"ip": "200.249.12.31",
"port": 80
},
"request": {
"headers": {
"Host": "localhost",
"User-Agent": "curl/7.38.0",
"Accept": "*/*",
"Content-Length": "0",
"Content-Type": "application/x-www-form-urlencoded"
},
"uri": "/?rxtest=wwwwwwwwwwwwwwwwwwwwwowwwwwwwwwww",
"method": "HEAD",
"body": [
""
]
},
"response": {
"headers": {
"Date": "Mon, 13 Jul 2015 20:02:41 GMT",
"Last-Modified": "Sun, 26 Oct 2014 22:33:37 GMT",
"Content-Type": "text/html",
"Content-Length": "8"
},
"body": [
"no need."
]
},
"expected": {
"debug_log": "rx: regex error 'MATCH_LIMIT' for pattern",
"error_log": "Matched \"Operator `StrEq' with parameter `1' against variable `TX:MSC_PCRE_LIMITS_EXCEEDED'",
"http_code": 200
},
"rules": [
"SecRuleEngine On",
"SecPcreMatchLimit 2",
"SecAction \"id:1,phase:1,pass,nolog,setvar:tx.pattern=(w+)+$\"",
"SecRule ARGS:rxtest \"@rx %{tx.pattern}\" \"id:2,phase:1,pass,t:trim,block\"",
"SecRule TX:MSC_PCRE_LIMITS_EXCEEDED \"@streq 1\" \"id:3,phase:1,pass,t:trim,block\""
]
}
]
Loading