Skip to content
Closed
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
83 changes: 61 additions & 22 deletions src/rule_with_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cstring>
#include <list>
#include <utility>
#include <vector>
#include <memory>

#include "modsecurity/rules_set.h"
Expand Down Expand Up @@ -120,18 +121,43 @@
}


namespace {

void addVariableException(variables::Variables *exclusion,
variables::Variables *addition, variables::Variable *b) {
if (auto vme = dynamic_cast<variables::VariableModificatorExclusion*>(b)) {
exclusion->push_back(vme->m_base.get());
} else {
addition->push_back(b);
}
}


/*
* Targets removed from rule `ruleId` by ctl:ruleRemoveTargetById, looked up
* once per rule evaluation instead of once per variable value: the list holds
* every entry of the transaction (all rules), and most rules have none.
*/
template<typename RuleId>
void removedTargetsById(const Transaction *trans, RuleId ruleId,
std::vector<const std::string *> *out) {
for (const auto &[id, target] : trans->m_ruleRemoveTargetById) {
if (id == ruleId) {
out->push_back(&target);
}
}
}

} // namespace


template<typename MapType, typename Operation>
void getVariablesExceptionsHelper(
variables::Variables *exclusion, variables::Variables *addition,
const MapType &map, Operation op) {
for (const auto &[x, v] : map) {
if (op(x)) {
auto b = v.get();
if (auto vme = dynamic_cast<variables::VariableModificatorExclusion*>(b)) {
exclusion->push_back(vme->m_base.get());
} else {
addition->push_back(b);
}
addVariableException(exclusion, addition, v.get());
}
}
}
Expand All @@ -147,9 +173,12 @@
t.m_rules->m_exceptions.m_variable_update_target_by_msg,
[this, &t](const auto &msg) { return containsMsg(*msg.get(), &t); });

getVariablesExceptionsHelper(exclusion, addition,
t.m_rules->m_exceptions.m_variable_update_target_by_id,
[this](const auto &id) { return m_ruleId == id; });
// keyed by rule id: look the rule up instead of walking the whole map
const auto range = t.m_rules->m_exceptions.m_variable_update_target_by_id
.equal_range(m_ruleId);

Check warning on line 178 in src/rule_with_operator.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this declaration by a structured binding declaration.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIIvihq7IMKGlXpl-dA&open=AaIIvihq7IMKGlXpl-dA&pullRequest=3776
for (auto it = range.first; it != range.second; ++it) {
addVariableException(exclusion, addition, it->second.get());
}
}


Expand All @@ -158,24 +187,24 @@
variables::Variables addition;
getVariablesExceptions(*trans, exclusion, &addition); // cppcheck-suppress ctunullpointer

std::vector<const std::string *> removedById;
removedTargetsById(trans, m_ruleId, &removedById);

