Handling key exceptions on the variable itself

This is the first step towords to solve #1697
This commit is contained in:
Felipe Zimmerle
2018-09-20 09:08:08 -03:00
parent 0d53111cb0
commit ee50fea266
54 changed files with 2337 additions and 2080 deletions

View File

@@ -96,25 +96,33 @@ void InMemoryPerProcess::resolveSingleMatch(const std::string& var,
void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
std::vector<const VariableValue *> *l) {
std::vector<const VariableValue *> *l, Variables::KeyExclusions &ke) {
size_t keySize = var.size();
l->reserve(15);
if (keySize == 0) {
for (auto &i : *this) {
l->insert(l->begin(), new VariableValue(&m_name, &i.first, &i.second));
if (ke.toOmit(i.first)) {
continue;
}
l->insert(l->begin(), new VariableValue(&m_name, &i.first,
&i.second));
}
} else {
auto range = this->equal_range(var);
for (auto it = range.first; it != range.second; ++it) {
l->insert(l->begin(), new VariableValue(&m_name, &var, &it->second));
if (ke.toOmit(var)) {
continue;
}
l->insert(l->begin(), new VariableValue(&m_name, &var,
&it->second));
}
}
}
void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
std::vector<const VariableValue *> *l) {
std::vector<const VariableValue *> *l, Variables::KeyExclusions &ke) {
//if (var.find(":") == std::string::npos) {
// return;
@@ -144,7 +152,9 @@ void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
if (ret <= 0) {
continue;
}
if (ke.toOmit(x.first)) {
continue;
}
l->insert(l->begin(), new VariableValue(&m_name, &x.first, &x.second));
}
}

View File

@@ -27,6 +27,7 @@
#include "modsecurity/variable_value.h"
#include "modsecurity/collection/collection.h"
#include "src/variables/variable.h"
#ifndef SRC_COLLECTION_BACKEND_IN_MEMORY_PER_PROCESS_H_
#define SRC_COLLECTION_BACKEND_IN_MEMORY_PER_PROCESS_H_
@@ -71,7 +72,7 @@ class InMemoryPerProcess :
/*std::hash<std::string>*/MyHash, MyEqual>,
public Collection {
public:
InMemoryPerProcess(std::string name);
explicit InMemoryPerProcess(std::string name);
~InMemoryPerProcess();
void store(std::string key, std::string value) override;
@@ -88,9 +89,11 @@ class InMemoryPerProcess :
void resolveSingleMatch(const std::string& var,
std::vector<const VariableValue *> *l) override;
void resolveMultiMatches(const std::string& var,
std::vector<const VariableValue *> *l) override;
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke) override;
void resolveRegularExpression(const std::string& var,
std::vector<const VariableValue *> *l) override;
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke) override;
private:
pthread_mutex_t m_lock;

View File

@@ -24,6 +24,7 @@
#include "modsecurity/variable_value.h"
#include "src/utils/regex.h"
#include "src/variables/variable.h"
#undef LMDB_STDOUT_COUT
@@ -465,7 +466,8 @@ end_txn:
void LMDB::resolveMultiMatches(const std::string& var,
std::vector<const VariableValue *> *l) {
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke) {
MDB_val key, data;
MDB_txn *txn = NULL;
MDB_dbi dbi;
@@ -495,24 +497,22 @@ void LMDB::resolveMultiMatches(const std::string& var,
if (keySize == 0) {
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
l->insert(l->begin(), new VariableValue(
&m_name,
&m_name,
new std::string(reinterpret_cast<char *>(key.mv_data),
key.mv_size),
new std::string(reinterpret_cast<char *>(data.mv_data),
data.mv_size))
);
data.mv_size)));
}
} else {
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
char *a = reinterpret_cast<char *>(key.mv_data);
if (strncmp(var.c_str(), a, keySize) == 0) {
l->insert(l->begin(), new VariableValue(
&m_name,
&m_name,
new std::string(reinterpret_cast<char *>(key.mv_data),
key.mv_size),
new std::string(reinterpret_cast<char *>(data.mv_data),
data.mv_size))
);
data.mv_size)));
}
}
}
@@ -528,7 +528,8 @@ end_txn:
void LMDB::resolveRegularExpression(const std::string& var,
std::vector<const VariableValue *> *l) {
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke) {
MDB_val key, data;
MDB_txn *txn = NULL;
MDB_dbi dbi;
@@ -563,6 +564,11 @@ void LMDB::resolveRegularExpression(const std::string& var,
if (ret <= 0) {
continue;
}
if (ke.toOmit(std::string(reinterpret_cast<char *>(key.mv_data),
key.mv_size))) {
continue;
}
VariableValue *v = new VariableValue(
new std::string(reinterpret_cast<char *>(key.mv_data),
key.mv_size),

View File

@@ -36,6 +36,7 @@
#include "modsecurity/variable_value.h"
#include "modsecurity/collection/collection.h"
#include "src/variables/variable.h"
#ifndef SRC_COLLECTION_BACKEND_LMDB_H_
#define SRC_COLLECTION_BACKEND_LMDB_H_
@@ -50,7 +51,7 @@ namespace backend {
class LMDB :
public Collection {
public:
LMDB(std::string name);
explicit LMDB(std::string name);
~LMDB();
void store(std::string key, std::string value) override;
@@ -67,9 +68,11 @@ class LMDB :
void resolveSingleMatch(const std::string& var,
std::vector<const VariableValue *> *l) override;
void resolveMultiMatches(const std::string& var,
std::vector<const VariableValue *> *l) override;
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke) override;
void resolveRegularExpression(const std::string& var,
std::vector<const VariableValue *> *l) override;
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke) override;
private:
void string2val(const std::string& str, MDB_val *val);