diff --git a/src/rule_with_operator.cc b/src/rule_with_operator.cc index c91ff47ce..48b7e54c2 100644 --- a/src/rule_with_operator.cc +++ b/src/rule_with_operator.cc @@ -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(b)) { + exclusion->push_back(vme->m_base.get()); + } else { + addition->push_back(b); + } +} + + template 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(b)) { - exclusion->push_back(vme->m_base.get()); - } else { - addition->push_back(b); - } + addVariableException(exclusion, addition, v.get()); } } } @@ -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()); + } } diff --git a/test/test-cases/regression/config-update-target-by-id.json b/test/test-cases/regression/config-update-target-by-id.json index a8bc0ea05..2b08e6ff4 100644 --- a/test/test-cases/regression/config-update-target-by-id.json +++ b/test/test-cases/regression/config-update-target-by-id.json @@ -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\"" + ] } ]