for (std::size_t i = 0; i < m_variables->size(); i++) {
Variable *variable = m_variables->at(i);
if (exclusion->contains(variable)) {
continue;
}
if (std::find_if(trans->m_ruleRemoveTargetById.begin(),
trans->m_ruleRemoveTargetById.end(),
[&, variable, this](const auto &m) -> bool {
const auto& str1 = m.second;
if (std::any_of(removedById.begin(), removedById.end(),
[variable](const std::string *str1) -> bool {

Check warning on line 199 in src/rule_with_operator.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the redundant return type of this lambda.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIIvihq7IMKGlXpl-dB&open=AaIIvihq7IMKGlXpl-dB&pullRequest=3776
const auto& str2 = *variable->m_fullName.get();
return m.first == m_ruleId &&
str1.size() == str2.size() &&
std::equal(str1.begin(), str1.end(), str2.begin(),
return 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
}) != trans->m_ruleRemoveTargetById.end()) {
})) {
continue;
}
if (std::find_if(trans->m_ruleRemoveTargetByTag.begin(),
Expand Down Expand Up @@ -252,6 +281,12 @@

getFinalVars(&vars, &exclusion, trans);

std::vector<const std::string *> removedById;
removedTargetsById(trans, m_ruleId, &removedById);
// a ctl run while this rule is evaluated (its own match actions) is seen
// by the next values, as with the list walk this replaces
std::size_t removedByIdSeen = trans->m_ruleRemoveTargetById.size();

for (auto &var : vars) {
std::vector<const VariableValue *> e;
if (!var) {
Expand All @@ -262,12 +297,16 @@
const std::string &value = v->getValue();
const std::string &key = v->getKeyWithCollection();

if (trans->m_ruleRemoveTargetById.size() != removedByIdSeen) {
removedById.clear();
removedTargetsById(trans, m_ruleId, &removedById);
removedByIdSeen = trans->m_ruleRemoveTargetById.size();
}
if (exclusion.contains(v) ||
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();
}) != trans->m_ruleRemoveTargetById.end()
std::any_of(removedById.begin(), removedById.end(),
[&key](const std::string *target) -> bool {

Check warning on line 307 in src/rule_with_operator.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the redundant return type of this lambda.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIIvihq7IMKGlXpl-dC&open=AaIIvihq7IMKGlXpl-dC&pullRequest=3776
return *target == key;
})
) {
delete v;
v = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,95 @@
"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: several entries for several rules, all of this rule's targets removed",
"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": "*/*",
"Cookie": "PHPSESSID=rAAAAAAA2t5uvjq435r4q7ib3vtdjq120",
"Content-Type": "text/xml",
"Referer": "This is an attack",
"Content-Length": "0"
},
"uri": "/index.html?a=attack&b=attack&c=ok",
"method": "GET",
"body": [
""
]
},
"response": {
"headers": {
"Content-Length": "0"
},
"body": [
""
]
},
"expected": {
"http_code": 200
},
"rules": [
"SecRuleEngine On",
"SecAction \"id:1,phase:1,pass,nolog,ctl:ruleRemoveTargetById=2;ARGS:c,ctl:ruleRemoveTargetById=3;ARGS:a,ctl:ruleRemoveTargetById=3;ARGS:b,ctl:ruleRemoveTargetById=4;ARGS:c\"",
"SecRule ARGS \"@contains attack\" \"id:3,phase:2,t:none,status:403,deny\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "Testing CtlRuleRemoveTargetById: several entries for several rules, other rules' targets kept",
"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": "*/*",
"Cookie": "PHPSESSID=rAAAAAAA2t5uvjq435r4q7ib3vtdjq120",
"Content-Type": "text/xml",
"Referer": "This is an attack",
"Content-Length": "0"
},
"uri": "/index.html?a=attack&b=attack&c=attack",
"method": "GET",
"body": [
""
]
},
"response": {
"headers": {
"Content-Length": "0"
},
"body": [
""
]
},
"expected": {
"http_code": 403
},
"rules": [
"SecRuleEngine On",
"SecAction \"id:1,phase:1,pass,nolog,ctl:ruleRemoveTargetById=2;ARGS:c,ctl:ruleRemoveTargetById=3;ARGS:a,ctl:ruleRemoveTargetById=3;ARGS:b,ctl:ruleRemoveTargetById=4;ARGS:c\"",
"SecRule ARGS \"@contains attack\" \"id:3,phase:2,t:none,status:403,deny\""
]
}
]
94 changes: 94 additions & 0 deletions test/test-cases/regression/config-update-target-by-id.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,99 @@
"SecRuleUpdateTargetById 1 \"!ARGS:/xxx/\"",
"SecRule ARGS_NAMES \"@contains yyy\" \"id:1,phase:2,deny,status:403\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "SecRuleUpdateTargetById - several rules, this rule's target removed",
"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"
},
"uri": "/?a=attack",
"method": "GET",
"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": {
"http_code": 200
},
"rules": [
"SecRuleEngine On",
"SecRuleUpdateTargetById 2 !ARGS:c",
"SecRuleUpdateTargetById 3 !ARGS:a",
"SecRuleUpdateTargetById 4 !ARGS:c",
"SecRule ARGS \"@contains attack\" \"id:3,phase:2,t:none,status:403,deny\""
]
},
{
"enabled": 1,
"version_min": 300000,
"title": "SecRuleUpdateTargetById - several rules, other rules' targets kept",
"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"
},
"uri": "/?c=attack",
"method": "GET",
"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": {
"http_code": 403
},
"rules": [
"SecRuleEngine On",
"SecRuleUpdateTargetById 2 !ARGS:c",
"SecRuleUpdateTargetById 3 !ARGS:a",
"SecRuleUpdateTargetById 4 !ARGS:c",
"SecRule ARGS \"@contains attack\" \"id:3,phase:2,t:none,status:403,deny\""
]
}
]