From 6c79e716e72581d93b3816c45df2729d801e39ed Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Fri, 21 Aug 2020 19:48:05 -0300 Subject: [PATCH] Refactoring on variables::Variable Using the references on key and collection as shared pointers --- src/actions/set_var.cc | 4 +- src/rule_with_actions.h.rej | 15 ----- src/rule_with_operator.cc | 4 +- src/variables/env.cc | 5 +- src/variables/global.h | 4 +- src/variables/highest_severity.cc | 2 +- src/variables/ip.h | 4 +- src/variables/resource.h | 8 +-- src/variables/session.h | 4 +- src/variables/tx.h | 4 +- src/variables/user.h | 6 +- src/variables/variable.cc | 32 ++++------ src/variables/variable.h | 98 +++++++++++++++++++++++-------- src/variables/web_app_id.h | 3 +- src/variables/xml.cc | 6 +- src/variables/xml.h | 2 +- 16 files changed, 116 insertions(+), 85 deletions(-) delete mode 100644 src/rule_with_actions.h.rej diff --git a/src/actions/set_var.cc b/src/actions/set_var.cc index 84a336df..8f20b141 100644 --- a/src/actions/set_var.cc +++ b/src/actions/set_var.cc @@ -73,7 +73,7 @@ bool SetVar::execute(Transaction *t) const noexcept { } else if (user) { m_variableNameExpanded = user->evaluateRunTimeString(t); } else { - m_variableNameExpanded = m_variable->m_name; + m_variableNameExpanded = *m_variable->getVariableKey(); } if (m_operation == setOperation) { @@ -126,7 +126,7 @@ bool SetVar::execute(Transaction *t) const noexcept { } } - ms_dbg_a(t, 8, "Saving variable: " + m_variable->m_collectionName \ + ms_dbg_a(t, 8, "Saving variable: " + *m_variable->getVariableKeyWithCollection() \ + ":" + m_variableNameExpanded + " with value: " + targetValue); if (tx) { diff --git a/src/rule_with_actions.h.rej b/src/rule_with_actions.h.rej deleted file mode 100644 index a490a2f0..00000000 --- a/src/rule_with_actions.h.rej +++ /dev/null @@ -1,15 +0,0 @@ ---- src/rule_with_actions.h -+++ src/rule_with_actions.h -@@ -520,11 +528,10 @@ class RuleWithActions : public Rule { - for (auto &a : m_XmlNSs) { - dst.push_back(a.get()); - } -- -+ - return dst; - } - -- - virtual void dump(std::stringstream &out) const override { - out << "RuleWithActions" << std::endl; - } diff --git a/src/rule_with_operator.cc b/src/rule_with_operator.cc index c73c8d03..0d921e7d 100644 --- a/src/rule_with_operator.cc +++ b/src/rule_with_operator.cc @@ -187,7 +187,7 @@ inline void RuleWithOperator::getFinalVars(variables::Variables *vars, trans->m_ruleRemoveTargetById.end(), [&, variable, this](std::pair &m) -> bool { return m.first == getId() - && m.second == *variable->m_fullName.get(); + && m.second == *variable->getVariableKeyWithCollection(); }) != trans->m_ruleRemoveTargetById.end()) { continue; } @@ -196,7 +196,7 @@ inline void RuleWithOperator::getFinalVars(variables::Variables *vars, [&, variable, trans, this]( std::pair &m) -> bool { return containsTag(m.first, trans) - && m.second == *variable->m_fullName.get(); + && m.second == *variable->getVariableKeyWithCollection(); }) != trans->m_ruleRemoveTargetByTag.end()) { continue; } diff --git a/src/variables/env.cc b/src/variables/env.cc index 7de87d84..09e88dcc 100644 --- a/src/variables/env.cc +++ b/src/variables/env.cc @@ -47,11 +47,12 @@ void Env::evaluate(Transaction *transaction, } for (auto& x : transaction->m_variableEnvs) { - if (x.first != m_name && m_name.length() > 0) { + if (x.first != *getVariableKey() && getVariableKey()->length() > 0) { continue; } if (!m_keyExclusion.toOmit(x.first)) { - l->emplace_back(std::make_shared(&m_collectionName, &x.first, &x.second)); + l->emplace_back(std::make_shared(getVariableKeyWithCollection().get(), + &x.first, &x.second)); } } } diff --git a/src/variables/global.h b/src/variables/global.h index 4d32f55f..9b2625b6 100644 --- a/src/variables/global.h +++ b/src/variables/global.h @@ -42,7 +42,7 @@ class Global_DictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { t->m_collections.m_global_collection->resolveMultiMatches( - m_name, t->m_collections.m_global_collection_key, + *getVariableKey(), t->m_collections.m_global_collection_key, t->m_rules->m_secWebAppId.m_value, l, m_keyExclusion); } @@ -86,7 +86,7 @@ class Global_DynamicElement : public VariableWithRunTimeString { public: explicit Global_DynamicElement(std::unique_ptr dictElement) : VariableWithRunTimeString( - "GLOBAL:dynamic", + "GLOBAL", std::move(dictElement) ) { }; diff --git a/src/variables/highest_severity.cc b/src/variables/highest_severity.cc index c2db0f21..f3a924b3 100644 --- a/src/variables/highest_severity.cc +++ b/src/variables/highest_severity.cc @@ -30,7 +30,7 @@ void HighestSeverity::evaluate(Transaction *transaction, std::vector> *l) { transaction->m_variableHighestSeverityAction.assign( std::to_string(transaction->m_highestSeverityAction)); - l->push_back(std::make_shared(m_fullName.get(), &transaction->m_variableHighestSeverityAction)); + l->push_back(std::make_shared(getVariableKeyWithCollection().get(), &transaction->m_variableHighestSeverityAction)); } diff --git a/src/variables/ip.h b/src/variables/ip.h index 12f076d6..3fd3baff 100644 --- a/src/variables/ip.h +++ b/src/variables/ip.h @@ -42,7 +42,7 @@ class Ip_DictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { t->m_collections.m_ip_collection->resolveMultiMatches( - m_name, t->m_collections.m_ip_collection_key, + *getVariableKey(), t->m_collections.m_ip_collection_key, t->m_rules->m_secWebAppId.m_value, l, m_keyExclusion); } @@ -85,7 +85,7 @@ class Ip_DynamicElement : public VariableWithRunTimeString { public: explicit Ip_DynamicElement(std::unique_ptr dictElement) : VariableWithRunTimeString( - "IP:dynamic", + "IP", std::move(dictElement) ) { } diff --git a/src/variables/resource.h b/src/variables/resource.h index f3ad152b..fba92c1b 100644 --- a/src/variables/resource.h +++ b/src/variables/resource.h @@ -42,7 +42,7 @@ class Resource_DictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { t->m_collections.m_resource_collection->resolveMultiMatches( - m_name, t->m_collections.m_resource_collection_key, + *getVariableKey(), t->m_collections.m_resource_collection_key, t->m_rules->m_secWebAppId.m_value, l, m_keyExclusion); } @@ -57,7 +57,7 @@ class Resource_NoDictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { - t->m_collections.m_resource_collection->resolveMultiMatches(m_name, + t->m_collections.m_resource_collection->resolveMultiMatches(*getVariableKey(), t->m_collections.m_resource_collection_key, t->m_rules->m_secWebAppId.m_value, l, m_keyExclusion); } @@ -67,7 +67,7 @@ class Resource_NoDictElement : public Variable { class Resource_DictElementRegexp : public VariableRegex { public: explicit Resource_DictElementRegexp(const std::string &dictElement) - : VariableRegex("RESOURCE:", dictElement), + : VariableRegex("RESOURCE", dictElement), m_dictElement(dictElement) { } void evaluate(Transaction *t, @@ -85,7 +85,7 @@ class Resource_DynamicElement : public VariableWithRunTimeString { public: explicit Resource_DynamicElement(std::unique_ptr dictElement) : VariableWithRunTimeString( - "RESOURCE:dynamic", + "RESOURCE", std::move(dictElement) ) { } diff --git a/src/variables/session.h b/src/variables/session.h index 0ac59bc8..7dca903f 100644 --- a/src/variables/session.h +++ b/src/variables/session.h @@ -41,7 +41,7 @@ class Session_DictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { t->m_collections.m_session_collection->resolveMultiMatches( - m_name, t->m_collections.m_session_collection_key, + *getVariableKey(), t->m_collections.m_session_collection_key, t->m_rules->m_secWebAppId.m_value, l, m_keyExclusion); } @@ -84,7 +84,7 @@ class Session_DynamicElement : public VariableWithRunTimeString { public: explicit Session_DynamicElement(std::unique_ptr dictElement) : VariableWithRunTimeString( - "SESSION:dynamic", + "SESSION", std::move(dictElement) ) { } diff --git a/src/variables/tx.h b/src/variables/tx.h index 3b2ce073..a2598034 100644 --- a/src/variables/tx.h +++ b/src/variables/tx.h @@ -42,7 +42,7 @@ class Tx_DictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { t->m_collections.m_tx_collection->resolveMultiMatches( - m_name, l, m_keyExclusion); + *getVariableKey(), l, m_keyExclusion); } std::string m_dictElement; @@ -82,7 +82,7 @@ class Tx_DynamicElement : public VariableWithRunTimeString { public: explicit Tx_DynamicElement(std::unique_ptr dictElement) : VariableWithRunTimeString( - "TX:dynamic", + "TX", std::move(dictElement) ) { } diff --git a/src/variables/user.h b/src/variables/user.h index 188df8fe..5c41a732 100644 --- a/src/variables/user.h +++ b/src/variables/user.h @@ -42,7 +42,7 @@ class User_DictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { t->m_collections.m_user_collection->resolveMultiMatches( - m_name, t->m_collections.m_user_collection_key, + *getVariableKey(), t->m_collections.m_user_collection_key, t->m_rules->m_secWebAppId.m_value, l, m_keyExclusion); } @@ -57,7 +57,7 @@ class User_NoDictElement : public Variable { void evaluate(Transaction *t, std::vector> *l) override { - t->m_collections.m_user_collection->resolveMultiMatches(m_name, + t->m_collections.m_user_collection->resolveMultiMatches(*getVariableKey(), t->m_collections.m_user_collection_key, t->m_rules->m_secWebAppId.m_value, l, m_keyExclusion); } @@ -85,7 +85,7 @@ class User_DynamicElement : public VariableWithRunTimeString { public: explicit User_DynamicElement(std::unique_ptr dictElement) : VariableWithRunTimeString( - "USER:dynamic", + "USER", std::move(dictElement) ) { } diff --git a/src/variables/variable.cc b/src/variables/variable.cc index 416ca9f4..161320b9 100644 --- a/src/variables/variable.cc +++ b/src/variables/variable.cc @@ -29,31 +29,25 @@ namespace variables { Variable::Variable(const std::string &name) - : m_name(name), - m_collectionName("") { - size_t a = m_name.find(":"); + : m_keyWithCollection(new std::string("")), + m_keyExclusion(), + m_collectionName(new std::string("")), + m_key(new std::string("")) { + size_t a = name.find(":"); if (a == std::string::npos) { - a = m_name.find("."); + a = 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); + m_collectionName->assign(utils::string::toupper(std::string(name, 0, a))); + m_key->assign(std::string(name, a + 1, name.size())); + m_keyWithCollection->assign(*m_collectionName.get() + ":" + std::string(name, a + 1, name.size())); } else { - m_fullName = std::make_shared(m_name); - m_collectionName = m_name; - m_name = ""; + m_keyWithCollection->assign(name); + m_collectionName->assign(name); } } -Variable::Variable(Variable *var) : - m_name(var->m_name), - m_collectionName(var->m_collectionName), - m_fullName(var->m_fullName) { } - - void Variable::addsKeyExclusion(Variable *v) { std::unique_ptr r; VariableModificatorExclusion *ve = \ @@ -67,7 +61,7 @@ void Variable::addsKeyExclusion(Variable *v) { vr = dynamic_cast(ve->m_base.get()); if (vr == NULL) { - r.reset(new KeyExclusionString(v->m_name)); + r.reset(new KeyExclusionString(*v->getVariableKey())); } else { r.reset(new KeyExclusionRegex(vr->m_regex)); } @@ -77,7 +71,7 @@ void Variable::addsKeyExclusion(Variable *v) { std::string operator+(const std::string &a, Variable *v) { - return a + *v->m_fullName.get(); + return a + *v->getVariableKeyWithCollection(); } diff --git a/src/variables/variable.h b/src/variables/variable.h index b636337c..202f674e 100644 --- a/src/variables/variable.h +++ b/src/variables/variable.h @@ -554,26 +554,35 @@ class Variable : public VariableMonkeyResolution { public: explicit Variable(const std::string &name); - explicit Variable(Variable *_name); - Variable(const Variable &v) - : m_name(v.m_name), - m_collectionName(v.m_collectionName), - m_fullName(v.m_fullName), - m_keyExclusion(v.m_keyExclusion) + explicit Variable(Variable *var) + : m_keyWithCollection(var->m_keyWithCollection), + m_keyExclusion(var->m_keyExclusion), + m_collectionName(var->m_collectionName), + m_key(var->m_key) { }; - virtual ~Variable() { } + + Variable(const Variable &v) + : m_keyWithCollection(v.m_keyWithCollection), + m_keyExclusion(v.m_keyExclusion), + m_collectionName(v.m_collectionName), + m_key(v.m_key) + { }; + + + virtual ~Variable() + { }; virtual void evaluate(Transaction *t, std::vector> *l) = 0; - bool inline belongsToCollection(Variable *var) { - return m_collectionName.size() == var->m_collectionName.size() - && std::equal(m_collectionName.begin(), m_collectionName.end(), - var->m_collectionName.begin(), + bool inline belongsToCollection(Variable *var) const noexcept { + return m_collectionName->size() == var->m_collectionName->size() + && std::equal(m_collectionName->begin(), m_collectionName->end(), + var->m_collectionName->begin(), [](char aa, char bb) { return toupper(aa) == bb; }); @@ -583,22 +592,65 @@ class Variable : public VariableMonkeyResolution { void addsKeyExclusion(Variable *v); - bool operator==(const Variable& b) const { - return m_collectionName == b.m_collectionName && - m_name == b.m_name && - *m_fullName == *b.m_fullName; + bool operator==(const Variable& b) const noexcept { + return *m_collectionName == *b.m_collectionName && + *m_key == *b.m_key && + *m_keyWithCollection == *b.m_keyWithCollection; } - std::string& operator+=(const char * p) { return m_name; } + std::string& operator+=(const char *p) { return *m_key; } - std::string m_name; - std::string m_collectionName; - std::shared_ptr m_fullName; + /** + * + * Returns the variable key, if any is specified. + * + * 'ENV:a' will return 'a' + * 'ENV' will return a blank std::shared_ptr. + * + **/ + std::shared_ptr getVariableKey() const noexcept { + return m_key; + } + + /** + * + * Returns the Collection name. + * + * 'ENV:a' will return 'ENV' + * 'ENV' will return 'ENV' + * + **/ + std::shared_ptr getVariableCollection() const noexcept { + return m_collectionName; + } + + + /** + * + * Returns the Collection with key name. + * + * 'ENV:a' will return 'ENV:a' + * 'ENV' will return 'ENV' + * + **/ + std::shared_ptr getVariableKeyWithCollection() const noexcept { + return m_keyWithCollection; + } + + + protected: KeyExclusions m_keyExclusion; + + private: + std::shared_ptr m_keyWithCollection; + std::shared_ptr m_key; + std::shared_ptr m_collectionName; }; + + class VariableDictElement : public Variable { public: VariableDictElement(const std::string &name, const std::string &dict_element) @@ -649,7 +701,7 @@ class Variables : public std::vector { if (r) { return r->m_r.searchAll(v->getKey()).size() > 0; } - return v->getKeyWithCollection() == *m->m_fullName.get(); + return v->getKeyWithCollection() == *m->getVariableKeyWithCollection(); }) != end(); }; @@ -658,9 +710,9 @@ class Variables : public std::vector { std::string names; for (auto a : *this) { if (names.length() > 0) { - names = names + sep + *a->m_fullName; + names = names + sep + *a->getVariableKeyWithCollection(); } else { - names = *a->m_fullName; + names = *a->getVariableKeyWithCollection(); } } return names; @@ -699,7 +751,7 @@ class VariableModificatorCount : public Variable { auto count = reslIn.size(); std::string res(std::to_string(count)); - l->push_back(std::make_shared(m_fullName.get(), &res)); + l->push_back(std::make_shared(getVariableKeyWithCollection().get(), &res)); return; } diff --git a/src/variables/web_app_id.h b/src/variables/web_app_id.h index e36daf5c..77a30fb6 100644 --- a/src/variables/web_app_id.h +++ b/src/variables/web_app_id.h @@ -37,9 +37,8 @@ class WebAppId : public Variable { void evaluate(Transaction *transaction, std::vector> *l) override { - const std::string name("WEBAPPID"); const std::string rname = transaction->m_rules->m_secWebAppId.m_value; - l->push_back(std::make_shared(&m_name, &rname)); + l->push_back(std::make_shared(getVariableKeyWithCollection().get(), &rname)); } }; diff --git a/src/variables/xml.cc b/src/variables/xml.cc index e343a87e..8d22ea4b 100644 --- a/src/variables/xml.cc +++ b/src/variables/xml.cc @@ -63,7 +63,7 @@ void XML_WithNSPath::evaluate(Transaction *t, int i; //size_t pos; - param = m_name; + param = *getVariableKey(); /* pos = m_name.find_first_of(":"); if (pos == std::string::npos) { @@ -124,8 +124,8 @@ void XML_WithNSPath::evaluate(Transaction *t, xmlNodeGetContent(nodes->nodeTab[i])); if (content != NULL) { std::string a(content); - if (!m_keyExclusion.toOmit(*m_fullName)) { - l->push_back(std::make_shared(m_fullName.get(), &a)); + if (!m_keyExclusion.toOmit(*getVariableKeyWithCollection())) { + l->push_back(std::make_shared(getVariableKeyWithCollection().get(), &a)); } xmlFree(content); } diff --git a/src/variables/xml.h b/src/variables/xml.h index 90437621..c35ffc43 100644 --- a/src/variables/xml.h +++ b/src/variables/xml.h @@ -40,7 +40,7 @@ class XML_WithoutNSPath : public RuleVariable, public Variable { : RuleVariable(), Variable("XML"), m_plain("[XML document tree]"), - m_var(std::make_shared(&m_name, &m_plain)) + m_var(std::make_shared(getVariableKeyWithCollection().get(), &m_plain)) { }; XML_WithoutNSPath(const XML_WithoutNSPath &r)