diff --git a/headers/modsecurity/anchored_set_variable.h b/headers/modsecurity/anchored_set_variable.h index 582ddc82..f52bdee9 100644 --- a/headers/modsecurity/anchored_set_variable.h +++ b/headers/modsecurity/anchored_set_variable.h @@ -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 *l); - void resolveRegularExpression(Utils::Regex *r, + void resolveRegularExpression(regex::Regex *r, std::vector *l); - void resolveRegularExpression(Utils::Regex *r, + void resolveRegularExpression(regex::Regex *r, std::vector *l, Variables::KeyExclusions &ke); diff --git a/src/Makefile.am b/src/Makefile.am index b6d23b01..305678f0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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} diff --git a/src/anchored_set_variable.cc b/src/anchored_set_variable.cc index faf3c744..00697747 100644 --- a/src/anchored_set_variable.cc +++ b/src/anchored_set_variable.cc @@ -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 AnchoredSetVariable::resolveFirst( } -void AnchoredSetVariable::resolveRegularExpression(Utils::Regex *r, +void AnchoredSetVariable::resolveRegularExpression(regex::Regex *r, std::vector *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 *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; } diff --git a/src/anchored_variable.cc b/src/anchored_variable.cc index 9347752c..6925ac03 100644 --- a/src/anchored_variable.cc +++ b/src/anchored_variable.cc @@ -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 { diff --git a/src/audit_log/audit_log.cc b/src/audit_log/audit_log.cc index fb7fe625..88ba0a66 100644 --- a/src/audit_log/audit_log.cc +++ b/src/audit_log/audit_log.cc @@ -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; } diff --git a/src/collection/backend/in_memory-per_process.cc b/src/collection/backend/in_memory-per_process.cc index 9f13fa58..27d8f3db 100644 --- a/src/collection/backend/in_memory-per_process.cc +++ b/src/collection/backend/in_memory-per_process.cc @@ -27,7 +27,7 @@ #include #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; } diff --git a/src/collection/backend/lmdb.cc b/src/collection/backend/lmdb.cc index 2b8724c7..b716e041 100644 --- a/src/collection/backend/lmdb.cc +++ b/src/collection/backend/lmdb.cc @@ -23,7 +23,7 @@ #include #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(key.mv_data); - int ret = Utils::regex_search(a, r); + int ret = regex::regex_search(a, r); if (ret <= 0) { continue; } diff --git a/src/modsecurity.cc b/src/modsecurity.cc index ab9bba2e..8b316b8a 100644 --- a/src/modsecurity.cc +++ b/src/modsecurity.cc @@ -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 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 vars = variables.searchAll(matchString); - std::list ops = operators.searchAll(matchString); - std::list trans = transformations.searchAll(matchString); + std::list vars = variables.searchAll(matchString); + std::list ops = operators.searchAll(matchString); + std::list trans = transformations.searchAll(matchString); g = yajl_gen_alloc(NULL); if (g == NULL) { diff --git a/src/operators/rx.h b/src/operators/rx.h index 7460c396..5da9bfe2 100644 --- a/src/operators/rx.h +++ b/src/operators/rx.h @@ -22,13 +22,13 @@ #include #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 { diff --git a/src/operators/verify_cpf.h b/src/operators/verify_cpf.h index 893c47fb..318625fd 100644 --- a/src/operators/verify_cpf.h +++ b/src/operators/verify_cpf.h @@ -21,13 +21,13 @@ #include #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 { diff --git a/src/operators/verify_ssn.h b/src/operators/verify_ssn.h index c8cf7f16..1386805b 100644 --- a/src/operators/verify_ssn.h +++ b/src/operators/verify_ssn.h @@ -21,13 +21,13 @@ #include #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 { diff --git a/src/regex/backend/pcre.cc b/src/regex/backend/pcre.cc new file mode 100644 index 00000000..78dd8ecd --- /dev/null +++ b/src/regex/backend/pcre.cc @@ -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 + +#include +#include +#include +#include + +#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_ diff --git a/src/regex/backend/pcre.h b/src/regex/backend/pcre.h new file mode 100644 index 00000000..78dd8ecd --- /dev/null +++ b/src/regex/backend/pcre.h @@ -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 + +#include +#include +#include +#include + +#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_ diff --git a/src/utils/regex.cc b/src/regex/regex.cc similarity index 97% rename from src/utils/regex.cc rename to src/regex/regex.cc index 461f0288..36de0c6d 100644 --- a/src/utils/regex.cc +++ b/src/regex/regex.cc @@ -13,7 +13,7 @@ * */ -#include "src/utils/regex.h" +#include "src/regex/regex.h" #include #include @@ -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 diff --git a/src/utils/regex.h b/src/regex/regex.h similarity index 93% rename from src/utils/regex.h rename to src/regex/regex.h index 147b48f4..1accf473 100644 --- a/src/utils/regex.h +++ b/src/regex/regex.h @@ -20,12 +20,13 @@ #include #include -#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_ diff --git a/src/variables/rule.h b/src/variables/rule.h index 4947a190..ba57a88f 100644 --- a/src/variables/rule.h +++ b/src/variables/rule.h @@ -198,23 +198,23 @@ class Rule_DictElementRegexp : public VariableRegex { void evaluate(Transaction *t, Rule *rule, std::vector *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; } diff --git a/src/variables/variable.h b/src/variables/variable.h index 7a63391f..70e9d75b 100644 --- a/src/variables/variable.h +++ b/src/variables/variable.h @@ -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 { diff --git a/test/regression/custom_debug_log.cc b/test/regression/custom_debug_log.cc index bffcfa7d..a86911c9 100644 --- a/test/regression/custom_debug_log.cc +++ b/test/regression/custom_debug_log.cc @@ -19,7 +19,7 @@ #include #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() { diff --git a/test/regression/regression.cc b/test/regression/regression.cc index 46da615e..6f6498ee 100644 --- a/test/regression/regression.cc +++ b/test/regression/regression.cc @@ -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 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; } diff --git a/test/unit/unit_test.cc b/test/unit/unit_test.cc index 23bbcb23..b0beb4e4 100644 --- a/test/unit/unit_test.cc +++ b/test/unit/unit_test.cc @@ -23,7 +23,7 @@ #include #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);