Moving regex from utils to its own namespace

This commit is contained in:
Felipe Zimmerle 2019-01-18 17:52:42 -03:00
parent 3dda0ea2c6
commit d3f9974d52
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
20 changed files with 146 additions and 70 deletions

View File

@ -38,7 +38,7 @@
namespace modsecurity {
class Transaction;
namespace Utils {
namespace regex {
class Regex;
}
namespace Variables {
@ -91,10 +91,10 @@ class AnchoredSetVariable : public std::unordered_multimap<std::string,
void resolve(const std::string &key,
std::vector<const VariableValue *> *l);
void resolveRegularExpression(Utils::Regex *r,
void resolveRegularExpression(regex::Regex *r,
std::vector<const VariableValue *> *l);
void resolveRegularExpression(Utils::Regex *r,
void resolveRegularExpression(regex::Regex *r,
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke);

View File

@ -239,13 +239,17 @@ UTILS = \
utils/md5.cc \
utils/msc_tree.cc \
utils/random.cc \
utils/regex.cc \
utils/sha1.cc \
utils/string.cc \
utils/system.cc \
utils/shared_files.cc
REGEX = \
regex/regex.cc \
regex/backend/pcre.cc
COLLECTION = \
collection/collections.cc \
collection/backend/in_memory-per_process.cc \
@ -287,6 +291,7 @@ libmodsecurity_la_SOURCES = \
${COLLECTION} \
${OPERATORS} \
${UTILS} \
${REGEX} \
${VARIABLES}

View File

@ -22,7 +22,7 @@
#include "modsecurity/anchored_set_variable.h"
#include "modsecurity/modsecurity.h"
#include "modsecurity/transaction.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#include "src/variables/variable.h"
namespace modsecurity {
@ -124,10 +124,10 @@ std::unique_ptr<std::string> AnchoredSetVariable::resolveFirst(
}
void AnchoredSetVariable::resolveRegularExpression(Utils::Regex *r,
void AnchoredSetVariable::resolveRegularExpression(regex::Regex *r,
std::vector<const VariableValue *> *l) {
for (const auto& x : *this) {
int ret = Utils::regex_search(x.first, *r);
int ret = regex::regex_search(x.first, *r);
if (ret <= 0) {
continue;
}
@ -136,11 +136,11 @@ void AnchoredSetVariable::resolveRegularExpression(Utils::Regex *r,
}
void AnchoredSetVariable::resolveRegularExpression(Utils::Regex *r,
void AnchoredSetVariable::resolveRegularExpression(regex::Regex *r,
std::vector<const VariableValue *> *l,
Variables::KeyExclusions &ke) {
for (const auto& x : *this) {
int ret = Utils::regex_search(x.first, *r);
int ret = regex::regex_search(x.first, *r);
if (ret <= 0) {
continue;
}

View File

@ -22,7 +22,7 @@
#include "../headers/modsecurity/anchored_variable.h"
#include "modsecurity/modsecurity.h"
#include "modsecurity/transaction.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
namespace modsecurity {

View File

@ -26,7 +26,7 @@
#include "src/audit_log/writer/parallel.h"
#include "src/audit_log/writer/serial.h"
#include "src/audit_log/writer/writer.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#define PARTS_CONSTAINS(a, c) \
if (new_parts.find(toupper(a)) != std::string::npos \
@ -279,8 +279,8 @@ bool AuditLog::isRelevant(int status) {
return true;
}
return Utils::regex_search(sstatus,
Utils::Regex(m_relevant)) != 0;
return regex::regex_search(sstatus,
regex::Regex(m_relevant)) != 0;
}

View File

@ -27,7 +27,7 @@
#include <pthread.h>
#include "modsecurity/variable_value.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#include "src/utils/string.h"
@ -134,7 +134,7 @@ void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
//std::string name = std::string(var, var.find(":") + 2,
// var.size() - var.find(":") - 3);
//size_t keySize = col.size();
Utils::Regex r(var);
regex::Regex r(var);
for (const auto& x : *this) {
//if (x.first.size() <= keySize + 1) {
@ -148,7 +148,7 @@ void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
//}
//std::string content = std::string(x.first, keySize + 1,
// x.first.size() - keySize - 1);
int ret = Utils::regex_search(x.first, r);
int ret = regex::regex_search(x.first, r);
if (ret <= 0) {
continue;
}

View File

@ -23,7 +23,7 @@
#include <memory>
#include "modsecurity/variable_value.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#include "src/variables/variable.h"
#undef LMDB_STDOUT_COUT
@ -538,7 +538,7 @@ void LMDB::resolveRegularExpression(const std::string& var,
MDB_cursor *cursor;
size_t pos;
Utils::Regex r(var);
regex::Regex r(var);
rc = mdb_txn_begin(m_env, NULL, 0, &txn);
lmdb_debug(rc, "txn", "resolveRegularExpression");
@ -560,7 +560,7 @@ void LMDB::resolveRegularExpression(const std::string& var,
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
char *a = reinterpret_cast<char *>(key.mv_data);
int ret = Utils::regex_search(a, r);
int ret = regex::regex_search(a, r);
if (ret <= 0) {
continue;
}

View File

@ -38,7 +38,7 @@
#include "src/collection/backend/in_memory-per_process.h"
#include "src/collection/backend/lmdb.h"
#include "src/unique_id.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#include "src/utils/geo_lookup.h"
#include "src/actions/transformations/transformation.h"
@ -219,18 +219,18 @@ void ModSecurity::serverLog(void *data, std::shared_ptr<RuleMessage> rm) {
int ModSecurity::processContentOffset(const char *content, size_t len,
const char *matchString, std::string *json, const char **err) {
#ifdef WITH_YAJL
Utils::Regex variables("v([0-9]+),([0-9]+)");
Utils::Regex operators("o([0-9]+),([0-9]+)");
Utils::Regex transformations("t:(?:(?!t:).)+");
regex::Regex variables("v([0-9]+),([0-9]+)");
regex::Regex operators("o([0-9]+),([0-9]+)");
regex::Regex transformations("t:(?:(?!t:).)+");
yajl_gen g;
std::string varValue;
std::string opValue;
const unsigned char *buf;
size_t jsonSize;
std::list<Utils::SMatch> vars = variables.searchAll(matchString);
std::list<Utils::SMatch> ops = operators.searchAll(matchString);
std::list<Utils::SMatch> trans = transformations.searchAll(matchString);
std::list<regex::SMatch> vars = variables.searchAll(matchString);
std::list<regex::SMatch> ops = operators.searchAll(matchString);
std::list<regex::SMatch> trans = transformations.searchAll(matchString);
g = yajl_gen_alloc(NULL);
if (g == NULL) {

View File

@ -22,13 +22,13 @@
#include <utility>
#include "src/operators/operator.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
namespace modsecurity {
using Utils::SMatch;
using Utils::regex_search;
using Utils::Regex;
using regex::SMatch;
using regex::regex_search;
using regex::Regex;
namespace operators {

View File

@ -21,13 +21,13 @@
#include <utility>
#include "src/operators/operator.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
namespace modsecurity {
using Utils::SMatch;
using Utils::regex_search;
using Utils::Regex;
using regex::SMatch;
using regex::regex_search;
using regex::Regex;
namespace operators {

View File

@ -21,13 +21,13 @@
#include <utility>
#include "src/operators/operator.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
namespace modsecurity {
using Utils::SMatch;
using Utils::regex_search;
using Utils::Regex;
using regex::SMatch;
using regex::regex_search;
using regex::Regex;
namespace operators {

35
src/regex/backend/pcre.cc Normal file
View File

@ -0,0 +1,35 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 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 security@modsecurity.org.
*
*/
#include <pcre.h>
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#ifndef SRC_REGEX_BACKEND_PCRE_H_
#define SRC_REGEX_BACKEND_PCRE_H_
namespace modsecurity {
namespace regex {
} // namespace regex
} // namespace modsecurity
#endif // SRC_REGEX_BACKEND_PCRE_H_

35
src/regex/backend/pcre.h Normal file
View File

@ -0,0 +1,35 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 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 security@modsecurity.org.
*
*/
#include <pcre.h>
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#ifndef SRC_REGEX_BACKEND_PCRE_H_
#define SRC_REGEX_BACKEND_PCRE_H_
namespace modsecurity {
namespace regex {
} // namespace regex
} // namespace modsecurity
#endif // SRC_REGEX_BACKEND_PCRE_H_

View File

@ -13,7 +13,7 @@
*
*/
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#include <pcre.h>
#include <sys/socket.h>
@ -35,7 +35,7 @@
#endif
namespace modsecurity {
namespace Utils {
namespace regex {
Regex::Regex(const std::string& pattern_)
@ -119,5 +119,5 @@ int Regex::search(const std::string& s) const {
s.size(), 0, 0, ovector, OVECCOUNT) > 0;
}
} // namespace Utils
} // namespace regex
} // namespace modsecurity

View File

@ -20,12 +20,13 @@
#include <string>
#include <list>
#ifndef SRC_UTILS_REGEX_H_
#define SRC_UTILS_REGEX_H_
#ifndef SRC_REGEX_REGEX_H_
#define SRC_REGEX_REGEX_H_
namespace modsecurity {
namespace Utils {
namespace regex {
#define OVECCOUNT 30
@ -78,7 +79,7 @@ static inline int regex_search(const std::string& s, const Regex& regex) {
}
} // namespace Utils
} // namespace regex
} // namespace modsecurity
#endif // SRC_UTILS_REGEX_H_
#endif // SRC_REGEX_REGEX_H_

View File

@ -198,23 +198,23 @@ class Rule_DictElementRegexp : public VariableRegex {
void evaluate(Transaction *t,
Rule *rule,
std::vector<const VariableValue *> *l) override {
if (Utils::regex_search("id", m_r) > 0) {
if (regex::regex_search("id", m_r) > 0) {
Rule_DictElement::id(t, rule, l);
return;
}
if (Utils::regex_search("rev", m_r) > 0) {
if (regex::regex_search("rev", m_r) > 0) {
Rule_DictElement::rev(t, rule, l);
return;
}
if (Utils::regex_search("severity", m_r) > 0) {
if (regex::regex_search("severity", m_r) > 0) {
Rule_DictElement::severity(t, rule, l);
return;
}
if (Utils::regex_search("logdata", m_r) > 0) {
if (regex::regex_search("logdata", m_r) > 0) {
Rule_DictElement::logData(t, rule, l);
return;
}
if (Utils::regex_search("msg", m_r) > 0) {
if (regex::regex_search("msg", m_r) > 0) {
Rule_DictElement::msg(t, rule, l);
return;
}

View File

@ -25,7 +25,7 @@
#include "modsecurity/rule.h"
#include "modsecurity/rules.h"
#include "src/utils/string.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#ifndef SRC_VARIABLES_VARIABLE_H_
#define SRC_VARIABLES_VARIABLE_H_
@ -114,7 +114,7 @@ class KeyExclusion {
// FIXME: use pre built regex.
class KeyExclusionRegex : public KeyExclusion {
public:
explicit KeyExclusionRegex(Utils::Regex re)
explicit KeyExclusionRegex(regex::Regex re)
: m_re(re.pattern) { }
explicit KeyExclusionRegex(std::string re)
: m_re(re) { }
@ -125,7 +125,7 @@ class KeyExclusionRegex : public KeyExclusion {
return m_re.searchAll(a).size() > 0;
}
Utils::Regex m_re;
regex::Regex m_re;
};
@ -596,7 +596,7 @@ class VariableRegex : public Variable {
// FIXME: no need for that.
std::string m_regex;
Utils::Regex m_r;
regex::Regex m_r;
};
class Variables : public std::vector<Variable *> {

View File

@ -19,7 +19,7 @@
#include <string>
#include "modsecurity/debug_log.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
namespace modsecurity_test {
@ -37,9 +37,9 @@ void CustomDebugLog::write(int level, const std::string &id,
}
bool CustomDebugLog::contains(const std::string& pattern) {
modsecurity::Utils::Regex re(pattern);
modsecurity::regex::Regex re(pattern);
std::string s = m_log.str();
return modsecurity::Utils::regex_search(s, re);
return modsecurity::regex::regex_search(s, re);
}
std::string CustomDebugLog::log_messages() {

View File

@ -31,7 +31,7 @@
#include "test/regression/regression_test.h"
#include "test/common/modsecurity_test_results.h"
#include "test/regression/custom_debug_log.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
using modsecurity_test::CustomDebugLog;
using modsecurity_test::ModSecurityTest;
@ -39,9 +39,9 @@ using modsecurity_test::ModSecurityTestResults;
using modsecurity_test::RegressionTest;
using modsecurity_test::RegressionTestResult;
using modsecurity::Utils::regex_search;
using modsecurity::Utils::SMatch;
using modsecurity::Utils::Regex;
using modsecurity::regex::regex_search;
using modsecurity::regex::SMatch;
using modsecurity::regex::Regex;
std::string default_test_path = "test-cases/regression";
std::list<std::string> resources;
@ -54,8 +54,8 @@ void print_help() {
bool contains(const std::string &s, const std::string &pattern) {
bool ret;
modsecurity::Utils::Regex re(pattern);
ret = modsecurity::Utils::regex_search(s, re);
modsecurity::regex::Regex re(pattern);
ret = modsecurity::regex::regex_search(s, re);
return ret;
}

View File

@ -23,7 +23,7 @@
#include <iterator>
#include "test/common/colors.h"
#include "src/utils/regex.h"
#include "src/regex/regex.h"
#include "src/utils/string.h"
@ -58,11 +58,11 @@ void replaceAll(std::string *s, const std::string &search,
void json2bin(std::string *str) {
modsecurity::Utils::Regex re("\\\\x([a-z0-9A-Z]{2})");
modsecurity::Utils::Regex re2("\\\\u([a-z0-9A-Z]{4})");
modsecurity::Utils::SMatch match;
modsecurity::regex::Regex re("\\\\x([a-z0-9A-Z]{2})");
modsecurity::regex::Regex re2("\\\\u([a-z0-9A-Z]{4})");
modsecurity::regex::SMatch match;
while (modsecurity::Utils::regex_search(*str, &match, re)) {
while (modsecurity::regex::regex_search(*str, &match, re)) {
unsigned int p;
std::string toBeReplaced = match.str();
toBeReplaced.erase(0, 2);
@ -70,7 +70,7 @@ void json2bin(std::string *str) {
replaceAll(str, match.str(), p);
}
while (modsecurity::Utils::regex_search(*str, &match, re2)) {
while (modsecurity::regex::regex_search(*str, &match, re2)) {
unsigned int p;
std::string toBeReplaced = match.str();
toBeReplaced.erase(0, 2);