From de7c5c89bb20e366b624c21b8fc66adf0301974a Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Fri, 2 Feb 2018 15:41:13 -0300 Subject: [PATCH] Using shared var for variables names --- headers/modsecurity/collection/collection.h | 1 + headers/modsecurity/collection/variable.h | 36 ++++++- src/anchored_set_variable.cc | 8 +- .../backend/in_memory-per_process.cc | 87 ++++++--------- .../backend/in_memory-per_process.h | 2 +- src/collection/collections.cc | 3 +- src/modsecurity.cc | 10 +- src/operators/pm.cc | 2 +- src/operators/rx.cc | 2 +- src/variables/env.cc | 7 +- src/variables/global.h | 8 +- src/variables/highest_severity.cc | 2 +- src/variables/ip.h | 2 +- src/variables/resource.h | 8 +- src/variables/session.h | 10 +- src/variables/tx.h | 14 ++- src/variables/user.h | 10 +- src/variables/variable.cc | 100 ++++-------------- src/variables/variable.h | 49 +++++++-- src/variables/xml.cc | 5 +- .../regression/collection-resource.json | 4 +- 21 files changed, 183 insertions(+), 187 deletions(-) diff --git a/headers/modsecurity/collection/collection.h b/headers/modsecurity/collection/collection.h index 92989459..ae1cb78f 100644 --- a/headers/modsecurity/collection/collection.h +++ b/headers/modsecurity/collection/collection.h @@ -40,6 +40,7 @@ namespace collection { class Collection { public: + Collection(std::string a) : m_name(a) { } virtual ~Collection() { } virtual void store(std::string key, std::string value) = 0; diff --git a/headers/modsecurity/collection/variable.h b/headers/modsecurity/collection/variable.h index f0b59c53..eda54096 100644 --- a/headers/modsecurity/collection/variable.h +++ b/headers/modsecurity/collection/variable.h @@ -35,28 +35,60 @@ typedef struct Variable_t Variable; namespace modsecurity { namespace collection { +class Collection; class Variable { public: explicit Variable(const std::string *key) : m_key(""), m_value("") { m_key.assign(*key); + m_keyWithCollection = std::make_shared(*key); } + Variable(const std::string *key, const std::string *value) : m_key(""), m_value("") { m_key.assign(*key); m_value.assign(*value); + m_keyWithCollection = std::make_shared(*key); } + Variable() : m_key(""), - m_value("") { } + m_value("") { + m_keyWithCollection = std::make_shared(m_key); + } + + Variable(const std::string *a, const std::string *b, const std::string *c) : + m_key(*a + ":" + *b), + m_value(*c) { + m_keyWithCollection = std::make_shared(*a + ":" + *b); + } + + Variable(std::shared_ptr fullName) : + m_key(""), + m_value("") { + m_keyWithCollection = fullName; + m_key.assign(*fullName.get()); + } + + Variable(std::shared_ptr fullName, const std::string *value) : + m_key(""), + m_value("") { + m_value.assign(*value); + m_keyWithCollection = fullName; + m_key.assign(*fullName.get()); + } + explicit Variable(const Variable *o) : m_key(""), m_value("") { m_key.assign(o->m_key); m_value.assign(o->m_value); + m_col.assign(o->m_col); + m_keyWithCollection = o->m_keyWithCollection; + for (auto &i : o->m_orign) { std::unique_ptr origin(new VariableOrigin()); origin->m_offset = i->m_offset; @@ -67,6 +99,8 @@ class Variable { std::string m_key; std::string m_value; + std::string m_col; + std::shared_ptr m_keyWithCollection; std::list> m_orign; }; diff --git a/src/anchored_set_variable.cc b/src/anchored_set_variable.cc index 5ed96674..b076cc29 100644 --- a/src/anchored_set_variable.cc +++ b/src/anchored_set_variable.cc @@ -53,10 +53,8 @@ void AnchoredSetVariable::set(const std::string &key, const std::string &value, size_t offset, size_t len) { std::unique_ptr origin(new VariableOrigin()); std::string *v = new std::string(value); - std::string *k = new std::string(m_name + ":" + key); - collection::Variable *var = new collection::Variable(k, v); + collection::Variable *var = new collection::Variable(std::make_shared(m_name + ":" + key), v); delete v; - delete k; origin->m_offset = offset; origin->m_length = len; @@ -70,10 +68,8 @@ void AnchoredSetVariable::set(const std::string &key, const std::string &value, size_t offset) { std::unique_ptr origin(new VariableOrigin()); std::string *v = new std::string(value); - std::string *k = new std::string(m_name + ":" + key); - collection::Variable *var = new collection::Variable(k, v); + collection::Variable *var = new collection::Variable(std::make_shared(m_name + ":" + key), v); delete v; - delete k; origin->m_offset = offset; origin->m_length = value.size(); diff --git a/src/collection/backend/in_memory-per_process.cc b/src/collection/backend/in_memory-per_process.cc index 844eb945..5301921e 100644 --- a/src/collection/backend/in_memory-per_process.cc +++ b/src/collection/backend/in_memory-per_process.cc @@ -36,7 +36,8 @@ namespace collection { namespace backend { -InMemoryPerProcess::InMemoryPerProcess() { +InMemoryPerProcess::InMemoryPerProcess(std::string name) : + Collection(name) { this->reserve(1000); pthread_mutex_init(&m_lock, NULL); } @@ -89,7 +90,7 @@ void InMemoryPerProcess::resolveSingleMatch(const std::string& var, auto range = this->equal_range(var); for (auto it = range.first; it != range.second; ++it) { - l->push_back(new Variable(&it->first, &it->second)); + l->push_back(new Variable(&m_name, &it->first, &it->second)); } } @@ -99,33 +100,15 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var, size_t keySize = var.size(); l->reserve(15); - auto range = this->equal_range(var); - - for (auto it = range.first; it != range.second; ++it) { - l->insert(l->begin(), new Variable(&var, &it->second)); - } - - for (const auto& x : *this) { - bool diff = false; - - if (x.first.size() <= keySize + 1) { - continue; + if (keySize == 0) { + for (auto &i : *this) { + l->insert(l->begin(), new Variable(&m_name, &i.first, &i.second)); } - if (x.first.at(keySize) != ':') { - continue; + } else { + auto range = this->equal_range(var); + for (auto it = range.first; it != range.second; ++it) { + l->insert(l->begin(), new Variable(&m_name, &var, &it->second)); } - - for (int i = 0; i < keySize && diff == false; i++) { - if (std::tolower(x.first.at(i)) != std::tolower(var.at(i))) { - diff = true; - } - } - - if (diff == true) { - continue; - } - - l->insert(l->begin(), new Variable(&x.first, &x.second)); } } @@ -133,36 +116,37 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var, void InMemoryPerProcess::resolveRegularExpression(const std::string& var, std::vector *l) { - if (var.find(":") == std::string::npos) { - return; - } - if (var.size() < var.find(":") + 3) { - return; - } - std::string col = std::string(var, 0, var.find(":")); - std::string name = std::string(var, var.find(":") + 2, - var.size() - var.find(":") - 3); - size_t keySize = col.size(); - Utils::Regex r = Utils::Regex(name); + + //if (var.find(":") == std::string::npos) { + // return; + //} + //if (var.size() < var.find(":") + 3) { + // return; + //} + //std::string col = std::string(var, 0, var.find(":")); + //std::string name = std::string(var, var.find(":") + 2, + // var.size() - var.find(":") - 3); + //size_t keySize = col.size(); + Utils::Regex r = Utils::Regex(var); for (const auto& x : *this) { - if (x.first.size() <= keySize + 1) { - continue; - } - if (x.first.at(keySize) != ':') { - continue; - } - if (std::string(x.first, 0, keySize) != col) { - continue; - } - std::string content = std::string(x.first, keySize + 1, - x.first.size() - keySize - 1); - int ret = Utils::regex_search(content, r); + //if (x.first.size() <= keySize + 1) { + // continue; + //} + //if (x.first.at(keySize) != ':') { + // continue; + //} + //if (std::string(x.first, 0, keySize) != col) { + // continue; + //} + //std::string content = std::string(x.first, keySize + 1, + // x.first.size() - keySize - 1); + int ret = Utils::regex_search(x.first, r); if (ret <= 0) { continue; } - l->insert(l->begin(), new Variable(&x.first, &x.second)); + l->insert(l->begin(), new Variable(&m_name, &x.first, &x.second)); } } @@ -170,7 +154,6 @@ void InMemoryPerProcess::resolveRegularExpression(const std::string& var, std::unique_ptr InMemoryPerProcess::resolveFirst( const std::string& var) { auto range = equal_range(var); - for (auto it = range.first; it != range.second; ++it) { return std::unique_ptr(new std::string(it->second)); } diff --git a/src/collection/backend/in_memory-per_process.h b/src/collection/backend/in_memory-per_process.h index 80e12915..020111f1 100644 --- a/src/collection/backend/in_memory-per_process.h +++ b/src/collection/backend/in_memory-per_process.h @@ -71,7 +71,7 @@ class InMemoryPerProcess : /*std::hash*/MyHash, MyEqual>, public Collection { public: - InMemoryPerProcess(); + InMemoryPerProcess(std::string name); ~InMemoryPerProcess(); void store(std::string key, std::string value) override; diff --git a/src/collection/collections.cc b/src/collection/collections.cc index 109f44f2..174f2a84 100644 --- a/src/collection/collections.cc +++ b/src/collection/collections.cc @@ -44,7 +44,8 @@ Collections::Collections(Collection *global, m_ip_collection(ip), m_session_collection(session), m_user_collection(user), - m_tx_collection(new backend::InMemoryPerProcess()) { } + m_tx_collection(new backend::InMemoryPerProcess("TX")) { + } Collections::~Collections() { } diff --git a/src/modsecurity.cc b/src/modsecurity.cc index d58b6075..0380d346 100644 --- a/src/modsecurity.cc +++ b/src/modsecurity.cc @@ -66,11 +66,11 @@ ModSecurity::ModSecurity() m_session_collection(new collection::backend::LMDB()), m_user_collection(new collection::backend::LMDB()), #else - m_global_collection(new collection::backend::InMemoryPerProcess()), - m_resource_collection(new collection::backend::InMemoryPerProcess()), - m_ip_collection(new collection::backend::InMemoryPerProcess()), - m_session_collection(new collection::backend::InMemoryPerProcess()), - m_user_collection(new collection::backend::InMemoryPerProcess()), + m_global_collection(new collection::backend::InMemoryPerProcess("GLOBAL")), + m_ip_collection(new collection::backend::InMemoryPerProcess("IP")), + m_resource_collection(new collection::backend::InMemoryPerProcess("RESOURCE")), + m_session_collection(new collection::backend::InMemoryPerProcess("SESSION")), + m_user_collection(new collection::backend::InMemoryPerProcess("USER")), #endif m_logCb(NULL) { UniqueId::uniqueId(); diff --git a/src/operators/pm.cc b/src/operators/pm.cc index b0f6460f..b3b70d0c 100644 --- a/src/operators/pm.cc +++ b/src/operators/pm.cc @@ -105,7 +105,7 @@ bool Pm::evaluate(Transaction *transaction, Rule *rule, } if (capture && transaction && rc) { - transaction->m_collections.m_tx_collection->storeOrUpdateFirst("TX:0", + transaction->m_collections.m_tx_collection->storeOrUpdateFirst("0", std::string(match)); #ifndef NO_LOGS transaction->debug(7, "Added pm match TX.0: " + \ diff --git a/src/operators/rx.cc b/src/operators/rx.cc index 99a49996..4eb41b52 100644 --- a/src/operators/rx.cc +++ b/src/operators/rx.cc @@ -59,7 +59,7 @@ bool Rx::evaluate(Transaction *transaction, Rule *rule, matches.reverse(); for (const SMatch& a : matches) { transaction->m_collections.m_tx_collection->storeOrUpdateFirst( - "TX:" + std::to_string(i), a.match); + std::to_string(i), a.match); #ifndef NO_LOGS transaction->debug(7, "Added regex subexpression TX." + std::to_string(i) + ": " + a.match); diff --git a/src/variables/env.cc b/src/variables/env.cc index 741f8111..15cd3af1 100644 --- a/src/variables/env.cc +++ b/src/variables/env.cc @@ -43,16 +43,15 @@ void Env::evaluate(Transaction *transaction, } std::string key = std::string(env, 0, pos); std::string value = std::string(env, pos+1, env.length() - (pos + 1)); - std::pair a("ENV:" + key, value); + std::pair a(key, value); transaction->m_variableEnvs.insert(a); } for (auto& x : transaction->m_variableEnvs) { - if ((x.first.substr(0, m_name.size() + 1).compare(m_name + ":") != 0) - && (x.first != m_name)) { + if (x.first != m_name && m_name.length() > 0) { continue; } - l->push_back(new collection::Variable(&x.first, &x.second)); + l->push_back(new collection::Variable(&m_collectionName, &x.first, &x.second)); } } diff --git a/src/variables/global.h b/src/variables/global.h index 2a3f97ff..f823798a 100644 --- a/src/variables/global.h +++ b/src/variables/global.h @@ -35,7 +35,7 @@ class Global_DictElement : public Variable { public: explicit Global_DictElement(std::string dictElement) : Variable("GLOBAL"), - m_dictElement("GLOBAL:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -67,7 +67,7 @@ class Global_DictElementRegexp : public Variable { explicit Global_DictElementRegexp(std::string dictElement) : Variable("GLOBAL:regex(" + dictElement + ")"), m_r(dictElement), - m_dictElement("GLOBAL:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -92,7 +92,7 @@ class Global_DynamicElement : public Variable { std::vector *l) override { std::string string = m_string->evaluate(t); t->m_collections.m_global_collection->resolveMultiMatches( - "GLOBAL:" + string, t->m_collections.m_global_collection_key, l); + string, t->m_collections.m_global_collection_key, l); } @@ -104,7 +104,7 @@ class Global_DynamicElement : public Variable { void storeOrUpdateFirst(Transaction *t, std::string var, std::string value) { t->m_collections.m_global_collection->storeOrUpdateFirst( - "GLOBAL:" + var, t->m_collections.m_global_collection_key, value); + var, t->m_collections.m_global_collection_key, value); } std::unique_ptr m_string; diff --git a/src/variables/highest_severity.cc b/src/variables/highest_severity.cc index ca0dc69a..13c13cb2 100644 --- a/src/variables/highest_severity.cc +++ b/src/variables/highest_severity.cc @@ -31,7 +31,7 @@ void HighestSeverity::evaluate(Transaction *transaction, std::vector *l) { transaction->m_variableHighestSeverityAction.assign( std::to_string(transaction->m_highestSeverityAction)); - l->push_back(new collection::Variable(&m_retName, + l->push_back(new collection::Variable(m_fullName, &transaction->m_variableHighestSeverityAction)); } diff --git a/src/variables/ip.h b/src/variables/ip.h index ebc308e8..65c36b16 100644 --- a/src/variables/ip.h +++ b/src/variables/ip.h @@ -104,7 +104,7 @@ class Ip_DynamicElement : public Variable { void storeOrUpdateFirst(Transaction *t, std::string var, std::string value) { t->m_collections.m_ip_collection->storeOrUpdateFirst( - "IP:" + var, t->m_collections.m_ip_collection_key, value); + var, t->m_collections.m_ip_collection_key, value); } std::unique_ptr m_string; diff --git a/src/variables/resource.h b/src/variables/resource.h index 09870e9f..e8dcbfdb 100644 --- a/src/variables/resource.h +++ b/src/variables/resource.h @@ -35,7 +35,7 @@ class Resource_DictElement : public Variable { public: explicit Resource_DictElement(std::string dictElement) : Variable("RESOURCE:" + dictElement), - m_dictElement("RESOURCE:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -69,7 +69,7 @@ class Resource_DictElementRegexp : public Variable { explicit Resource_DictElementRegexp(std::string dictElement) : Variable("RESOURCE:regex(" + dictElement + ")"), m_r(dictElement), - m_dictElement("RESOURCE:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -95,7 +95,7 @@ class Resource_DynamicElement : public Variable { std::vector *l) override { std::string string = m_string->evaluate(t); t->m_collections.m_resource_collection->resolveMultiMatches( - "RESOURCE:" + string, + string, t->m_collections.m_resource_collection_key, t->m_rules->m_secWebAppId.m_value, l); } @@ -108,7 +108,7 @@ class Resource_DynamicElement : public Variable { void storeOrUpdateFirst(Transaction *t, std::string var, std::string value) { t->m_collections.m_resource_collection->storeOrUpdateFirst( - "RESOURCE:" + var, + var, t->m_collections.m_resource_collection_key, t->m_rules->m_secWebAppId.m_value, value); } diff --git a/src/variables/session.h b/src/variables/session.h index e967cd1d..70b62a3c 100644 --- a/src/variables/session.h +++ b/src/variables/session.h @@ -35,7 +35,7 @@ class Session_DictElement : public Variable { public: explicit Session_DictElement(std::string dictElement) : Variable("SESSION"), - m_dictElement("SESSION:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -67,9 +67,9 @@ class Session_NoDictElement : public Variable { class Session_DictElementRegexp : public Variable { public: explicit Session_DictElementRegexp(std::string dictElement) - : Variable("SESSION"), + : Variable("SESSION:regex(" + dictElement + ")"), m_r(dictElement), - m_dictElement("SESSION:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -95,7 +95,7 @@ class Session_DynamicElement : public Variable { std::vector *l) override { std::string string = m_string->evaluate(t); t->m_collections.m_session_collection->resolveMultiMatches( - "SESSION:" + string, + string, t->m_collections.m_session_collection_key, l); } @@ -107,7 +107,7 @@ class Session_DynamicElement : public Variable { void storeOrUpdateFirst(Transaction *t, std::string var, std::string value) { t->m_collections.m_session_collection->storeOrUpdateFirst( - "SESSION:" + var, t->m_collections.m_session_collection_key, + var, t->m_collections.m_session_collection_key, value); } diff --git a/src/variables/tx.h b/src/variables/tx.h index 00e3087a..a983b10b 100644 --- a/src/variables/tx.h +++ b/src/variables/tx.h @@ -35,7 +35,7 @@ class Tx_DictElement : public Variable { public: explicit Tx_DictElement(std::string dictElement) : Variable("TX:" + dictElement), - m_dictElement("TX:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -56,7 +56,7 @@ class Tx_NoDictElement : public Variable { void evaluate(Transaction *t, Rule *rule, std::vector *l) override { - t->m_collections.m_tx_collection->resolveMultiMatches(m_name, l); + t->m_collections.m_tx_collection->resolveMultiMatches("", l); } }; @@ -64,9 +64,9 @@ class Tx_NoDictElement : public Variable { class Tx_DictElementRegexp : public Variable { public: explicit Tx_DictElementRegexp(std::string dictElement) - : Variable("TX"), + : Variable("TX:regex(" + dictElement + ")"), m_r(dictElement), - m_dictElement("TX:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -90,8 +90,7 @@ class Tx_DynamicElement : public Variable { Rule *rule, std::vector *l) override { std::string string = m_string->evaluate(t); - t->m_collections.m_tx_collection->resolveMultiMatches( - "TX:" + string, l); + t->m_collections.m_tx_collection->resolveMultiMatches(string, l); } void del(Transaction *t, std::string k) { @@ -100,8 +99,7 @@ class Tx_DynamicElement : public Variable { void storeOrUpdateFirst(Transaction *t, std::string var, std::string value) { - t->m_collections.m_tx_collection->storeOrUpdateFirst( - "TX:" + var, value); + t->m_collections.m_tx_collection->storeOrUpdateFirst(var, value); } std::unique_ptr m_string; diff --git a/src/variables/user.h b/src/variables/user.h index 81c396d1..30c0d86e 100644 --- a/src/variables/user.h +++ b/src/variables/user.h @@ -35,7 +35,7 @@ class User_DictElement : public Variable { public: explicit User_DictElement(std::string dictElement) : Variable("USER"), - m_dictElement("USER:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -67,9 +67,9 @@ class User_NoDictElement : public Variable { class User_DictElementRegexp : public Variable { public: explicit User_DictElementRegexp(std::string dictElement) - : Variable("USER"), + : Variable("USER:regex(" + dictElement + ")"), m_r(dictElement), - m_dictElement("USER:" + dictElement) { } + m_dictElement(dictElement) { } void evaluate(Transaction *t, Rule *rule, @@ -95,7 +95,7 @@ class User_DynamicElement : public Variable { std::vector *l) override { std::string string = m_string->evaluate(t); t->m_collections.m_user_collection->resolveMultiMatches( - "USER:" + string, t->m_collections.m_user_collection_key, l); + string, t->m_collections.m_user_collection_key, l); } void del(Transaction *t, std::string k) { @@ -106,7 +106,7 @@ class User_DynamicElement : public Variable { void storeOrUpdateFirst(Transaction *t, std::string var, std::string value) { t->m_collections.m_user_collection->storeOrUpdateFirst( - "USER:" + var, t->m_collections.m_user_collection_key, + var, t->m_collections.m_user_collection_key, value); } diff --git a/src/variables/variable.cc b/src/variables/variable.cc index 2f3758b3..62274f83 100644 --- a/src/variables/variable.cc +++ b/src/variables/variable.cc @@ -33,47 +33,18 @@ Variable::Variable(std::string name) m_collectionName(""), m_isExclusion(false), m_isCount(false) { - if (m_name.find(":") != std::string::npos) { - std::string col = utils::string::toupper( - std::string(m_name, 0, m_name.find(":"))); - std::string name = std::string(m_name, m_name.find(":") + 1, - m_name.size()); - if (col == "TX" || col == "IP" || col == "GLOBAL" - || col == "RESOURCE" || col == "SESSION" || col == "USER") { - m_collectionName = col; - } - if ((name.at(0) == '\\') || (name.at(0) == '/')) { - m_type = RegularExpression; - } else { - m_type = SingleMatch; - } - } else { - m_type = MultipleMatches; + size_t a = m_name.find(":"); + if (a == std::string::npos) { + a = m_name.find("."); } - - if (utils::string::tolower(m_name) == "tx") { - m_collectionName = "TX"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "ip") { - m_collectionName = "IP"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "global") { - m_collectionName = "GLOBAL"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "resource") { - m_collectionName = "RESOURCE"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "session") { - m_collectionName = "SESSION"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "user") { - m_collectionName = "USER"; - m_type = MultipleMatches; - } else if (m_name.find(".") != std::string::npos) { - m_kind = CollectionVarible; - m_collectionName = std::string(m_name, 0, m_name.find(".")); + if (a != std::string::npos) { + m_collectionName = utils::string::toupper(std::string(m_name, 0, a)); + m_name = std::string(m_name, a + 1, m_name.size()); + m_fullName = std::make_shared(m_collectionName + ":" + m_name); } else { - m_kind = DirectVariable; + m_fullName = std::make_shared(m_name); + m_collectionName = m_name; + m_name = ""; } } @@ -84,42 +55,18 @@ Variable::Variable(std::string name, VariableKind kind) m_kind(kind), m_isExclusion(false), m_isCount(false) { - if (m_name.find(":") != std::string::npos) { - std::string col = utils::string::toupper( - std::string(m_name, 0, m_name.find(":"))); - std::string name = std::string(m_name, m_name.find(":") + 1, - m_name.size()); - if (col == "TX" || col == "IP" || col == "GLOBAL" - || col == "RESOURCE" || col == "SESSION") { - m_collectionName = col; - } - if ((name.at(0) == '\\') || (name.at(0) == '/')) { - m_type = RegularExpression; - } else { - m_type = SingleMatch; - } + size_t a = m_name.find(":"); + if (a == std::string::npos) { + a = m_name.find("."); + } + if (a != std::string::npos) { + m_collectionName = utils::string::toupper(std::string(m_name, 0, a)); + m_name = std::string(m_name, a + 1, m_name.size()); + m_fullName = std::make_shared(m_collectionName + ":" + m_name); } else { - m_type = MultipleMatches; + m_fullName = std::make_shared(m_name); } - if (utils::string::tolower(m_name) == "tx") { - m_collectionName = "TX"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "ip") { - m_collectionName = "IP"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "global") { - m_collectionName = "GLOBAL"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "resource") { - m_collectionName = "RESOURCE"; - m_type = MultipleMatches; - } else if (utils::string::tolower(m_name) == "session") { - m_collectionName = "SESSION"; - m_type = MultipleMatches; - } else if (m_name.find(".") != std::string::npos) { - m_collectionName = std::string(m_name, 0, m_name.find(".")); - } } @@ -128,22 +75,21 @@ std::string Variable::to_s( std::string ret; std::string except(""); for (int i = 0; i < variables->size() ; i++) { - std::string name = variables->at(i)->m_name; VariableModificatorExclusion *e = dynamic_cast(variables->at(i)); if (e != NULL) { if (except.empty()) { - except = except + name; + except = except + *variables->at(i)->m_fullName.get(); } else { - except = except + "|" + name; + except = except + "|" + *variables->at(i)->m_fullName.get(); } continue; } if (i == 0) { - ret = ret + name; + ret = ret + *variables->at(i)->m_fullName.get(); } else { - ret = ret + "|" + name; + ret = ret + "|" + *variables->at(i)->m_fullName.get(); } } diff --git a/src/variables/variable.h b/src/variables/variable.h index 210a6089..1fbe2cfe 100644 --- a/src/variables/variable.h +++ b/src/variables/variable.h @@ -22,6 +22,7 @@ #include "modsecurity/transaction.h" #include "modsecurity/rule.h" +#include "modsecurity/rules.h" #include "src/utils/string.h" #include "src/utils/regex.h" @@ -357,6 +358,23 @@ class Variable { vv = t->m_variableUrlEncodedError.resolveFirst(); } else if (comp(variable, "USERID")) { vv = t->m_variableUserID.resolveFirst(); + } else if (comp(variable, "TX")) { + vv = t->m_collections.m_tx_collection->resolveFirst(""); + } else if (comp(variable, "RESOURCE")) { + vv = t->m_collections.m_resource_collection->resolveFirst("", + t->m_collections.m_resource_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(variable, "USER")) { + vv = t->m_collections.m_user_collection->resolveFirst("", + t->m_collections.m_user_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(variable, "SESSION")) { + vv = t->m_collections.m_session_collection->resolveFirst("", + t->m_collections.m_session_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(variable, "IP")) { + vv = t->m_collections.m_ip_collection->resolveFirst("", + t->m_collections.m_ip_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(variable, "GLOBAL")) { + vv = t->m_collections.m_global_collection->resolveFirst("", + t->m_collections.m_global_collection_key, t->m_rules->m_secWebAppId.m_value); } else { throw std::invalid_argument("Variable not found."); } @@ -410,15 +428,36 @@ class Variable { vv = t->m_variableRequestCookiesNames.resolveFirst(var); } else if (comp(col, "FILES_TMPNAMES")) { vv = t->m_variableFilesTmpNames.resolveFirst(var); + } else if (comp(col, "TX")) { + vv = t->m_collections.m_tx_collection->resolveFirst(var); + } else if (comp(col, "RESOURCE")) { + vv = t->m_collections.m_resource_collection->resolveFirst(var, + t->m_collections.m_resource_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(col, "USER")) { + vv = t->m_collections.m_user_collection->resolveFirst(var, + t->m_collections.m_user_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(col, "SESSION")) { + vv = t->m_collections.m_session_collection->resolveFirst(var, + t->m_collections.m_session_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(col, "IP")) { + vv = t->m_collections.m_ip_collection->resolveFirst(var, + t->m_collections.m_ip_collection_key, t->m_rules->m_secWebAppId.m_value); + } else if (comp(col, "GLOBAL")) { + vv = t->m_collections.m_global_collection->resolveFirst(var, + t->m_collections.m_global_collection_key, t->m_rules->m_secWebAppId.m_value); } else { throw std::invalid_argument("Variable not found."); } } + if (vv == nullptr) { + return std::string(""); + } return std::string(*vv.get()); } std::string m_name; std::string m_collectionName; + std::shared_ptr m_fullName; VariableType m_type; VariableKind m_kind; @@ -430,7 +469,7 @@ class Variable { class VariableModificatorExclusion : public Variable { public: explicit VariableModificatorExclusion(std::unique_ptr var) - : Variable(var->m_name), + : Variable(*var->m_fullName.get()), m_var(std::move(var)) { m_isExclusion = true; } @@ -448,7 +487,7 @@ class VariableModificatorExclusion : public Variable { class VariableModificatorCount : public Variable { public: explicit VariableModificatorCount(std::unique_ptr var) - : Variable(var->m_name), + : Variable(*var->m_fullName.get()), m_var(std::move(var)) { m_isCount = true; } @@ -463,15 +502,13 @@ class VariableModificatorCount : public Variable { m_var->evaluate(t, rule, &reslIn); for (const collection::Variable *a : reslIn) { count++; - delete a; + delete a; a = NULL; } reslIn.clear(); std::string *res = new std::string(std::to_string(count)); - std::string *name = new std::string(m_name); - val = new collection::Variable(name, res); - delete name; + val = new collection::Variable(m_var->m_fullName, res); delete res; l->push_back(val); diff --git a/src/variables/xml.cc b/src/variables/xml.cc index 5f17a319..aa42b402 100644 --- a/src/variables/xml.cc +++ b/src/variables/xml.cc @@ -56,13 +56,14 @@ void XML::evaluate(Transaction *t, size_t pos; param = m_name; + /* pos = m_name.find_first_of(":"); if (pos == std::string::npos) { param = ""; } else { param = std::string(m_name, pos+1, m_name.length() - (pos + 1)); } - + */ /* Is there an XML document tree at all? */ if (t->m_xml->m_data.doc == NULL) { /* Sorry, we've got nothing to give! */ @@ -126,7 +127,7 @@ void XML::evaluate(Transaction *t, xmlNodeGetContent(nodes->nodeTab[i])); if (content != NULL) { std::string *a = new std::string(content); - collection::Variable *var = new collection::Variable(&m_name, + collection::Variable *var = new collection::Variable(m_fullName, a); delete a; l->push_back(var); diff --git a/test/test-cases/regression/collection-resource.json b/test/test-cases/regression/collection-resource.json index c6b102ca..b73d00cb 100644 --- a/test/test-cases/regression/collection-resource.json +++ b/test/test-cases/regression/collection-resource.json @@ -40,7 +40,7 @@ }, "expected":{ "audit_log":"", - "debug_log":"Target value: \"123\" \\(Variable: whee::::RESOURCE:test\\)", + "debug_log":"Target value: \"123\" \\(Variable: RESOURCE:whee::::test\\)", "error_log":"" }, "rules":[ @@ -92,7 +92,7 @@ }, "expected":{ "audit_log":"", - "debug_log":"whee::webappid::RESOURCE:test", + "debug_log":"RESOURCE:whee::webappid::test", "error_log":"" }, "rules":[