-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathin_memory-per_process.cc
257 lines (206 loc) · 7.7 KB
/
in_memory-per_process.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2023 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 [email protected].
*
*/
#include "src/collection/backend/in_memory-per_process.h"
#include "src/collection/backend/collection_data.h"
#ifdef __cplusplus
#include <string>
#include <iostream>
#include <unordered_map>
#include <list>
#include <memory>
#endif
#include "modsecurity/variable_value.h"
#include "src/utils/regex.h"
#include "src/utils/string.h"
namespace modsecurity {
namespace collection {
namespace backend {
InMemoryPerProcess::InMemoryPerProcess(const std::string &name) :
Collection(name) {
m_map.reserve(1000);
}
InMemoryPerProcess::~InMemoryPerProcess() {
m_map.clear();
}
template<typename Map>
inline void __store(Map &map, std::string key, std::string value) {
// NOTE: should be called with write-lock previously acquired
map.emplace(key, value);
}
template<typename Map>
inline bool __updateFirst(Map &map,
const std::string &key,
const std::string &value) {
// NOTE: should be called with write-lock previously acquired
if (auto search = map.find(key); search != map.end()) {
search->second.setValue(value);
return true;
}
return false;
}
void InMemoryPerProcess::store(const std::string &key, const std::string &value) {
const std::lock_guard lock(m_mutex); // write lock (exclusive access)
__store(m_map, key, value);
}
bool InMemoryPerProcess::storeOrUpdateFirst(const std::string &key,
const std::string &value) {
const std::lock_guard lock(m_mutex); // write lock (exclusive access)
if (__updateFirst(m_map, key, value) == false) {
__store(m_map, key, value);
}
return true;
}
bool InMemoryPerProcess::updateFirst(const std::string &key,
const std::string &value) {
const std::lock_guard lock(m_mutex); // write lock (exclusive access)
return __updateFirst(m_map, key, value);
}
void InMemoryPerProcess::del(const std::string& key) {
const std::lock_guard lock(m_mutex); // write lock (exclusive access)
m_map.erase(key);
}
void InMemoryPerProcess::delIfExpired(const std::string& key) {
const std::lock_guard lock(m_mutex); // write lock (exclusive access)
// Double check the status while within the mutex
const auto iter = std::find_if(m_map.begin(), m_map.end(),
[&key](const auto &x) { return x.first == key && x.second.isExpired(); });
if (iter != m_map.end()) {
m_map.erase(key);
}
}
void InMemoryPerProcess::setExpiry(const std::string& key, int32_t expiry_seconds) {
const std::lock_guard lock(m_mutex); // write lock (exclusive access)
if (const auto search = m_map.find(key); search != m_map.end()) {
search->second.setExpiry(expiry_seconds);
return;
}
// We allow an expiry value to be set for a key that has not (yet) had a value set.
const auto iter = m_map.emplace(key, CollectionData());
iter->second.setExpiry(expiry_seconds);
}
void InMemoryPerProcess::resolveSingleMatch(const std::string& var,
std::vector<const VariableValue *> *l) {
std::list<std::string> expiredVars;
{
const std::shared_lock lock(m_mutex); // read lock (shared access)
const auto range = m_map.equal_range(var);
for (auto it = range.first; it != range.second; ++it) {
if (it->second.isExpired()) {
expiredVars.push_back(it->first);
} else if (it->second.hasValue() == false) {
// No-op. A non-expired expiry exists for the key but there is no actual value
} else {
l->push_back(new VariableValue(&m_name, &it->first, &it->second.getValue()));
}
}
}
for (const auto& expiredVar : expiredVars) {
delIfExpired(expiredVar);
}
}
void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
std::vector<const VariableValue *> *l, variables::KeyExclusions &ke) {
const auto keySize = var.size();
l->reserve(15);
std::list<std::string> expiredVars;
{
const std::shared_lock lock(m_mutex); // read lock (shared access)
if (keySize == 0) {
for (auto &i : m_map) {
if (ke.toOmit(i.first)) {
continue;
}
if (i.second.isExpired()) {
expiredVars.push_back(i.first);
} else if (i.second.hasValue() == false) {
// No-op. A non-expired expiry exists for the key but there is no actual value
} else {
l->insert(l->begin(), new VariableValue(&m_name, &i.first,
&i.second.getValue()));
}
}
} else {
const auto range = m_map.equal_range(var);
for (auto it = range.first; it != range.second; ++it) {
if (ke.toOmit(var)) {
continue;
}
if (it->second.isExpired()) {
expiredVars.push_back(it->first);
} else if (it->second.hasValue() == false) {
// No-op. A non-expired expiry exists for the key but there is no actual value
} else {
l->insert(l->begin(), new VariableValue(&m_name, &var,
&it->second.getValue()));
}
}
}
}
for (const auto& expiredVar : expiredVars) {
delIfExpired(expiredVar);
}
}
void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
std::vector<const VariableValue *> *l, variables::KeyExclusions &ke) {
Utils::Regex r(var, true);
std::list<std::string> expiredVars;
{
const std::shared_lock lock(m_mutex); // read lock (shared access)
for (const auto& x : m_map) {
const auto ret = Utils::regex_search(x.first, r);
if (ret <= 0) {
continue;
}
if (ke.toOmit(x.first)) {
continue;
}
if (x.second.isExpired()) {
expiredVars.push_back(x.first);
} else if (x.second.hasValue() == false) {
// No-op. A non-expired expiry exists for the key but there is no actual value
} else {
l->insert(l->begin(), new VariableValue(&m_name, &x.first, &x.second.getValue()));
}
}
}
for (const auto& expiredVar : expiredVars) {
delIfExpired(expiredVar);
}
}
std::unique_ptr<std::string> InMemoryPerProcess::resolveFirst(
const std::string& var) {
std::unique_ptr<std::string> ret;
std::list<std::string> expiredVars;
{
const std::shared_lock lock(m_mutex); // read lock (shared access)
const auto range = m_map.equal_range(var);
for (auto it = range.first; it != range.second; ++it) {
if (it->second.isExpired()) {
expiredVars.push_back(it->first);
} else if (it->second.hasValue() == false) {
// No-op. A non-expired expiry exists for the key but there is no actual value
} else {
ret = std::make_unique<std::string>(it->second.getValue());
}
}
}
for (const auto& expiredVar : expiredVars) {
delIfExpired(expiredVar);
}
return ret;
}
} // namespace backend
} // namespace collection
} // namespace modsecurity