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
26 changes: 17 additions & 9 deletions src/rule_with_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,23 @@ bool RuleWithOperator::executeOperatorAt(Transaction *trans, const std::string &
}


static 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);
}
}


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 +152,12 @@ void RuleWithOperator::getVariablesExceptions(Transaction &t,
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 [first, last] = t.m_rules->m_exceptions
.m_variable_update_target_by_id.equal_range(m_ruleId);
for (auto it = first; it != last; ++it) {
addVariableException(exclusion, addition, it->second.get());
}
}


Expand Down
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\""
]
}
]