Refactoring on the VariableValue class

This commit is contained in:
Felipe Zimmerle
2019-06-14 10:13:54 -03:00
parent cbd15ec138
commit 47dd9c5df4
16 changed files with 147 additions and 112 deletions

View File

@@ -31,7 +31,7 @@ void HighestSeverity::evaluate(Transaction *transaction,
std::vector<const VariableValue *> *l) {
transaction->m_variableHighestSeverityAction.assign(
std::to_string(transaction->m_highestSeverityAction));
l->push_back(new VariableValue(m_fullName,
l->push_back(new VariableValue(m_fullName.get(),
&transaction->m_variableHighestSeverityAction));
}

View File

@@ -31,13 +31,12 @@ namespace variables {
class HighestSeverity : public Variable {
public:
explicit HighestSeverity(std::string _name)
: Variable(_name),
m_retName("HIGHEST_SEVERITY") { }
: Variable(_name)
{ }
void evaluate(Transaction *transaction,
Rule *rule,
std::vector<const VariableValue *> *l) override;
std::string m_retName;
};

View File

@@ -52,7 +52,7 @@ void RemoteUser::evaluate(Transaction *transaction,
goto clear;
}
header = std::string(l2->at(0)->m_value);
header = std::string(l2->at(0)->getValue());
if (header.compare(0, 6, "Basic ") == 0) {
base64 = std::string(header, 6, header.length());
@@ -66,14 +66,14 @@ void RemoteUser::evaluate(Transaction *transaction,
}
transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos));
var = new VariableValue(&l2->at(0)->m_key,
var = new VariableValue(&l2->at(0)->getKeyWithCollection(),
&transaction->m_variableRemoteUser);
for (auto &i : l2->at(0)->m_orign) {
for (auto &i : l2->at(0)->getOrigin()) {
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
origin->m_offset = i->m_offset;
origin->m_length = i->m_length;
var->m_orign.push_back(std::move(origin));
var->addOrigin(std::move(origin));
}
l->push_back(var);

32
src/variables/rule.cc Normal file
View File

@@ -0,0 +1,32 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include "src/variables/rule.h"
namespace modsecurity {
namespace variables {
const std::string Rule_DictElement::m_rule("RULE");
const std::string Rule_DictElement::m_rule_id("id");
const std::string Rule_DictElement::m_rule_rev("rev");
const std::string Rule_DictElement::m_rule_severity("severity");
const std::string Rule_DictElement::m_rule_logdata("logdata");
const std::string Rule_DictElement::m_rule_msg("msg");
} // namespace variables
} // namespace modsecurity

View File

@@ -35,7 +35,7 @@ namespace variables {
class Rule_DictElement : public VariableDictElement { \
public:
explicit Rule_DictElement(std::string dictElement)
: VariableDictElement("RULE", dictElement) { }
: VariableDictElement(std::string("RULE"), dictElement) { }
static void id(Transaction *t,
Rule *rule,
@@ -51,14 +51,13 @@ class Rule_DictElement : public VariableDictElement { \
}
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
std::string *a = new std::string(std::to_string(r->m_ruleId));
VariableValue *var = new VariableValue(
std::make_shared<std::string>("RULE:id"),
VariableValue *var = new VariableValue(&m_rule, &m_rule_id,
a
);
delete a;
origin->m_offset = 0;
origin->m_length = 0;
var->m_orign.push_back(std::move(origin));
var->addOrigin(std::move(origin));
l->push_back(var);
}
@@ -78,14 +77,13 @@ class Rule_DictElement : public VariableDictElement { \
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
std::string *a = new std::string(r->m_rev);
VariableValue *var = new VariableValue(
std::make_shared<std::string>("RULE:rev"),
VariableValue *var = new VariableValue(&m_rule, &m_rule_rev,
a
);
delete a;
origin->m_offset = 0;
origin->m_length = 0;
var->m_orign.push_back(std::move(origin));
var->addOrigin(std::move(origin));
l->push_back(var);
}
@@ -102,14 +100,13 @@ class Rule_DictElement : public VariableDictElement { \
if (r && r->m_severity) {
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
std::string *a = new std::string(std::to_string(r->m_severity->m_severity));
VariableValue *var = new VariableValue(
std::make_shared<std::string>("RULE:severity"),
VariableValue *var = new VariableValue(&m_rule, &m_rule_severity,
a
);
delete a;
origin->m_offset = 0;
origin->m_length = 0;
var->m_orign.push_back(std::move(origin));
var->addOrigin(std::move(origin));
l->push_back(var);
}
}
@@ -127,14 +124,13 @@ class Rule_DictElement : public VariableDictElement { \
if (r && r->m_logData) {
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
std::string *a = new std::string(r->m_logData->data(t));
VariableValue *var = new VariableValue(
std::make_shared<std::string>("RULE:logdata"),
VariableValue *var = new VariableValue(&m_rule, &m_rule_logdata,
a
);
delete a;
origin->m_offset = 0;
origin->m_length = 0;
var->m_orign.push_back(std::move(origin));
var->addOrigin(std::move(origin));
l->push_back(var);
}
}
@@ -151,14 +147,13 @@ class Rule_DictElement : public VariableDictElement { \
if (r && r->m_msg) {
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
std::string *a = new std::string(r->m_msg->data(t));
VariableValue *var = new VariableValue(
std::make_shared<std::string>("RULE:msg"),
VariableValue *var = new VariableValue(&m_rule, &m_rule_msg,
a
);
delete a;
origin->m_offset = 0;
origin->m_length = 0;
var->m_orign.push_back(std::move(origin));
var->addOrigin(std::move(origin));
l->push_back(var);
}
}
@@ -187,6 +182,13 @@ class Rule_DictElement : public VariableDictElement { \
return;
}
}
static const std::string m_rule;
static const std::string m_rule_id;
static const std::string m_rule_rev;
static const std::string m_rule_severity;
static const std::string m_rule_logdata;
static const std::string m_rule_msg;
};

View File

@@ -658,7 +658,7 @@ class VariableModificatorCount : public Variable {
reslIn.clear();
std::string *res = new std::string(std::to_string(count));
val = new VariableValue(m_fullName, res);
val = new VariableValue(m_fullName.get(), res);
delete res;
l->push_back(val);

View File

@@ -125,7 +125,7 @@ void XML::evaluate(Transaction *t,
xmlNodeGetContent(nodes->nodeTab[i]));
if (content != NULL) {
std::string *a = new std::string(content);
VariableValue *var = new VariableValue(m_fullName,
VariableValue *var = new VariableValue(m_fullName.get(),
a);
if (!m_keyExclusion.toOmit(*m_fullName)) {
l->push_back(var);