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
31 changes: 20 additions & 11 deletions src/rule_with_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
#include <list>
#include <utility>
Expand All @@ -45,6 +46,21 @@

namespace modsecurity {

namespace {

// Collection keys are matched without regard to case (see MyEqual in
// anchored_set_variable.h), so target exclusions have to be too.
bool equalsIgnoreCase(const std::string &a, const std::string &b) {

Check warning on line 53 in src/rule_with_operator.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this const reference to "std::string" by a "std::string_view".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIDy5lEIgZiFB_G8F5t&open=AaIDy5lEIgZiFB_G8F5t&pullRequest=3773

Check warning on line 53 in src/rule_with_operator.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this const reference to "std::string" by a "std::string_view".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIDy5lEIgZiFB_G8F5u&open=AaIDy5lEIgZiFB_G8F5u&pullRequest=3773
return a.size() == b.size() &&
std::equal(a.begin(), a.end(), b.begin(),
[](char x, char y) {
return std::tolower(static_cast<unsigned char>(x)) ==
std::tolower(static_cast<unsigned char>(y));
});
}

} // namespace

using operators::Operator;
using actions::Action;
using variables::Variable;
Expand Down Expand Up @@ -166,15 +182,8 @@
if (std::find_if(trans->m_ruleRemoveTargetById.begin(),
trans->m_ruleRemoveTargetById.end(),
[&, variable, this](const auto &m) -> bool {
const auto& str1 = m.second;
const auto& str2 = *variable->m_fullName.get();
return m.first == m_ruleId &&
str1.size() == str2.size() &&
std::equal(str1.begin(), str1.end(), str2.begin(),
[](char a, char b) {
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
}); // end-of std::equal
equalsIgnoreCase(m.second, *variable->m_fullName.get());
}) != trans->m_ruleRemoveTargetById.end()) {
continue;
}
Expand All @@ -183,7 +192,7 @@
[&, variable, trans, this](
const auto &m) -> bool {
return containsTag(m.first, trans)
&& m.second == *variable->m_fullName.get();
&& equalsIgnoreCase(m.second, *variable->m_fullName.get());
}) != trans->m_ruleRemoveTargetByTag.end()) {
continue;
}
Expand Down Expand Up @@ -266,7 +275,7 @@
std::find_if(trans->m_ruleRemoveTargetById.begin(),
trans->m_ruleRemoveTargetById.end(),
[&, v, this](const auto &m) -> bool {
return m.first == m_ruleId && m.second == v->getKeyWithCollection();
return m.first == m_ruleId && equalsIgnoreCase(m.second, v->getKeyWithCollection());
}) != trans->m_ruleRemoveTargetById.end()
) {
delete v;
Expand All @@ -277,7 +286,7 @@
std::find_if(trans->m_ruleRemoveTargetByTag.begin(),
trans->m_ruleRemoveTargetByTag.end(),
[&, v, trans, this](const auto &m) -> bool {
return containsTag(m.first, trans) && m.second == v->getKeyWithCollection();
return containsTag(m.first, trans) && equalsIgnoreCase(m.second, v->getKeyWithCollection());
}) != trans->m_ruleRemoveTargetByTag.end()
) {
delete v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,70 @@
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:9002100,phase:2,pass,ctl:ruleRemoveTargetById=1;ARGS:foo'bar\"",
"SecRule ARGS \"@contains attack\" \"id:1,phase:3,status:403,block,deny\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "Testing CtlRuleRemoveTargetById (6): lowercase exclusion, rule reads all REQUEST_HEADERS",
"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": "*/*",
"Referer": "This is an attack"
},
"uri": "/",
"method": "GET",
"body": [ ]
},
"expected": {
"http_code": 200
},
"rules": [
"SecRuleEngine On",
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:1,phase:1,pass,t:none,ctl:ruleRemoveTargetById=2;REQUEST_HEADERS:referer\"",
"SecRule REQUEST_HEADERS \"@contains attack\" \"id:2,phase:1,deny,t:none,log\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "Testing CtlRuleRemoveTargetById (7): other headers are still checked",
"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": "*/*",
"Referer": "This is an attack",
"X-Other": "another attack"
},
"uri": "/",
"method": "GET",
"body": [ ]
},
"expected": {
"http_code": 403
},
"rules": [
"SecRuleEngine On",
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:1,phase:1,pass,t:none,ctl:ruleRemoveTargetById=2;REQUEST_HEADERS:referer\"",
"SecRule REQUEST_HEADERS \"@contains attack\" \"id:2,phase:1,deny,t:none,log\""
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,69 @@
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:9002100,phase:2,pass,ctl:ruleRemoveTargetByTag=CRS;ARGS:foo'bar\"",
"SecRule ARGS \"@contains attack\" \"id:1,phase:3,status:403,block,deny,tag:'CRS'\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "Testing CtlRuleRemoveTargetByTag: lowercase exclusion, rule reads all REQUEST_HEADERS",
"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": "*/*",
"Referer": "This is an attack"
},
"uri": "/",
"method": "GET",
"body": [ ]
},
"expected": {
"http_code": 200
},
"rules": [
"SecRuleEngine On",
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:1,phase:1,pass,t:none,ctl:ruleRemoveTargetByTag=CRS;REQUEST_HEADERS:referer\"",
"SecRule REQUEST_HEADERS \"@contains attack\" \"id:2,phase:1,deny,t:none,log,tag:'CRS'\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "Testing CtlRuleRemoveTargetByTag: lowercase exclusion, rule reads REQUEST_HEADERS:Referer",
"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": "*/*",
"Referer": "This is an attack"
},
"uri": "/",
"method": "GET",
"body": [ ]
},
"expected": {
"http_code": 200
},
"rules": [
"SecRuleEngine On",
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:1,phase:1,pass,t:none,ctl:ruleRemoveTargetByTag=CRS;REQUEST_HEADERS:referer\"",
"SecRule REQUEST_HEADERS:Referer \"@contains attack\" \"id:2,phase:1,deny,t:none,log,tag:'CRS'\""
]
}
]