Skip to content

Commit f17af95

Browse files
author
Felipe Zimmerle
committed
Using RunTimeString on setvar action
1 parent a6830c7 commit f17af95

File tree

14 files changed

+7490
-6735
lines changed

14 files changed

+7490
-6735
lines changed

src/actions/set_var.cc

Lines changed: 51 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -24,99 +24,64 @@
2424
#include "modsecurity/rule.h"
2525
#include "src/macro_expansion.h"
2626
#include "src/utils/string.h"
27-
27+
#include "src/variables/global.h"
28+
#include "src/variables/ip.h"
29+
#include "src/variables/resource.h"
30+
#include "src/variables/session.h"
31+
#include "src/variables/tx.h"
32+
#include "src/variables/user.h"
33+
#include "src/variables/variable.h"
2834

2935
namespace modsecurity {
3036
namespace actions {
3137

3238

3339
bool SetVar::init(std::string *error) {
34-
size_t pos;
35-
36-
if (m_variableName.empty() == false) {
37-
pos = m_variableName.find(".");
38-
if (pos != std::string::npos) {
39-
m_collectionName = std::string(m_variableName, 0, pos);
40-
m_collectionName = utils::string::toupper(m_collectionName);
41-
m_variableName = std::string(m_variableName, pos + 1,
42-
m_variableName.size() - (pos + 1));
43-
} else {
44-
error->assign("Missing the collection and/or variable name");
45-
return false;
46-
}
47-
return true;
48-
}
40+
return true;
41+
}
4942

50-
// Resolv operation
51-
m_operation = setToOneOperation;
52-
pos = m_parser_payload.find("=");
53-
if (pos != std::string::npos) {
54-
m_operation = setOperation;
55-
}
56-
pos = m_parser_payload.find("=+");
57-
if (pos != std::string::npos) {
58-
m_operation = sumAndSetOperation;
59-
}
60-
pos = m_parser_payload.find("=-");
61-
if (pos != std::string::npos) {
62-
m_operation = substractAndSetOperation;
63-
}
6443

65-
// Collection name
66-
pos = m_parser_payload.find(".");
67-
if (pos != std::string::npos) {
68-
m_collectionName = std::string(m_parser_payload, 0, pos);
69-
m_collectionName = utils::string::toupper(m_collectionName);
70-
} else {
71-
error->assign("Missing the collection and/or variable name");
72-
return false;
73-
}
44+
bool SetVar::evaluate(Rule *rule, Transaction *t) {
45+
std::string targetValue;
46+
std::string resolvedPre;
7447

75-
// Variable name
76-
if (m_operation == setToOneOperation) {
77-
m_variableName = std::string(m_parser_payload, pos + 1,
78-
m_parser_payload.length()
79-
- (pos + 1));
80-
} else {
81-
size_t pos2 = m_parser_payload.find("=");
82-
m_variableName = std::string(m_parser_payload, pos + 1,
83-
pos2 - (pos + 1));
84-
if (pos2 + 2 > m_parser_payload.length()) {
85-
m_predicate = "";
86-
} else {
87-
if (m_operation == setOperation) {
88-
m_predicate = std::string(m_parser_payload, pos2 + 1,
89-
m_parser_payload.length() - (pos2));
90-
} else {
91-
m_predicate = std::string(m_parser_payload, pos2 + 2,
92-
m_parser_payload.length()
93-
- (pos2 + 1));
94-
}
95-
}
48+
if (m_string) {
49+
resolvedPre = m_string->evaluate(t);
9650
}
9751

98-
if (m_collectionName.empty() || m_variableName.empty()) {
99-
error->assign("Something wrong with the input format");
100-
return false;
52+
std::string m_variableNameExpanded;
53+
std::vector<const collection::Variable *> l;
54+
55+
auto *v = m_variable.get();
56+
Variables::Tx_DynamicElement *tx = dynamic_cast<Variables::Tx_DynamicElement *> (v);
57+
Variables::Session_DynamicElement *session = dynamic_cast<Variables::Session_DynamicElement *> (v);
58+
Variables::Ip_DynamicElement *ip = dynamic_cast<Variables::Ip_DynamicElement *> (v);
59+
Variables::Resource_DynamicElement *resource = dynamic_cast<Variables::Resource_DynamicElement *> (v);
60+
Variables::Global_DynamicElement *global = dynamic_cast<Variables::Global_DynamicElement *> (v);
61+
Variables::User_DynamicElement *user = dynamic_cast<Variables::User_DynamicElement *> (v);
62+
if (tx) {
63+
m_variableNameExpanded = tx->m_string->evaluate(t);
64+
} else if (session) {
65+
m_variableNameExpanded = session->m_string->evaluate(t);
66+
} else if (ip) {
67+
m_variableNameExpanded = ip->m_string->evaluate(t);
68+
} else if (resource) {
69+
m_variableNameExpanded = resource->m_string->evaluate(t);
70+
} else if (global) {
71+
m_variableNameExpanded = global->m_string->evaluate(t);
72+
} else if (user) {
73+
m_variableNameExpanded = user->m_string->evaluate(t);
74+
} else {
75+
m_variableNameExpanded = m_variable->m_name;
10176
}
10277

103-
return true;
104-
}
105-
106-
107-
bool SetVar::evaluate(Rule *rule, Transaction *transm_parser_payload) {
108-
std::string targetValue;
109-
std::string m_variableNameExpanded = MacroExpansion::expand(m_variableName,
110-
rule, transm_parser_payload);
111-
std::string resolvedPre = MacroExpansion::expand(m_predicate,
112-
rule, transm_parser_payload);
113-
11478
if (m_operation == setOperation) {
11579
targetValue = resolvedPre;
11680
} else if (m_operation == setToOneOperation) {
11781
targetValue = std::string("1");
11882
} else if (m_operation == unsetOperation) {
119-
transm_parser_payload->m_collections.del(m_collectionName + ":" +
83+
//m_variable
84+
t->m_collections.del(m_variable->m_collectionName + ":" +
12085
m_variableNameExpanded);
12186
goto end;
12287
} else {
@@ -130,14 +95,15 @@ bool SetVar::evaluate(Rule *rule, Transaction *transm_parser_payload) {
13095
}
13196

13297
try {
133-
std::unique_ptr<std::string> resolvedValue =
134-
transm_parser_payload->m_collections.resolveFirst(
135-
m_collectionName,
136-
m_variableNameExpanded);
137-
if (resolvedValue == NULL || resolvedValue->empty()) {
98+
std::vector<const collection::Variable *> l;
99+
m_variable->evaluate(t, rule, &l);
100+
if (l.size() == 0) {
138101
value = 0;
139102
} else {
140-
value = stoi(*resolvedValue);
103+
value = stoi(l[0]->m_value);
104+
for (auto &i : l) {
105+
delete i;
106+
}
141107
}
142108
} catch (...) {
143109
value = 0;
@@ -151,13 +117,12 @@ bool SetVar::evaluate(Rule *rule, Transaction *transm_parser_payload) {
151117
}
152118

153119
#ifndef NO_LOGS
154-
transm_parser_payload->debug(8, "Saving variable: " + m_collectionName \
120+
t->debug(8, "Saving variable: " + m_variable->m_collectionName \
155121
+ ":" + m_variableNameExpanded + " with value: " + targetValue);
156122
#endif
157-
transm_parser_payload->m_collections.storeOrUpdateFirst(m_collectionName,
158-
m_variableNameExpanded,
159-
transm_parser_payload->m_rules->m_secWebAppId.m_value, targetValue);
160-
123+
t->m_collections.storeOrUpdateFirst(m_variable->m_collectionName,
124+
m_variableNameExpanded,
125+
t->m_rules->m_secWebAppId.m_value, targetValue);
161126
end:
162127
return true;
163128
}

src/actions/set_var.h

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string>
1717

1818
#include "modsecurity/actions/action.h"
19+
#include "src/run_time_string.h"
1920

2021
#ifndef SRC_ACTIONS_SET_VAR_H_
2122
#define SRC_ACTIONS_SET_VAR_H_
@@ -41,35 +42,27 @@ enum SetVarOperation {
4142

4243
class SetVar : public Action {
4344
public:
44-
explicit SetVar(std::string action) : Action(action),
45-
m_operation(SetVarOperation::setOperation),
46-
m_collectionName(""),
47-
m_variableName(""),
48-
m_predicate("") { }
49-
5045
SetVar(SetVarOperation operation,
51-
std::string variableName,
52-
std::string predicate) : Action("setvar"),
46+
std::unique_ptr<modsecurity::Variables::Variable> variable,
47+
std::unique_ptr<RunTimeString> predicate)
48+
: Action("setvar"),
5349
m_operation(operation),
54-
m_collectionName(""),
55-
m_variableName(variableName),
56-
m_predicate(predicate) { }
50+
m_variable(std::move(variable)),
51+
m_string(std::move(predicate)) { }
5752

5853
SetVar(SetVarOperation operation,
59-
std::string variableName) : Action("setvar"),
54+
std::unique_ptr<modsecurity::Variables::Variable> variable)
55+
: Action("setvar"),
6056
m_operation(operation),
61-
m_collectionName(""),
62-
m_variableName(variableName),
63-
m_predicate("") { }
57+
m_variable(std::move(variable)) { }
6458

6559
bool evaluate(Rule *rule, Transaction *transaction) override;
6660
bool init(std::string *error) override;
6761

6862
private:
6963
SetVarOperation m_operation;
70-
std::string m_collectionName;
71-
std::string m_variableName;
72-
std::string m_predicate;
64+
std::unique_ptr<modsecurity::Variables::Variable> m_variable;
65+
std::unique_ptr<RunTimeString> m_string;
7366
};
7467

7568
} // namespace actions

0 commit comments

Comments
 (0)