24
24
#include " modsecurity/rule.h"
25
25
#include " src/macro_expansion.h"
26
26
#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"
28
34
29
35
namespace modsecurity {
30
36
namespace actions {
31
37
32
38
33
39
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
+ }
49
42
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
- }
64
43
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;
74
47
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);
96
50
}
97
51
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 ;
101
76
}
102
77
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
-
114
78
if (m_operation == setOperation) {
115
79
targetValue = resolvedPre;
116
80
} else if (m_operation == setToOneOperation) {
117
81
targetValue = std::string (" 1" );
118
82
} 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 + " :" +
120
85
m_variableNameExpanded);
121
86
goto end;
122
87
} else {
@@ -130,14 +95,15 @@ bool SetVar::evaluate(Rule *rule, Transaction *transm_parser_payload) {
130
95
}
131
96
132
97
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 ) {
138
101
value = 0 ;
139
102
} else {
140
- value = stoi (*resolvedValue);
103
+ value = stoi (l[0 ]->m_value );
104
+ for (auto &i : l) {
105
+ delete i;
106
+ }
141
107
}
142
108
} catch (...) {
143
109
value = 0 ;
@@ -151,13 +117,12 @@ bool SetVar::evaluate(Rule *rule, Transaction *transm_parser_payload) {
151
117
}
152
118
153
119
#ifndef NO_LOGS
154
- transm_parser_payload ->debug (8 , " Saving variable: " + m_collectionName \
120
+ t ->debug (8 , " Saving variable: " + m_variable-> m_collectionName \
155
121
+ " :" + m_variableNameExpanded + " with value: " + targetValue);
156
122
#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);
161
126
end:
162
127
return true ;
163
128
}
0 commit comments