Skip to content

Commit 3e8e28d

Browse files
author
Felipe Zimmerle
committed
Refactoring on the RULE variable
1 parent 554251b commit 3e8e28d

File tree

11 files changed

+188
-40
lines changed

11 files changed

+188
-40
lines changed

headers/modsecurity/rule.h

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class Rule {
9494
void updateMatchedVars(Transaction *trasn, std::string key,
9595
std::string value);
9696
void cleanMatchedVars(Transaction *trasn);
97-
void updateRulesVariable(Transaction *trasn, std::shared_ptr<RuleMessage> rm);
9897

9998
std::vector<actions::Action *> getActionsByName(const std::string& name,
10099
Transaction *t);

headers/modsecurity/transaction.h

-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ class TransactionAnchoredVariables {
174174
m_variableResponseHeaders(t, "RESPONSE_HEADERS"),
175175
m_variableGeo(t, "GEO"),
176176
m_variableRequestCookiesNames(t, "REQUEST_COOKIES_NAMES"),
177-
m_variableRule(t, "RULE"),
178177
m_variableFilesTmpNames(t, "FILES_TMPNAMES"),
179178
m_variableOffset(0)
180179
{ }
@@ -256,7 +255,6 @@ class TransactionAnchoredVariables {
256255
AnchoredSetVariable m_variableResponseHeaders;
257256
AnchoredSetVariable m_variableGeo;
258257
AnchoredSetVariable m_variableRequestCookiesNames;
259-
AnchoredSetVariable m_variableRule;
260258
AnchoredSetVariable m_variableFilesTmpNames;
261259

262260
int m_variableOffset;

src/actions/log_data.cc

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ bool LogData::evaluate(Rule *rule, Transaction *transaction,
3333
std::shared_ptr<RuleMessage> rm) {
3434
rm->m_data = data(transaction);
3535

36-
transaction->m_variableRule.set("logdata", rm->m_data, 0);
37-
3836
return true;
3937
}
4038

src/actions/msg.cc

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ bool Msg::evaluate(Rule *rule, Transaction *transaction,
5454
transaction->debug(9, "Saving msg: " + msg);
5555
#endif
5656

57-
transaction->m_variableRule.set("msg", msg, 0);
58-
5957
return true;
6058
}
6159

src/actions/set_var.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool SetVar::evaluate(Rule *rule, Transaction *t) {
4545
std::string resolvedPre;
4646

4747
if (m_string) {
48-
resolvedPre = m_string->evaluate(t);
48+
resolvedPre = m_string->evaluate(t, rule);
4949
}
5050

5151
std::string m_variableNameExpanded;

src/actions/severity.cc

-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ bool Severity::evaluate(Rule *rule, Transaction *transaction,
8484
transaction->m_highestSeverityAction = this->m_severity;
8585
}
8686

87-
transaction->m_variableRule.set("severity", std::to_string(m_severity), 0);
88-
8987
return true;
9088
}
9189

src/rule.cc

-23
Original file line numberDiff line numberDiff line change
@@ -258,27 +258,6 @@ void Rule::cleanMatchedVars(Transaction *trans) {
258258
}
259259

260260

261-
void Rule::updateRulesVariable(Transaction *trans,
262-
std::shared_ptr<RuleMessage> rm) {
263-
if (m_ruleId != 0) {
264-
trans->m_variableRule.set("id", std::to_string(m_ruleId), 0);
265-
}
266-
if (m_rev.empty() == false) {
267-
trans->m_variableRule.set("rev", m_rev, 0);
268-
}
269-
if (m_severity) {
270-
trans->m_variableRule.set("severity",
271-
std::to_string(m_severity->m_severity), 0);
272-
}
273-
if (m_logData) {
274-
trans->m_variableRule.set("logdata", m_logData->data(trans), 0);
275-
}
276-
if (m_msg) {
277-
trans->m_variableRule.set("msg", m_msg->data(trans), 0);
278-
}
279-
}
280-
281-
282261
void Rule::executeActionsIndependentOfChainedRuleResult(Transaction *trans,
283262
bool *containsBlock, std::shared_ptr<RuleMessage> ruleMessage) {
284263

@@ -720,8 +699,6 @@ bool Rule::evaluate(Transaction *trans,
720699
#endif
721700
}
722701

723-
updateRulesVariable(trans, ruleMessage);
724-
725702
getFinalVars(&vars, &exclusion, trans);
726703

727704
for (auto &var : vars) {

src/run_time_string.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ void RunTimeString::appendVar(
4747

4848

4949
std::string RunTimeString::evaluate(Transaction *t) {
50+
return evaluate(t, NULL);
51+
}
52+
53+
54+
std::string RunTimeString::evaluate(Transaction *t, Rule *r) {
5055
std::string s;
5156
for (auto &z : m_elements) {
5257
if (z->m_string.size() > 0) {
5358
s.append(z->m_string);
5459
} else if (z->m_var != NULL && t != NULL) {
5560
std::vector<const VariableValue *> l;
56-
z->m_var->evaluate(t, NULL, &l);
61+
z->m_var->evaluate(t, r, &l);
5762
if (l.size() > 0) {
5863
s.append(l[0]->m_value);
5964
}
@@ -65,5 +70,4 @@ std::string RunTimeString::evaluate(Transaction *t) {
6570
return s;
6671
}
6772

68-
6973
} // namespace modsecurity

src/run_time_string.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class RunTimeString {
4949
void appendText(std::string text);
5050
void appendVar(std::unique_ptr<modsecurity::Variables::Variable> var);
5151
std::string evaluate(Transaction *t);
52+
std::string evaluate(Transaction *t, Rule *r);
5253
std::string evaluate() {
5354
return evaluate(NULL);
5455
}

src/variables/rule.h

+180-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,193 @@
2121
#define SRC_VARIABLES_RULE_H_
2222

2323
#include "src/variables/variable.h"
24+
#include "src/actions/severity.h"
25+
#include "src/actions/log_data.h"
26+
#include "src/actions/msg.h"
27+
2428

2529
namespace modsecurity {
2630

2731
class Transaction;
2832
namespace Variables {
2933

3034

31-
DEFINE_VARIABLE_DICT(Rule, RULE, m_variableRule)
35+
class Rule_DictElement : public VariableDictElement { \
36+
public:
37+
explicit Rule_DictElement(std::string dictElement)
38+
: VariableDictElement("RULE", dictElement) { }
39+
40+
static void id(Transaction *t,
41+
Rule *rule,
42+
std::vector<const VariableValue *> *l) {
43+
if (!rule) {
44+
return;
45+
}
46+
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
47+
std::string *a = new std::string(std::to_string(rule->m_ruleId));
48+
VariableValue *var = new VariableValue(
49+
std::make_shared<std::string>("RULE:id"),
50+
a
51+
);
52+
delete a;
53+
origin->m_offset = 0;
54+
origin->m_length = 0;
55+
var->m_orign.push_back(std::move(origin));
56+
l->push_back(var);
57+
}
58+
59+
60+
static void rev(Transaction *t,
61+
Rule *rule,
62+
std::vector<const VariableValue *> *l) {
63+
if (!rule) {
64+
return;
65+
}
66+
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
67+
std::string *a = new std::string(rule->m_rev);
68+
VariableValue *var = new VariableValue(
69+
std::make_shared<std::string>("RULE:rev"),
70+
a
71+
);
72+
delete a;
73+
origin->m_offset = 0;
74+
origin->m_length = 0;
75+
var->m_orign.push_back(std::move(origin));
76+
l->push_back(var);
77+
}
78+
79+
80+
static void severity(Transaction *t,
81+
Rule *rule,
82+
std::vector<const VariableValue *> *l) {
83+
if (rule && rule->m_severity) {
84+
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
85+
std::string *a = new std::string(std::to_string(rule->m_severity->m_severity));
86+
VariableValue *var = new VariableValue(
87+
std::make_shared<std::string>("RULE:severity"),
88+
a
89+
);
90+
delete a;
91+
origin->m_offset = 0;
92+
origin->m_length = 0;
93+
var->m_orign.push_back(std::move(origin));
94+
l->push_back(var);
95+
}
96+
}
97+
98+
99+
static void logData(Transaction *t,
100+
Rule *rule,
101+
std::vector<const VariableValue *> *l) {
102+
if (rule && rule->m_logData) {
103+
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
104+
std::string *a = new std::string(rule->m_logData->data(t));
105+
VariableValue *var = new VariableValue(
106+
std::make_shared<std::string>("RULE:logdata"),
107+
a
108+
);
109+
delete a;
110+
origin->m_offset = 0;
111+
origin->m_length = 0;
112+
var->m_orign.push_back(std::move(origin));
113+
l->push_back(var);
114+
}
115+
}
116+
117+
static void msg(Transaction *t,
118+
Rule *rule,
119+
std::vector<const VariableValue *> *l) {
120+
if (rule && rule->m_msg) {
121+
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
122+
std::string *a = new std::string(rule->m_msg->data(t));
123+
VariableValue *var = new VariableValue(
124+
std::make_shared<std::string>("RULE:msg"),
125+
a
126+
);
127+
delete a;
128+
origin->m_offset = 0;
129+
origin->m_length = 0;
130+
var->m_orign.push_back(std::move(origin));
131+
l->push_back(var);
132+
}
133+
}
134+
135+
void evaluate(Transaction *t,
136+
Rule *rule,
137+
std::vector<const VariableValue *> *l) override {
138+
if (m_dictElement == "id") {
139+
id(t, rule, l);
140+
return;
141+
}
142+
if (rule && m_dictElement == "rev") {
143+
rev(t, rule, l);
144+
return;
145+
}
146+
if (rule && m_dictElement == "severity") {
147+
severity(t, rule, l);
148+
return;
149+
}
150+
if (m_dictElement == "logdata") {
151+
logData(t, rule, l);
152+
return;
153+
}
154+
if (m_dictElement == "msg") {
155+
msg(t, rule, l);
156+
return;
157+
}
158+
}
159+
};
160+
161+
162+
class Rule_DictElementRegexp : public VariableRegex {
163+
public:
164+
explicit Rule_DictElementRegexp(std::string regex)
165+
: VariableRegex("RULE", regex) { }
166+
167+
void evaluate(Transaction *t,
168+
Rule *rule,
169+
std::vector<const VariableValue *> *l) override {
170+
if (Utils::regex_search("id", m_r) > 0) {
171+
Rule_DictElement::id(t, rule, l);
172+
return;
173+
}
174+
if (Utils::regex_search("rev", m_r) > 0) {
175+
Rule_DictElement::rev(t, rule, l);
176+
return;
177+
}
178+
if (Utils::regex_search("severity", m_r) > 0) {
179+
Rule_DictElement::severity(t, rule, l);
180+
return;
181+
}
182+
if (Utils::regex_search("logdata", m_r) > 0) {
183+
Rule_DictElement::logData(t, rule, l);
184+
return;
185+
}
186+
if (Utils::regex_search("msg", m_r) > 0) {
187+
Rule_DictElement::msg(t, rule, l);
188+
return;
189+
}
190+
}
191+
};
192+
193+
194+
class Rule_NoDictElement : public Variable {
195+
public:
196+
explicit Rule_NoDictElement()
197+
: Variable("RULE") { }
198+
199+
void evaluate(Transaction *t,
200+
Rule *rule,
201+
std::vector<const VariableValue *> *l) override {
202+
Rule_DictElement::id(t, rule, l);
203+
Rule_DictElement::rev(t, rule, l);
204+
Rule_DictElement::severity(t, rule, l);
205+
Rule_DictElement::logData(t, rule, l);
206+
Rule_DictElement::msg(t, rule, l);
207+
}
208+
};
209+
210+
// DEFINE_VARIABLE_DICT(Rule, RULE, m_variableRule)
32211

33212

34213
} // namespace Variables

src/variables/variable.h

-4
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ class VariableMonkeyResolution {
292292
t->m_variableArgsGetNames.resolve(var, l);
293293
} else if (comp(variable, "ARGS_POST_NAMES")) {
294294
t->m_variableArgsPostNames.resolve(var, l);
295-
} else if (comp(col, "RULE")) {
296-
t->m_variableRule.resolve(var, l);
297295
} else if (comp(col, "ARGS_GET")) {
298296
t->m_variableArgsGet.resolve(var, l);
299297
} else if (comp(col, "ARGS_POST")) {
@@ -474,8 +472,6 @@ class VariableMonkeyResolution {
474472
vv = t->m_variableArgsGetNames.resolveFirst(var);
475473
} else if (comp(variable, "ARGS_POST_NAMES")) {
476474
vv = t->m_variableArgsPostNames.resolveFirst(var);
477-
} else if (comp(col, "RULE")) {
478-
vv = t->m_variableRule.resolveFirst(var);
479475
} else if (comp(col, "ARGS_GET")) {
480476
vv = t->m_variableArgsGet.resolveFirst(var);
481477
} else if (comp(col, "ARGS_POST")) {

0 commit comments

Comments
 (0)