Skip to content

Commit de7c5c8

Browse files
author
Felipe Zimmerle
committed
Using shared var for variables names
1 parent 6f7fdd9 commit de7c5c8

File tree

21 files changed

+183
-187
lines changed

21 files changed

+183
-187
lines changed

headers/modsecurity/collection/collection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace collection {
4040

4141
class Collection {
4242
public:
43+
Collection(std::string a) : m_name(a) { }
4344
virtual ~Collection() { }
4445
virtual void store(std::string key, std::string value) = 0;
4546

headers/modsecurity/collection/variable.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,60 @@ typedef struct Variable_t Variable;
3535
namespace modsecurity {
3636
namespace collection {
3737

38+
class Collection;
3839
class Variable {
3940
public:
4041
explicit Variable(const std::string *key) :
4142
m_key(""),
4243
m_value("") {
4344
m_key.assign(*key);
45+
m_keyWithCollection = std::make_shared<std::string>(*key);
4446
}
47+
4548
Variable(const std::string *key, const std::string *value) :
4649
m_key(""),
4750
m_value("") {
4851
m_key.assign(*key);
4952
m_value.assign(*value);
53+
m_keyWithCollection = std::make_shared<std::string>(*key);
5054
}
55+
5156
Variable() :
5257
m_key(""),
53-
m_value("") { }
58+
m_value("") {
59+
m_keyWithCollection = std::make_shared<std::string>(m_key);
60+
}
61+
62+
Variable(const std::string *a, const std::string *b, const std::string *c) :
63+
m_key(*a + ":" + *b),
64+
m_value(*c) {
65+
m_keyWithCollection = std::make_shared<std::string>(*a + ":" + *b);
66+
}
67+
68+
Variable(std::shared_ptr<std::string> fullName) :
69+
m_key(""),
70+
m_value("") {
71+
m_keyWithCollection = fullName;
72+
m_key.assign(*fullName.get());
73+
}
74+
75+
Variable(std::shared_ptr<std::string> fullName, const std::string *value) :
76+
m_key(""),
77+
m_value("") {
78+
m_value.assign(*value);
79+
m_keyWithCollection = fullName;
80+
m_key.assign(*fullName.get());
81+
}
82+
5483

5584
explicit Variable(const Variable *o) :
5685
m_key(""),
5786
m_value("") {
5887
m_key.assign(o->m_key);
5988
m_value.assign(o->m_value);
89+
m_col.assign(o->m_col);
90+
m_keyWithCollection = o->m_keyWithCollection;
91+
6092
for (auto &i : o->m_orign) {
6193
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
6294
origin->m_offset = i->m_offset;
@@ -67,6 +99,8 @@ class Variable {
6799

68100
std::string m_key;
69101
std::string m_value;
102+
std::string m_col;
103+
std::shared_ptr<std::string> m_keyWithCollection;
70104
std::list<std::unique_ptr<VariableOrigin>> m_orign;
71105
};
72106

src/anchored_set_variable.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ void AnchoredSetVariable::set(const std::string &key,
5353
const std::string &value, size_t offset, size_t len) {
5454
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
5555
std::string *v = new std::string(value);
56-
std::string *k = new std::string(m_name + ":" + key);
57-
collection::Variable *var = new collection::Variable(k, v);
56+
collection::Variable *var = new collection::Variable(std::make_shared<std::string>(m_name + ":" + key), v);
5857
delete v;
59-
delete k;
6058

6159
origin->m_offset = offset;
6260
origin->m_length = len;
@@ -70,10 +68,8 @@ void AnchoredSetVariable::set(const std::string &key,
7068
const std::string &value, size_t offset) {
7169
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
7270
std::string *v = new std::string(value);
73-
std::string *k = new std::string(m_name + ":" + key);
74-
collection::Variable *var = new collection::Variable(k, v);
71+
collection::Variable *var = new collection::Variable(std::make_shared<std::string>(m_name + ":" + key), v);
7572
delete v;
76-
delete k;
7773

7874
origin->m_offset = offset;
7975
origin->m_length = value.size();

src/collection/backend/in_memory-per_process.cc

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace collection {
3636
namespace backend {
3737

3838

39-
InMemoryPerProcess::InMemoryPerProcess() {
39+
InMemoryPerProcess::InMemoryPerProcess(std::string name) :
40+
Collection(name) {
4041
this->reserve(1000);
4142
pthread_mutex_init(&m_lock, NULL);
4243
}
@@ -89,7 +90,7 @@ void InMemoryPerProcess::resolveSingleMatch(const std::string& var,
8990
auto range = this->equal_range(var);
9091

9192
for (auto it = range.first; it != range.second; ++it) {
92-
l->push_back(new Variable(&it->first, &it->second));
93+
l->push_back(new Variable(&m_name, &it->first, &it->second));
9394
}
9495
}
9596

@@ -99,78 +100,60 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
99100
size_t keySize = var.size();
100101
l->reserve(15);
101102

102-
auto range = this->equal_range(var);
103-
104-
for (auto it = range.first; it != range.second; ++it) {
105-
l->insert(l->begin(), new Variable(&var, &it->second));
106-
}
107-
108-
for (const auto& x : *this) {
109-
bool diff = false;
110-
111-
if (x.first.size() <= keySize + 1) {
112-
continue;
113-
}
114-
if (x.first.at(keySize) != ':') {
115-
continue;
116-
}
117-
118-
for (int i = 0; i < keySize && diff == false; i++) {
119-
if (std::tolower(x.first.at(i)) != std::tolower(var.at(i))) {
120-
diff = true;
121-
}
103+
if (keySize == 0) {
104+
for (auto &i : *this) {
105+
l->insert(l->begin(), new Variable(&m_name, &i.first, &i.second));
122106
}
123-
124-
if (diff == true) {
125-
continue;
107+
} else {
108+
auto range = this->equal_range(var);
109+
for (auto it = range.first; it != range.second; ++it) {
110+
l->insert(l->begin(), new Variable(&m_name, &var, &it->second));
126111
}
127-
128-
l->insert(l->begin(), new Variable(&x.first, &x.second));
129112
}
130113
}
131114

132115

133116
void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
134117
std::vector<const Variable *> *l) {
135118

136-
if (var.find(":") == std::string::npos) {
137-
return;
138-
}
139-
if (var.size() < var.find(":") + 3) {
140-
return;
141-
}
142-
std::string col = std::string(var, 0, var.find(":"));
143-
std::string name = std::string(var, var.find(":") + 2,
144-
var.size() - var.find(":") - 3);
145-
size_t keySize = col.size();
146-
Utils::Regex r = Utils::Regex(name);
119+
120+
//if (var.find(":") == std::string::npos) {
121+
// return;
122+
//}
123+
//if (var.size() < var.find(":") + 3) {
124+
// return;
125+
//}
126+
//std::string col = std::string(var, 0, var.find(":"));
127+
//std::string name = std::string(var, var.find(":") + 2,
128+
// var.size() - var.find(":") - 3);
129+
//size_t keySize = col.size();
130+
Utils::Regex r = Utils::Regex(var);
147131

148132
for (const auto& x : *this) {
149-
if (x.first.size() <= keySize + 1) {
150-
continue;
151-
}
152-
if (x.first.at(keySize) != ':') {
153-
continue;
154-
}
155-
if (std::string(x.first, 0, keySize) != col) {
156-
continue;
157-
}
158-
std::string content = std::string(x.first, keySize + 1,
159-
x.first.size() - keySize - 1);
160-
int ret = Utils::regex_search(content, r);
133+
//if (x.first.size() <= keySize + 1) {
134+
// continue;
135+
//}
136+
//if (x.first.at(keySize) != ':') {
137+
// continue;
138+
//}
139+
//if (std::string(x.first, 0, keySize) != col) {
140+
// continue;
141+
//}
142+
//std::string content = std::string(x.first, keySize + 1,
143+
// x.first.size() - keySize - 1);
144+
int ret = Utils::regex_search(x.first, r);
161145
if (ret <= 0) {
162146
continue;
163147
}
164148

165-
l->insert(l->begin(), new Variable(&x.first, &x.second));
149+
l->insert(l->begin(), new Variable(&m_name, &x.first, &x.second));
166150
}
167151
}
168152

169153

170154
std::unique_ptr<std::string> InMemoryPerProcess::resolveFirst(
171155
const std::string& var) {
172156
auto range = equal_range(var);
173-
174157
for (auto it = range.first; it != range.second; ++it) {
175158
return std::unique_ptr<std::string>(new std::string(it->second));
176159
}

src/collection/backend/in_memory-per_process.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class InMemoryPerProcess :
7171
/*std::hash<std::string>*/MyHash, MyEqual>,
7272
public Collection {
7373
public:
74-
InMemoryPerProcess();
74+
InMemoryPerProcess(std::string name);
7575
~InMemoryPerProcess();
7676
void store(std::string key, std::string value) override;
7777

src/collection/collections.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Collections::Collections(Collection *global,
4444
m_ip_collection(ip),
4545
m_session_collection(session),
4646
m_user_collection(user),
47-
m_tx_collection(new backend::InMemoryPerProcess()) { }
47+
m_tx_collection(new backend::InMemoryPerProcess("TX")) {
48+
}
4849

4950

5051
Collections::~Collections() { }

src/modsecurity.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ ModSecurity::ModSecurity()
6666
m_session_collection(new collection::backend::LMDB()),
6767
m_user_collection(new collection::backend::LMDB()),
6868
#else
69-
m_global_collection(new collection::backend::InMemoryPerProcess()),
70-
m_resource_collection(new collection::backend::InMemoryPerProcess()),
71-
m_ip_collection(new collection::backend::InMemoryPerProcess()),
72-
m_session_collection(new collection::backend::InMemoryPerProcess()),
73-
m_user_collection(new collection::backend::InMemoryPerProcess()),
69+
m_global_collection(new collection::backend::InMemoryPerProcess("GLOBAL")),
70+
m_ip_collection(new collection::backend::InMemoryPerProcess("IP")),
71+
m_resource_collection(new collection::backend::InMemoryPerProcess("RESOURCE")),
72+
m_session_collection(new collection::backend::InMemoryPerProcess("SESSION")),
73+
m_user_collection(new collection::backend::InMemoryPerProcess("USER")),
7474
#endif
7575
m_logCb(NULL) {
7676
UniqueId::uniqueId();

src/operators/pm.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool Pm::evaluate(Transaction *transaction, Rule *rule,
105105
}
106106

107107
if (capture && transaction && rc) {
108-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("TX:0",
108+
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("0",
109109
std::string(match));
110110
#ifndef NO_LOGS
111111
transaction->debug(7, "Added pm match TX.0: " + \

src/operators/rx.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool Rx::evaluate(Transaction *transaction, Rule *rule,
5959
matches.reverse();
6060
for (const SMatch& a : matches) {
6161
transaction->m_collections.m_tx_collection->storeOrUpdateFirst(
62-
"TX:" + std::to_string(i), a.match);
62+
std::to_string(i), a.match);
6363
#ifndef NO_LOGS
6464
transaction->debug(7, "Added regex subexpression TX." +
6565
std::to_string(i) + ": " + a.match);

src/variables/env.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ void Env::evaluate(Transaction *transaction,
4343
}
4444
std::string key = std::string(env, 0, pos);
4545
std::string value = std::string(env, pos+1, env.length() - (pos + 1));
46-
std::pair<std::string, std::string> a("ENV:" + key, value);
46+
std::pair<std::string, std::string> a(key, value);
4747
transaction->m_variableEnvs.insert(a);
4848
}
4949

5050
for (auto& x : transaction->m_variableEnvs) {
51-
if ((x.first.substr(0, m_name.size() + 1).compare(m_name + ":") != 0)
52-
&& (x.first != m_name)) {
51+
if (x.first != m_name && m_name.length() > 0) {
5352
continue;
5453
}
55-
l->push_back(new collection::Variable(&x.first, &x.second));
54+
l->push_back(new collection::Variable(&m_collectionName, &x.first, &x.second));
5655
}
5756
}
5857

0 commit comments

Comments
 (0)