diff --git a/src/actions/expire_var.cc b/src/actions/expire_var.cc index ffc89f477..2ae38d463 100644 --- a/src/actions/expire_var.cc +++ b/src/actions/expire_var.cc @@ -34,7 +34,7 @@ namespace actions { bool ExpireVar::evaluate(RuleWithActions *rule, Transaction *t) { - std::string expireExpressionExpanded(m_string->evaluate(t)); + std::string expireExpressionExpanded(m_string->evaluate(t, rule)); std::string fully_qualified_var; int expirySeconds = 0; diff --git a/src/actions/log_data.cc b/src/actions/log_data.cc index 596505868..81cce50ec 100644 --- a/src/actions/log_data.cc +++ b/src/actions/log_data.cc @@ -22,6 +22,7 @@ #include "modsecurity/actions/action.h" #include "modsecurity/transaction.h" #include "modsecurity/rule.h" +#include "modsecurity/rule_with_actions.h" #include "modsecurity/rule_message.h" @@ -30,7 +31,7 @@ namespace actions { bool LogData::evaluate(RuleWithActions *rule, Transaction *transaction, RuleMessage &ruleMessage) { - ruleMessage.m_data = data(transaction); + ruleMessage.m_data = m_string->evaluate(transaction, rule); return true; } diff --git a/src/actions/msg.cc b/src/actions/msg.cc index fecab5385..467b38f15 100644 --- a/src/actions/msg.cc +++ b/src/actions/msg.cc @@ -22,6 +22,7 @@ #include "modsecurity/actions/action.h" #include "modsecurity/transaction.h" #include "modsecurity/rule.h" +#include "modsecurity/rule_with_actions.h" #include "modsecurity/rule_message.h" /* @@ -47,7 +48,7 @@ namespace actions { bool Msg::evaluate(RuleWithActions *rule, Transaction *transaction, RuleMessage &ruleMessage) { - const auto msg = data(transaction); + const auto msg = m_string->evaluate(transaction, rule); ruleMessage.m_message = msg; ms_dbg_a(transaction, 9, "Saving msg: " + msg); diff --git a/src/actions/set_var.cc b/src/actions/set_var.cc index 70c07d065..ad1eb2272 100644 --- a/src/actions/set_var.cc +++ b/src/actions/set_var.cc @@ -101,8 +101,7 @@ bool SetVar::evaluate(RuleWithActions *rule, Transaction *t) { try { std::vector l; - RuleWithOperator *rr = dynamic_cast(rule); - m_variable->evaluate(t, rr, &l); + m_variable->evaluate(t, rule, &l); if (l.size() == 0) { value = 0; } else { diff --git a/src/run_time_string.cc b/src/run_time_string.cc index 45f298bc3..d66cc7b40 100644 --- a/src/run_time_string.cc +++ b/src/run_time_string.cc @@ -58,8 +58,7 @@ std::string RunTimeString::evaluate(Transaction *t, Rule *r) { s.append(z->m_string); } else if (z->m_var != NULL && t != NULL) { std::vector l; - // FIXME: This cast should be removed. - RuleWithOperator *rr = dynamic_cast(r); + auto *rr = dynamic_cast(r); z->m_var->evaluate(t, rr, &l); if (l.size() > 0) { s.append(l[0]->getValue()); diff --git a/src/variables/global.h b/src/variables/global.h index 8c7d39360..81246577c 100644 --- a/src/variables/global.h +++ b/src/variables/global.h @@ -92,7 +92,7 @@ class Global_DynamicElement : public Variable { void evaluate(Transaction *t, RuleWithActions *rule, std::vector *l) override { - std::string string = m_string->evaluate(t); + std::string string = m_string->evaluate(t, rule); t->m_collections.m_global_collection->resolveMultiMatches( string, t->m_collections.m_global_collection_key, diff --git a/src/variables/ip.h b/src/variables/ip.h index a32dbecd9..93fdda422 100644 --- a/src/variables/ip.h +++ b/src/variables/ip.h @@ -91,7 +91,7 @@ class Ip_DynamicElement : public Variable { void evaluate(Transaction *t, RuleWithActions *rule, std::vector *l) override { - std::string string = m_string->evaluate(t); + std::string string = m_string->evaluate(t, rule); t->m_collections.m_ip_collection->resolveMultiMatches( string, t->m_collections.m_ip_collection_key, diff --git a/src/variables/resource.h b/src/variables/resource.h index a198ce762..033807d31 100644 --- a/src/variables/resource.h +++ b/src/variables/resource.h @@ -91,7 +91,7 @@ class Resource_DynamicElement : public Variable { void evaluate(Transaction *t, RuleWithActions *rule, std::vector *l) override { - std::string string = m_string->evaluate(t); + std::string string = m_string->evaluate(t, rule); t->m_collections.m_resource_collection->resolveMultiMatches( string, t->m_collections.m_resource_collection_key, diff --git a/src/variables/session.h b/src/variables/session.h index 02efbe66d..6dc0a82aa 100644 --- a/src/variables/session.h +++ b/src/variables/session.h @@ -91,7 +91,7 @@ class Session_DynamicElement : public Variable { void evaluate(Transaction *t, RuleWithActions *rule, std::vector *l) override { - std::string string = m_string->evaluate(t); + std::string string = m_string->evaluate(t, rule); t->m_collections.m_session_collection->resolveMultiMatches( string, t->m_collections.m_session_collection_key, diff --git a/src/variables/tx.h b/src/variables/tx.h index 1fae827f1..40d02e684 100644 --- a/src/variables/tx.h +++ b/src/variables/tx.h @@ -89,7 +89,7 @@ class Tx_DynamicElement : public Variable { void evaluate(Transaction *t, RuleWithActions *rule, std::vector *l) override { - std::string string = m_string->evaluate(t); + std::string string = m_string->evaluate(t, rule); t->m_collections.m_tx_collection->resolveMultiMatches(string, l, m_keyExclusion); } diff --git a/src/variables/user.h b/src/variables/user.h index a0a46533e..1edcbc9dc 100644 --- a/src/variables/user.h +++ b/src/variables/user.h @@ -91,7 +91,7 @@ class User_DynamicElement : public Variable { void evaluate(Transaction *t, RuleWithActions *rule, std::vector *l) override { - std::string string = m_string->evaluate(t); + std::string string = m_string->evaluate(t, rule); t->m_collections.m_user_collection->resolveMultiMatches( string, t->m_collections.m_user_collection_key, diff --git a/test/test-cases/regression/variable-RULE.json b/test/test-cases/regression/variable-RULE.json index f622a7cec..ffac51d51 100644 --- a/test/test-cases/regression/variable-RULE.json +++ b/test/test-cases/regression/variable-RULE.json @@ -2,7 +2,7 @@ { "enabled": 1, "version_min": 300000, - "title": "Testing Variables :: RULE (1/7)", + "title": "Testing Variables :: RULE (1/16)", "client": { "ip": "200.249.12.31", "port": 123 @@ -48,7 +48,7 @@ { "enabled": 1, "version_min": 300000, - "title": "Testing Variables :: RULE (2/7)", + "title": "Testing Variables :: RULE (2/16)", "client": { "ip": "200.249.12.31", "port": 123 @@ -94,7 +94,7 @@ { "enabled": 1, "version_min": 300000, - "title": "Testing Variables :: RULE (3/7)", + "title": "Testing Variables :: RULE (3/16)", "client": { "ip": "200.249.12.31", "port": 123 @@ -140,7 +140,7 @@ { "enabled": 1, "version_min": 300000, - "title": "Testing Variables :: RULE (4/7)", + "title": "Testing Variables :: RULE (4/16)", "client": { "ip": "200.249.12.31", "port": 123 @@ -186,7 +186,7 @@ { "enabled": 1, "version_min": 300000, - "title": "Testing Variables :: RULE (5/7)", + "title": "Testing Variables :: RULE (5/16)", "client": { "ip": "200.249.12.31", "port": 123 @@ -232,7 +232,7 @@ { "enabled": 1, "version_min": 300000, - "title": "Testing Variables :: RULE (6/7)", + "title": "Testing Variables :: RULE (6/16)", "client": { "ip": "200.249.12.31", "port": 123 @@ -278,7 +278,7 @@ { "enabled": 1, "version_min": 300000, - "title": "Testing Variables :: RULE (7/7)", + "title": "Testing Variables :: RULE (7/16)", "client": { "ip": "200.249.12.31", "port": 123 @@ -320,5 +320,417 @@ "SecRuleEngine On", "SecRule rule:msg \"@contains message\" \"id:1,msg:'message123',setvar:'ip.block_reason=%{RULE.msg}%',phase:3,pass,t:trim\"" ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (8/16)", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": "Saving variable: TX:rule_id with value: 108", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecAction \"id:108,phase:3,pass,setvar:tx.rule_id=%{RULE.id}\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (9/16)", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": "Saving variable: TX:rule_msg with value: message123", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecAction \"id:109,msg:'message123',phase:3,pass,setvar:tx.rule_msg=%{RULE.msg}\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (10/16)", + "resource": "lua", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": "Saving variable: TX:rule_id with value: 110.*Called from rule 110\\.", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecRuleScript test-cases/data/lua-getvar-tx-rule-id.lua \"id:110,phase:3,pass,setvar:tx.rule_id=%{RULE.id}\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (11/16)", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": "Saving variable: TX:score with value: 117", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecAction \"id:111,phase:3,pass,setvar:tx.score=5\"", + "SecAction \"id:112,phase:3,pass,setvar:tx.score=+%{RULE.id}\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (12/16)", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": "Saving variable: TX:score_113 with value: 6", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecAction \"id:113,phase:3,pass,setvar:tx.score_%{RULE.id}=5,setvar:tx.score_%{RULE.id}=+1\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (13/16)", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": "Saving variable: TX:score_114 with value: 6", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecRule REQUEST_METHOD \"@streq POST\" \"id:114,phase:3,pass,setvar:tx.score_%{RULE.id}=5,setvar:tx.score_%{RULE.id}=+1\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (14/16)", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": "Saving msg: id 115", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecRule REQUEST_METHOD \"@streq POST\" \"id:115,phase:3,pass,msg:'id %{RULE.id}'\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (15/16)", + "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": "27", + "Content-Type": "application/x-www-form-urlencoded" + }, + "uri": "/", + "method": "POST", + "body": [ + "param1=value1¶m2=value2" + ] + }, + "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": { + "error_log": "data \"id 116\"", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecAction \"id:116,phase:3,pass,log,msg:'logdata test',logdata:'id %{RULE.id}'\"" + ] + }, + { + "enabled": 1, + "version_min": 300000, + "title": "Testing Variables :: RULE (16/16)", + "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": "*/*" + }, + "uri": "/", + "method": "GET" + }, + "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": "Setting variable `score_200' to expire in 60 seconds", + "http_code": 200 + }, + "rules": [ + "SecRuleEngine On", + "SecAction \"id:1,phase:1,pass,nolog,initcol:ip=%{REMOTE_ADDR}\"", + "SecAction \"id:200,phase:1,pass,nolog,setvar:ip.score_%{RULE.id}=5,expirevar:ip.score_%{RULE.id}=60\"" + ] } ]