From 2244e874e28e06fc73ec494b2a9273883244c3ca Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Fri, 4 Nov 2016 15:58:05 -0300 Subject: [PATCH] Moves static methods from class String to the namespace string --- src/actions/allow.cc | 5 +- src/actions/ctl/rule_remove_target_by_id.cc | 4 +- src/actions/ctl/rule_remove_target_by_tag.cc | 3 +- src/actions/phase.cc | 5 +- src/actions/set_var.cc | 4 +- src/actions/severity.cc | 5 +- src/actions/transformations/css_decode.cc | 10 +-- .../transformations/escape_seq_decode.cc | 2 +- src/actions/transformations/hex_decode.cc | 2 +- src/actions/transformations/js_decode.cc | 4 +- src/actions/transformations/sql_hex_decode.cc | 2 +- src/actions/transformations/url_decode_uni.cc | 4 +- src/actions/transformations/url_encode.cc | 2 +- .../transformations/utf8_to_unicode.cc | 2 +- .../backend/in_memory-per_process.cc | 7 +- src/collection/collections.cc | 90 +++++++++---------- src/macro_expansion.cc | 4 +- src/parser/seclang-parser.yy | 3 +- src/parser/seclang-scanner.ll | 3 +- src/request_body_processor/multipart.cc | 9 +- src/rule.cc | 10 +-- src/rule_message.cc | 2 +- src/rules_exceptions.cc | 7 +- src/transaction.cc | 36 ++++---- src/utils/decode.cc | 6 +- src/utils/string.cc | 24 ++--- src/utils/string.h | 33 ++++--- src/variables/rule.cc | 7 +- src/variables/variable.cc | 25 +++--- test/unit/unit.cc | 4 +- test/unit/unit_test.cc | 9 +- 31 files changed, 151 insertions(+), 182 deletions(-) diff --git a/src/actions/allow.cc b/src/actions/allow.cc index e76dc590..c7b81286 100644 --- a/src/actions/allow.cc +++ b/src/actions/allow.cc @@ -24,14 +24,11 @@ #include "modsecurity/modsecurity.h" -using modsecurity::utils::String; - - namespace modsecurity { namespace actions { bool Allow::init(std::string *error) { - std::string a = String::tolower(m_parser_payload); + std::string a = utils::string::tolower(m_parser_payload); if (a == "phase") { m_allowType = PhaseAllowType; diff --git a/src/actions/ctl/rule_remove_target_by_id.cc b/src/actions/ctl/rule_remove_target_by_id.cc index 300551b8..697d77fd 100644 --- a/src/actions/ctl/rule_remove_target_by_id.cc +++ b/src/actions/ctl/rule_remove_target_by_id.cc @@ -22,8 +22,6 @@ #include "modsecurity/transaction.h" #include "src/utils/string.h" -using modsecurity::utils::String; - namespace modsecurity { namespace actions { @@ -32,7 +30,7 @@ namespace ctl { bool RuleRemoveTargetById::init(std::string *error) { std::string what(m_parser_payload, 21, m_parser_payload.size() - 21); - std::vector param = String::split(what, ';'); + std::vector param = utils::string::split(what, ';'); if (param.size() < 2) { error->assign(what + " is not a valid `ID;VARIABLE'"); diff --git a/src/actions/ctl/rule_remove_target_by_tag.cc b/src/actions/ctl/rule_remove_target_by_tag.cc index c8f02971..0e0b6280 100644 --- a/src/actions/ctl/rule_remove_target_by_tag.cc +++ b/src/actions/ctl/rule_remove_target_by_tag.cc @@ -22,7 +22,6 @@ #include "modsecurity/transaction.h" #include "src/utils/string.h" -using modsecurity::utils::String; namespace modsecurity { namespace actions { @@ -31,7 +30,7 @@ namespace ctl { bool RuleRemoveTargetByTag::init(std::string *error) { std::string what(m_parser_payload, 22, m_parser_payload.size() - 22); - std::vector param = String::split(what, ';'); + std::vector param = utils::string::split(what, ';'); if (param.size() < 2) { error->assign(what + " is not a valid `TAG;VARIABLE'"); diff --git a/src/actions/phase.cc b/src/actions/phase.cc index 865914cf..4f71acae 100644 --- a/src/actions/phase.cc +++ b/src/actions/phase.cc @@ -24,14 +24,11 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - - namespace modsecurity { namespace actions { bool Phase::init(std::string *error) { - std::string a = String::tolower(m_parser_payload); + std::string a = utils::string::tolower(m_parser_payload); m_phase = -1; try { diff --git a/src/actions/set_var.cc b/src/actions/set_var.cc index 6fd19c62..c3027077 100644 --- a/src/actions/set_var.cc +++ b/src/actions/set_var.cc @@ -23,8 +23,6 @@ #include "src/macro_expansion.h" #include "src/utils/string.h" -using modsecurity::utils::String; - namespace modsecurity { namespace actions { @@ -52,7 +50,7 @@ bool SetVar::init(std::string *error) { pos = m_parser_payload.find("."); if (pos != std::string::npos) { m_collectionName = std::string(m_parser_payload, 0, pos); - m_collectionName = String::toupper(m_collectionName); + m_collectionName = utils::string::toupper(m_collectionName); } else { error->assign("Missing the collection and/or variable name"); return false; diff --git a/src/actions/severity.cc b/src/actions/severity.cc index 82e4e434..b8d62df3 100644 --- a/src/actions/severity.cc +++ b/src/actions/severity.cc @@ -25,15 +25,12 @@ #include "modsecurity/rule_message.h" -using modsecurity::utils::String; - - namespace modsecurity { namespace actions { bool Severity::init(std::string *error) { - std::string a = String::tolower(m_parser_payload); + std::string a = utils::string::tolower(m_parser_payload); if (a == "emergency") { m_severity = 0; return true; diff --git a/src/actions/transformations/css_decode.cc b/src/actions/transformations/css_decode.cc index a69200b9..ec41e4fb 100644 --- a/src/actions/transformations/css_decode.cc +++ b/src/actions/transformations/css_decode.cc @@ -90,20 +90,20 @@ int CssDecode::css_decode_inplace(unsigned char *input, int64_t input_len) { switch (j) { /* Number of hex characters */ case 1: - *d++ = modsecurity::utils::xsingle2c(&input[i]); + *d++ = utils::string::xsingle2c(&input[i]); break; case 2: case 3: /* Use the last two from the end. */ - *d++ = modsecurity::utils::x2c(&input[i + j - 2]); + *d++ = utils::string::x2c(&input[i + j - 2]); break; case 4: /* Use the last two from the end, but request * a full width check. */ - *d = modsecurity::utils::x2c(&input[i + j - 2]); + *d = utils::string::x2c(&input[i + j - 2]); fullcheck = 1; break; @@ -112,7 +112,7 @@ int CssDecode::css_decode_inplace(unsigned char *input, int64_t input_len) { * a full width check if the number is greater * or equal to 0xFFFF. */ - *d = modsecurity::utils::x2c(&input[i + j - 2]); + *d = utils::string::x2c(&input[i + j - 2]); /* Do full check if first byte is 0 */ if (input[i] == '0') { fullcheck = 1; @@ -126,7 +126,7 @@ int CssDecode::css_decode_inplace(unsigned char *input, int64_t input_len) { * a full width check if the number is greater * or equal to 0xFFFF. */ - *d = modsecurity::utils::x2c(&input[i + j - 2]); + *d = utils::string::x2c(&input[i + j - 2]); /* Do full check if first/second bytes are 0 */ if ((input[i] == '0') diff --git a/src/actions/transformations/escape_seq_decode.cc b/src/actions/transformations/escape_seq_decode.cc index 44bf136b..2525e306 100644 --- a/src/actions/transformations/escape_seq_decode.cc +++ b/src/actions/transformations/escape_seq_decode.cc @@ -92,7 +92,7 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input, if ((i + 3 < input_len) && (isxdigit(input[i + 2])) && (isxdigit(input[i + 3]))) { /* Two digits. */ - c = modsecurity::utils::x2c(&input[i + 2]); + c = utils::string::x2c(&input[i + 2]); i += 4; } else { /* Invalid encoding, do nothing. */ diff --git a/src/actions/transformations/hex_decode.cc b/src/actions/transformations/hex_decode.cc index 9c99d2f7..f9f47397 100644 --- a/src/actions/transformations/hex_decode.cc +++ b/src/actions/transformations/hex_decode.cc @@ -65,7 +65,7 @@ int HexDecode::inplace(unsigned char *data, int len) { } for (i = 0; i <= len - 2; i += 2) { - *d++ = modsecurity::utils::x2c(&data[i]); + *d++ = utils::string::x2c(&data[i]); count++; } *d = '\0'; diff --git a/src/actions/transformations/js_decode.cc b/src/actions/transformations/js_decode.cc index cc7ee2af..a84442ce 100644 --- a/src/actions/transformations/js_decode.cc +++ b/src/actions/transformations/js_decode.cc @@ -72,7 +72,7 @@ int JsDecode::inplace(unsigned char *input, u_int64_t input_len) { /* \uHHHH */ /* Use only the lower byte. */ - *d = modsecurity::utils::x2c(&input[i + 4]); + *d = utils::string::x2c(&input[i + 4]); /* Full width ASCII (ff01 - ff5e) needs 0x20 added */ if ((*d > 0x00) && (*d < 0x5f) @@ -87,7 +87,7 @@ int JsDecode::inplace(unsigned char *input, u_int64_t input_len) { } else if ((i + 3 < input_len) && (input[i + 1] == 'x') && VALID_HEX(input[i + 2]) && VALID_HEX(input[i + 3])) { /* \xHH */ - *d++ = modsecurity::utils::x2c(&input[i + 2]); + *d++ = utils::string::x2c(&input[i + 2]); count++; i += 4; } else if ((i + 1 < input_len) && ISODIGIT(input[i + 1])) { diff --git a/src/actions/transformations/sql_hex_decode.cc b/src/actions/transformations/sql_hex_decode.cc index e1fe560e..3ead2e40 100644 --- a/src/actions/transformations/sql_hex_decode.cc +++ b/src/actions/transformations/sql_hex_decode.cc @@ -96,7 +96,7 @@ int SqlHexDecode::inplace(unsigned char *data, int len) { } while (VALID_HEX(data[0]) && VALID_HEX(data[1])) { - *d++ = modsecurity::utils::x2c(data); + *d++ = utils::string::x2c(data); data += 2; count += 2; } diff --git a/src/actions/transformations/url_decode_uni.cc b/src/actions/transformations/url_decode_uni.cc index 0014efa8..430758b5 100644 --- a/src/actions/transformations/url_decode_uni.cc +++ b/src/actions/transformations/url_decode_uni.cc @@ -114,7 +114,7 @@ int UrlDecodeUni::inplace(unsigned char *input, u_int64_t input_len, } else { /* We first make use of the lower byte here, * ignoring the higher byte. */ - *d = modsecurity::utils::x2c(&input[i + 4]); + *d = utils::string::x2c(&input[i + 4]); /* Full width ASCII (ff01 - ff5e) * needs 0x20 added */ @@ -153,7 +153,7 @@ int UrlDecodeUni::inplace(unsigned char *input, u_int64_t input_len, char c2 = input[i + 2]; if (VALID_HEX(c1) && VALID_HEX(c2)) { - *d++ = modsecurity::utils::x2c(&input[i + 1]); + *d++ = utils::string::x2c(&input[i + 1]); count++; i += 3; } else { diff --git a/src/actions/transformations/url_encode.cc b/src/actions/transformations/url_encode.cc index ffcf2ed7..b3580a9f 100644 --- a/src/actions/transformations/url_encode.cc +++ b/src/actions/transformations/url_encode.cc @@ -69,7 +69,7 @@ std::string UrlEncode::url_enc(const char *input, } else { *d++ = '%'; count++; - modsecurity::utils::c2x(c, (unsigned char *)d); + utils::string::c2x(c, (unsigned char *)d); d += 2; count++; count++; diff --git a/src/actions/transformations/utf8_to_unicode.cc b/src/actions/transformations/utf8_to_unicode.cc index 175cea5a..8a9b2cb8 100644 --- a/src/actions/transformations/utf8_to_unicode.cc +++ b/src/actions/transformations/utf8_to_unicode.cc @@ -93,7 +93,7 @@ char *Utf8ToUnicode::inplace(unsigned char *input, count++; if (count <= len) { if (c == 0) - *data = modsecurity::utils::x2c(&c); + *data = utils::string::x2c(&c); else *data++ = c; } diff --git a/src/collection/backend/in_memory-per_process.cc b/src/collection/backend/in_memory-per_process.cc index 0f3bbeb0..d8bb1213 100644 --- a/src/collection/backend/in_memory-per_process.cc +++ b/src/collection/backend/in_memory-per_process.cc @@ -28,9 +28,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - - namespace modsecurity { namespace collection { namespace backend { @@ -103,8 +100,8 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var, if (x.first.at(keySize) != ':') { continue; } - std::string fu = String::toupper(x.first); - std::string fvar = String::toupper(var); + std::string fu = utils::string::toupper(x.first); + std::string fvar = utils::string::toupper(var); if (fu.compare(0, keySize, fvar) != 0) { continue; } diff --git a/src/collection/collections.cc b/src/collection/collections.cc index 82ad72a6..61e7c140 100644 --- a/src/collection/collections.cc +++ b/src/collection/collections.cc @@ -30,9 +30,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - - namespace modsecurity { namespace collection { @@ -64,28 +61,28 @@ Collections::~Collections() { void Collections::storeOrUpdateFirst(const std::string& collectionName, const std::string& variableName, const std::string& targetValue) { - if (String::tolower(collectionName) == "ip" + if (utils::string::tolower(collectionName) == "ip" && !m_ip_collection_key.empty()) { m_ip_collection->storeOrUpdateFirst(collectionName + ":" + variableName, m_ip_collection_key, targetValue); return; } - if (String::tolower(collectionName) == "global" + if (utils::string::tolower(collectionName) == "global" && !m_global_collection_key.empty()) { m_global_collection->storeOrUpdateFirst(collectionName + ":" + variableName, m_global_collection_key, targetValue); return; } - if (String::tolower(collectionName) == "resource" + if (utils::string::tolower(collectionName) == "resource" && !m_resource_collection_key.empty()) { m_resource_collection->storeOrUpdateFirst(collectionName + ":" + variableName, m_resource_collection_key, targetValue); return; } - if (String::tolower(collectionName) == "session" + if (utils::string::tolower(collectionName) == "session" && !m_session_collection_key.empty()) { m_session_collection->storeOrUpdateFirst(collectionName + ":" + variableName, m_session_collection_key, targetValue); @@ -137,7 +134,7 @@ std::string* Collections::resolveFirst(const std::string& var) { for (auto &a : *this) { std::string *res = a.second->resolveFirst( - String::toupper(a.first) + ":" + var); + utils::string::toupper(a.first) + ":" + var); if (res != NULL) { return res; } @@ -149,38 +146,39 @@ std::string* Collections::resolveFirst(const std::string& var) { std::string* Collections::resolveFirst(const std::string& collectionName, const std::string& var) { - if (String::tolower(collectionName) == "ip" + if (utils::string::tolower(collectionName) == "ip" && !m_ip_collection_key.empty()) { return m_ip_collection->resolveFirst( - String::toupper(collectionName) + utils::string::toupper(collectionName) + ":" + var, m_ip_collection_key); } - if (String::tolower(collectionName) == "global" + if (utils::string::tolower(collectionName) == "global" && !m_global_collection_key.empty()) { return m_global_collection->resolveFirst( - String::toupper(collectionName) + utils::string::toupper(collectionName) + ":" + var, m_global_collection_key); } - if (String::tolower(collectionName) == "resource" + if (utils::string::tolower(collectionName) == "resource" && !m_resource_collection_key.empty()) { return m_resource_collection->resolveFirst( - String::toupper(collectionName) + utils::string::toupper(collectionName) + ":" + var, m_resource_collection_key); } - if (String::tolower(collectionName) == "session" + if (utils::string::tolower(collectionName) == "session" && !m_session_collection_key.empty()) { return m_session_collection->resolveFirst( - String::toupper(collectionName) + utils::string::toupper(collectionName) + ":" + var, m_session_collection_key); } for (auto &a : *this) { - if (String::tolower(a.first) == String::tolower(collectionName)) { + if (utils::string::tolower(a.first) + == utils::string::tolower(collectionName)) { std::string *res = a.second->resolveFirst( - String::toupper(a.first) + utils::string::toupper(a.first) + ":" + var); if (res != NULL) { return res; @@ -200,7 +198,8 @@ std::string Collections::resolveFirstCopy(const std::string& var) { } for (auto &a : *this) { - std::string res = a.second->resolveFirstCopy(String::toupper(a.first) + + std::string res = a.second->resolveFirstCopy( + utils::string::toupper(a.first) + ":" + var); if (res.empty() == false) { return res; @@ -213,38 +212,39 @@ std::string Collections::resolveFirstCopy(const std::string& var) { std::string Collections::resolveFirstCopy(const std::string& collectionName, const std::string& var) { - if (String::tolower(collectionName) == "ip" + if (utils::string::tolower(collectionName) == "ip" && !m_ip_collection_key.empty()) { return m_ip_collection->resolveFirstCopy( - String::toupper(collectionName) + utils::string::toupper(collectionName) + ":" + var, m_ip_collection_key); } - if (String::tolower(collectionName) == "global" + if (utils::string::tolower(collectionName) == "global" && !m_global_collection_key.empty()) { return m_global_collection->resolveFirstCopy( - String::toupper(collectionName) + ":" + var, + utils::string::toupper(collectionName) + ":" + var, m_global_collection_key); } - if (String::tolower(collectionName) == "resource" + if (utils::string::tolower(collectionName) == "resource" && !m_resource_collection_key.empty()) { return m_resource_collection->resolveFirstCopy( - String::toupper(collectionName) + ":" + var, + utils::string::toupper(collectionName) + ":" + var, m_resource_collection_key); } - if (String::tolower(collectionName) == "session" + if (utils::string::tolower(collectionName) == "session" && !m_session_collection_key.empty()) { return m_session_collection->resolveFirstCopy( - String::toupper(collectionName) + ":" + var, + utils::string::toupper(collectionName) + ":" + var, m_session_collection_key); } for (auto &a : *this) { - if (String::tolower(a.first) == String::tolower(collectionName)) { + if (utils::string::tolower(a.first) + == utils::string::tolower(collectionName)) { std::string res = a.second->resolveFirstCopy( - String::toupper(a.first) + ":" + var); + utils::string::toupper(a.first) + ":" + var); if (res.empty() == false) { return res; } @@ -266,27 +266,27 @@ void Collections::resolveSingleMatch(const std::string& var, const std::string& collection, std::vector *l) { - if (String::tolower(collection) == "ip" + if (utils::string::tolower(collection) == "ip" && !m_ip_collection_key.empty()) { m_ip_collection->resolveSingleMatch(var, m_ip_collection_key, l); return; } - if (String::tolower(collection) == "global" + if (utils::string::tolower(collection) == "global" && !m_global_collection_key.empty()) { m_global_collection->resolveSingleMatch(var, m_global_collection_key, l); return; } - if (String::tolower(collection) == "resource" + if (utils::string::tolower(collection) == "resource" && !m_resource_collection_key.empty()) { m_resource_collection->resolveSingleMatch(var, m_resource_collection_key, l); return; } - if (String::tolower(collection) == "session" + if (utils::string::tolower(collection) == "session" && !m_session_collection_key.empty()) { m_session_collection->resolveSingleMatch(var, m_session_collection_key, l); @@ -308,27 +308,27 @@ void Collections::resolveMultiMatches(const std::string& var, void Collections::resolveMultiMatches(const std::string& var, const std::string& collection, std::vector *l) { - if (String::tolower(collection) == "ip" + if (utils::string::tolower(collection) == "ip" && !m_ip_collection_key.empty()) { m_ip_collection->resolveMultiMatches(var, m_ip_collection_key, l); return; } - if (String::tolower(collection) == "global" + if (utils::string::tolower(collection) == "global" && !m_global_collection_key.empty()) { m_global_collection->resolveMultiMatches(var, m_global_collection_key, l); return; } - if (String::tolower(collection) == "resource" + if (utils::string::tolower(collection) == "resource" && !m_resource_collection_key.empty()) { m_resource_collection->resolveMultiMatches(var, m_resource_collection_key, l); return; } - if (String::tolower(collection) == "session" + if (utils::string::tolower(collection) == "session" && !m_session_collection_key.empty()) { m_session_collection->resolveMultiMatches(var, m_session_collection_key, l); @@ -349,34 +349,34 @@ void Collections::resolveRegularExpression(const std::string& var, void Collections::resolveRegularExpression(const std::string& var, const std::string& collection, std::vector *l) { - if (String::tolower(collection) == "ip" + if (utils::string::tolower(collection) == "ip" && !m_ip_collection_key.empty()) { m_ip_collection->resolveRegularExpression( - String::toupper(collection) + utils::string::toupper(collection) + ":" + var, m_ip_collection_key, l); return; } - if (String::tolower(collection) == "global" + if (utils::string::tolower(collection) == "global" && !m_global_collection_key.empty()) { m_global_collection->resolveRegularExpression( - String::toupper(collection) + utils::string::toupper(collection) + ":" + var, m_global_collection_key, l); return; } - if (String::tolower(collection) == "resource" + if (utils::string::tolower(collection) == "resource" && !m_resource_collection_key.empty()) { m_resource_collection->resolveRegularExpression( - String::toupper(collection) + utils::string::toupper(collection) + ":" + var, m_resource_collection_key, l); return; } - if (String::tolower(collection) == "session" + if (utils::string::tolower(collection) == "session" && !m_session_collection_key.empty()) { m_session_collection->resolveRegularExpression( - String::toupper(collection) + utils::string::toupper(collection) + ":" + var, m_session_collection_key, l); return; } diff --git a/src/macro_expansion.cc b/src/macro_expansion.cc index 302d5fae..409a3e86 100644 --- a/src/macro_expansion.cc +++ b/src/macro_expansion.cc @@ -22,8 +22,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - namespace modsecurity { MacroExpansion::MacroExpansion() { } @@ -75,7 +73,7 @@ std::string MacroExpansion::expand(const std::string& input, std::string var = std::string(variable, collection + 1, variable.length() - (collection + 1)); - if (String::toupper(col) == "RULE") { + if (utils::string::toupper(col) == "RULE") { if (rule == NULL) { transaction->debug(9, "macro expansion: cannot resolve " \ "RULE variable without the Rule object"); diff --git a/src/parser/seclang-parser.yy b/src/parser/seclang-parser.yy index 10c1273f..bf815ba8 100644 --- a/src/parser/seclang-parser.yy +++ b/src/parser/seclang-parser.yy @@ -122,7 +122,6 @@ using modsecurity::actions::Ver; using modsecurity::actions::transformations::None; using modsecurity::actions::transformations::Transformation; using modsecurity::operators::Operator; -using modsecurity::utils::String; @@ -578,7 +577,7 @@ expression: } | CONFIG_DIR_SEC_MARKER { - driver.addSecMarker(String::removeBracketsIfNeeded($1)); + driver.addSecMarker(modsecurity::utils::string::removeBracketsIfNeeded($1)); } | CONFIG_DIR_RULE_ENG CONFIG_VALUE_OFF { diff --git a/src/parser/seclang-scanner.ll b/src/parser/seclang-scanner.ll index 88e49f92..89c55369 100755 --- a/src/parser/seclang-scanner.ll +++ b/src/parser/seclang-scanner.ll @@ -11,7 +11,6 @@ using modsecurity::Parser::Driver; using modsecurity::Utils::HttpsClient; -using modsecurity::utils::String; typedef yy::seclang_parser p; @@ -436,7 +435,7 @@ VAR_FREE_TEXT_SPACE_COMMA [^, \t\"]+ std::string key; std::string url; - std::vector conf = String::split(yytext, ' '); + std::vector conf = modsecurity::utils::string::split(yytext, ' '); key = conf[1]; url = conf[2]; c.setKey(key); diff --git a/src/request_body_processor/multipart.cc b/src/request_body_processor/multipart.cc index bb777ec4..90fc49cd 100644 --- a/src/request_body_processor/multipart.cc +++ b/src/request_body_processor/multipart.cc @@ -31,9 +31,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - - namespace modsecurity { namespace RequestBodyProcessor { @@ -720,7 +717,7 @@ int Multipart::process_part_header(std::string *error) { } new_value = std::string(data); - String::chomp(&new_value); + utils::string::chomp(&new_value); /* update the header value in the table */ header_value = m_mpp->m_headers.at(m_mpp->m_last_header_name); @@ -770,7 +767,7 @@ int Multipart::process_part_header(std::string *error) { data++; } header_value = std::string(data); - String::chomp(&header_value); + utils::string::chomp(&header_value); /* error if the name already exists */ if (m_mpp->m_headers.count(header_name) > 0) { @@ -1040,7 +1037,7 @@ int Multipart::multipart_complete(std::string *error) { int Multipart::count_boundary_params(const std::string& str_header_value) { - std::string lower = String::tolower(str_header_value); + std::string lower = utils::string::tolower(str_header_value); const char *header_value = lower.c_str(); char *duplicate = NULL; char *s = NULL; diff --git a/src/rule.cc b/src/rule.cc index 58b14347..5df53336 100644 --- a/src/rule.cc +++ b/src/rule.cc @@ -37,7 +37,6 @@ using modsecurity::Variables::Variations::Exclusion; -using modsecurity::utils::String; namespace modsecurity { @@ -428,8 +427,8 @@ bool Rule::evaluate(Transaction *trasn) { } #ifndef NO_LOGS - trasn->debug(9, "Target value: \"" + String::limitTo(80, - String::toHexIfNeeded(value)) \ + trasn->debug(9, "Target value: \"" + utils::string::limitTo(80, + utils::string::toHexIfNeeded(value)) \ + "\" (Variable: " + v->m_key + ")"); #endif @@ -453,9 +452,10 @@ bool Rule::evaluate(Transaction *trasn) { if (this->op->m_match_message.empty() == true) { ruleMessage->m_match = "Matched \"Operator `" + this->op->m_op + "' with parameter `" + - String::limitTo(200, this->op->m_param) + + utils::string::limitTo(200, this->op->m_param) + "' against variable `" + v->m_key + "' (Value: `" + - String::limitTo(100, String::toHexIfNeeded(value)) + + utils::string::limitTo(100, + utils::string::toHexIfNeeded(value)) + "' ) \" at " + v->m_key; } else { ruleMessage->m_match = this->op->m_match_message; diff --git a/src/rule_message.cc b/src/rule_message.cc index 93036369..0d0d8262 100644 --- a/src/rule_message.cc +++ b/src/rule_message.cc @@ -48,7 +48,7 @@ std::string RuleMessage::errorLog(Transaction *trans) { msg.append(" [uri \"" + std::string(trans->m_uri) + "\"]"); msg.append(" [unique_id \"" + trans->m_id + "\"]"); - return modsecurity::utils::String::toHexIfNeeded(msg); + return modsecurity::utils::string::toHexIfNeeded(msg); } } // namespace modsecurity diff --git a/src/rules_exceptions.cc b/src/rules_exceptions.cc index e4e7aad3..64d647d9 100644 --- a/src/rules_exceptions.cc +++ b/src/rules_exceptions.cc @@ -20,16 +20,13 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - - namespace modsecurity { bool RulesExceptions::load(const std::string &a, std::string *error) { - std::vector toRemove = String::split(a, ' '); + std::vector toRemove = utils::string::split(a, ' '); for (std::string &a : toRemove) { - std::string b = String::removeBracketsIfNeeded(a); + std::string b = utils::string::removeBracketsIfNeeded(a); size_t dash = b.find('-'); if (dash != std::string::npos) { diff --git a/src/transaction.cc b/src/transaction.cc index 1d18ed31..934da714 100644 --- a/src/transaction.cc +++ b/src/transaction.cc @@ -55,7 +55,6 @@ using modsecurity::actions::Action; using modsecurity::RequestBodyProcessor::Multipart; using modsecurity::RequestBodyProcessor::XML; -using modsecurity::utils::String; namespace modsecurity { @@ -251,7 +250,7 @@ int Transaction::processConnection(const char *client, int cPort, bool Transaction::extractArguments(const std::string &orig, const std::string& buf) { char sep1 = '&'; - std::vector key_value_sets = String::split(buf, sep1); + std::vector key_value_sets = utils::string::split(buf, sep1); for (std::string t : key_value_sets) { char sep2 = '='; @@ -263,7 +262,7 @@ bool Transaction::extractArguments(const std::string &orig, std::string key; std::string value; - std::vector key_value = String::split(t, sep2); + std::vector key_value = utils::string::split(t, sep2); for (auto& a : key_value) { if (i == 0) { key = a; @@ -499,16 +498,17 @@ int Transaction::addRequestHeader(const std::string& key, this->m_collections.store("REQUEST_HEADERS:" + key, value); - std::string keyl = String::tolower(key); + std::string keyl = utils::string::tolower(key); if (keyl == "authorization") { - std::vector type = String::split(value, ' '); + std::vector type = utils::string::split(value, ' '); this->m_collections.store("AUTH_TYPE", type[0]); } if (keyl == "cookie") { - std::vector cookies = String::split(value, ';'); + std::vector cookies = utils::string::split(value, ';'); while (cookies.empty() == false) { - std::vector s = String::split(cookies.back(), '='); + std::vector s = utils::string::split(cookies.back(), + '='); if (s.size() > 1) { if (s[0].at(0) == ' ') { s[0].erase(0, 1); @@ -530,7 +530,7 @@ int Transaction::addRequestHeader(const std::string& key, if (keyl == "content-type") { std::string multipart("multipart/form-data"); - std::string l = String::tolower(value); + std::string l = utils::string::tolower(value); if (l.compare(0, multipart.length(), multipart) == 0) { this->m_requestBodyType = MultiPartRequestBody; m_collections.store("REQBODY_PROCESSOR", "MULTIPART"); @@ -543,7 +543,7 @@ int Transaction::addRequestHeader(const std::string& key, } if (keyl == "host") { - std::vector host = String::split(value, ':'); + std::vector host = utils::string::split(value, ':'); m_collections.store("SERVER_NAME", host[0]); } return 1; @@ -948,7 +948,7 @@ int Transaction::addResponseHeader(const std::string& key, this->m_collections.store("RESPONSE_HEADERS:" + key, value); - if (String::tolower(key) == "content-type") { + if (utils::string::tolower(key) == "content-type") { this->m_responseContentType->assign(value); } return 1; @@ -1307,15 +1307,15 @@ std::string Transaction::toOldAuditLogFormatIndex(const std::string &filename, strftime(tstr, 299, "[%d/%b/%Y:%H:%M:%S %z]", &timeinfo); - ss << String::dash_if_empty( + ss << utils::string::dash_if_empty( this->m_collections.resolveFirst("REQUEST_HEADERS:Host")) << " "; - ss << String::dash_if_empty(this->m_clientIpAddress) << " "; + ss << utils::string::dash_if_empty(this->m_clientIpAddress) << " "; /** TODO: Check variable */ - ss << String::dash_if_empty( + ss << utils::string::dash_if_empty( this->m_collections.resolveFirst("REMOTE_USER")); ss << " "; /** TODO: Check variable */ - ss << String::dash_if_empty( + ss << utils::string::dash_if_empty( this->m_collections.resolveFirst("LOCAL_USER")); ss << " "; ss << tstr << " "; @@ -1329,15 +1329,15 @@ std::string Transaction::toOldAuditLogFormatIndex(const std::string &filename, ss << this->m_httpCodeReturned << " "; ss << this->m_responseBody.tellp(); /** TODO: Check variable */ - ss << String::dash_if_empty( + ss << utils::string::dash_if_empty( this->m_collections.resolveFirst("REFERER")) << " "; ss << "\""; - ss << String::dash_if_empty( + ss << utils::string::dash_if_empty( this->m_collections.resolveFirst("REQUEST_HEADERS:User-Agent")); ss << "\" "; ss << this->m_id << " "; /** TODO: Check variable */ - ss << String::dash_if_empty( + ss << utils::string::dash_if_empty( this->m_collections.resolveFirst("REFERER")) << " "; ss << filename << " "; @@ -1435,7 +1435,7 @@ std::string Transaction::toJSON(int parts) { const unsigned char *buf; size_t len; yajl_gen g = NULL; - std::string ts = String::ascTime(&m_timeStamp).c_str(); + std::string ts = utils::string::ascTime(&m_timeStamp).c_str(); std::string uniqueId = UniqueId::uniqueId(); g = yajl_gen_alloc(NULL); diff --git a/src/utils/decode.cc b/src/utils/decode.cc index c33f46d4..04dc1218 100644 --- a/src/utils/decode.cc +++ b/src/utils/decode.cc @@ -43,7 +43,7 @@ int urldecode_nonstrict_inplace(unsigned char *input, char c1 = input[i + 1]; char c2 = input[i + 2]; if (VALID_HEX(c1) && VALID_HEX(c2)) { - uint64_t uni = x2c(&input[i + 1]); + uint64_t uni = string::x2c(&input[i + 1]); *d++ = (wchar_t)uni; count++; @@ -99,8 +99,8 @@ std::string uri_decode(const std::string & sSrc) { while (pSrc < SRC_LAST_DEC) { if (*pSrc == '%') { char dec1, dec2; - if (-1 != (dec1 = HEX2DEC[*(pSrc + 1)]) - && -1 != (dec2 = HEX2DEC[*(pSrc + 2)])) { + if (-1 != (dec1 = string::HEX2DEC[*(pSrc + 1)]) + && -1 != (dec2 = string::HEX2DEC[*(pSrc + 2)])) { *pEnd++ = (dec1 << 4) + dec2; pSrc += 3; continue; diff --git a/src/utils/string.cc b/src/utils/string.cc index 797c9cf8..36b7c3bf 100644 --- a/src/utils/string.cc +++ b/src/utils/string.cc @@ -44,16 +44,17 @@ namespace modsecurity { namespace utils { +namespace string { -std::string String::ascTime(time_t *t) { +std::string ascTime(time_t *t) { std::string ts = std::ctime(t); ts.pop_back(); return ts; } -std::string String::dash_if_empty(const std::string *str) { +std::string dash_if_empty(const std::string *str) { if (str == NULL || str->empty()) { return "-"; } @@ -62,7 +63,7 @@ std::string String::dash_if_empty(const std::string *str) { } -std::string String::dash_if_empty(const char *str) { +std::string dash_if_empty(const char *str) { if (str == NULL || strlen(str) == 0) { return "-"; } @@ -71,7 +72,7 @@ std::string String::dash_if_empty(const char *str) { } -std::string String::limitTo(int amount, const std::string &str) { +std::string limitTo(int amount, const std::string &str) { std::string ret; if (str.length() > amount) { @@ -85,7 +86,7 @@ std::string String::limitTo(int amount, const std::string &str) { } -std::string String::removeBracketsIfNeeded(std::string a) { +std::string removeBracketsIfNeeded(std::string a) { if ((a.at(0) == '"') && (a.at(a.length()-1) == '"')) { a.pop_back(); a.erase(0, 1); @@ -94,7 +95,7 @@ std::string String::removeBracketsIfNeeded(std::string a) { } -std::string String::string_to_hex(const std::string& input) { +std::string string_to_hex(const std::string& input) { static const char* const lut = "0123456789ABCDEF"; size_t len = input.length(); @@ -109,7 +110,7 @@ std::string String::string_to_hex(const std::string& input) { } -std::string String::toHexIfNeeded(const std::string &str) { +std::string toHexIfNeeded(const std::string &str) { std::stringstream res; for (int i = 0; i < str.size(); i++) { @@ -125,7 +126,7 @@ std::string String::toHexIfNeeded(const std::string &str) { } -std::string String::tolower(std::string str) { +std::string tolower(std::string str) { std::string value; value.resize(str.length()); @@ -138,7 +139,7 @@ std::string String::tolower(std::string str) { } -std::string String::toupper(std::string str) { +std::string toupper(std::string str) { std::locale loc; std::string value; @@ -150,7 +151,7 @@ std::string String::toupper(std::string str) { } -std::vector String::split(std::string str, char delimiter) { +std::vector split(std::string str, char delimiter) { std::vector internal; std::stringstream ss(str); // Turn the string into a stream. std::string tok; @@ -163,7 +164,7 @@ std::vector String::split(std::string str, char delimiter) { } -void String::chomp(std::string *str) { +void chomp(std::string *str) { std::string::size_type pos = str->find_last_not_of("\n\r"); if (pos != std::string::npos) { str->erase(pos+1, str->length()-pos-1); @@ -205,5 +206,6 @@ unsigned char *c2x(unsigned what, unsigned char *where) { } +} // namespace string } // namespace utils } // namespace modsecurity diff --git a/src/utils/string.h b/src/utils/string.h index a4b96255..72e4f375 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -18,8 +18,8 @@ #include #include -#ifndef SRC_UTILS_MSC_STRING_H_ -#define SRC_UTILS_MSC_STRING_H_ +#ifndef SRC_UTILS_STRING_H_ +#define SRC_UTILS_STRING_H_ #define VALID_HEX(X) (((X >= '0') && (X <= '9')) || \ ((X >= 'a') && (X <= 'f')) || ((X >= 'A') && (X <= 'F'))) @@ -29,6 +29,7 @@ namespace modsecurity { namespace utils { +namespace string { const char HEX2DEC[256] = { /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ @@ -54,26 +55,24 @@ const char HEX2DEC[256] = { }; -class String { - public: - static std::string ascTime(time_t *t); - static std::string dash_if_empty(const char *str); - static std::string dash_if_empty(const std::string *str); - static std::string limitTo(int amount, const std::string &str); - static std::string removeBracketsIfNeeded(std::string a); - static std::string string_to_hex(const std::string& input); - static std::string toHexIfNeeded(const std::string &str); - static std::string tolower(std::string str); - static std::string toupper(std::string str); - static std::vector split(std::string str, char delimiter); - static void chomp(std::string *str); -}; +std::string ascTime(time_t *t); +std::string dash_if_empty(const char *str); +std::string dash_if_empty(const std::string *str); +std::string limitTo(int amount, const std::string &str); +std::string removeBracketsIfNeeded(std::string a); +std::string string_to_hex(const std::string& input); +std::string toHexIfNeeded(const std::string &str); +std::string tolower(std::string str); +std::string toupper(std::string str); +std::vector split(std::string str, char delimiter); +void chomp(std::string *str); unsigned char x2c(unsigned char *what); unsigned char xsingle2c(unsigned char *what); unsigned char *c2x(unsigned what, unsigned char *where); +} // namespace string } // namespace utils } // namespace modsecurity -#endif // SRC_UTILS_MSC_STRING_H_ +#endif // SRC_UTILS_STRING_H_ diff --git a/src/variables/rule.cc b/src/variables/rule.cc index 38a7f53d..338742c9 100644 --- a/src/variables/rule.cc +++ b/src/variables/rule.cc @@ -47,9 +47,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - - namespace modsecurity { namespace Variables { @@ -57,7 +54,7 @@ void Rule::evaluateInternal(Transaction *t, modsecurity::Rule *rule, std::vector *l) { std::map envs; - std::string m_name_upper = String::toupper(m_name); + std::string m_name_upper = utils::string::toupper(m_name); // id envs.insert(std::pair("RULE:id", @@ -99,7 +96,7 @@ void Rule::evaluateInternal(Transaction *t, } for (auto& x : envs) { - std::string xup = String::toupper(x.first); + std::string xup = utils::string::toupper(x.first); if ((xup.substr(0, m_name_upper.size() + 1) .compare(m_name_upper + ":") != 0) && (xup != m_name_upper)) { diff --git a/src/variables/variable.cc b/src/variables/variable.cc index c8379185..7a19f3ff 100644 --- a/src/variables/variable.cc +++ b/src/variables/variable.cc @@ -25,7 +25,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; using modsecurity::Variables::Variations::Exclusion; @@ -39,7 +38,7 @@ Variable::Variable(std::string name) m_isExclusion(false), m_isCount(false) { if (m_name.find(":") != std::string::npos) { - std::string col = String::toupper( + std::string col = utils::string::toupper( std::string(m_name, 0, m_name.find(":"))); std::string name = std::string(m_name, m_name.find(":") + 1, m_name.size()); @@ -56,19 +55,19 @@ Variable::Variable(std::string name) m_type = MultipleMatches; } - if (String::tolower(m_name) == "tx") { + if (utils::string::tolower(m_name) == "tx") { m_collectionName = "TX"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "ip") { + } else if (utils::string::tolower(m_name) == "ip") { m_collectionName = "IP"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "global") { + } else if (utils::string::tolower(m_name) == "global") { m_collectionName = "GLOBAL"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "resource") { + } else if (utils::string::tolower(m_name) == "resource") { m_collectionName = "RESOURCE"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "session") { + } else if (utils::string::tolower(m_name) == "session") { m_collectionName = "SESSION"; m_type = MultipleMatches; } else if (m_name.find(".") != std::string::npos) { @@ -87,7 +86,7 @@ Variable::Variable(std::string name, VariableKind kind) m_isExclusion(false), m_isCount(false) { if (m_name.find(":") != std::string::npos) { - std::string col = String::toupper( + std::string col = utils::string::toupper( std::string(m_name, 0, m_name.find(":"))); std::string name = std::string(m_name, m_name.find(":") + 1, m_name.size()); @@ -104,19 +103,19 @@ Variable::Variable(std::string name, VariableKind kind) m_type = MultipleMatches; } - if (String::tolower(m_name) == "tx") { + if (utils::string::tolower(m_name) == "tx") { m_collectionName = "TX"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "ip") { + } else if (utils::string::tolower(m_name) == "ip") { m_collectionName = "IP"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "global") { + } else if (utils::string::tolower(m_name) == "global") { m_collectionName = "GLOBAL"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "resource") { + } else if (utils::string::tolower(m_name) == "resource") { m_collectionName = "RESOURCE"; m_type = MultipleMatches; - } else if (String::tolower(m_name) == "session") { + } else if (utils::string::tolower(m_name) == "session") { m_collectionName = "SESSION"; m_type = MultipleMatches; } else if (m_name.find(".") != std::string::npos) { diff --git a/test/unit/unit.cc b/test/unit/unit.cc index c963df31..2ff5592a 100644 --- a/test/unit/unit.cc +++ b/test/unit/unit.cc @@ -31,7 +31,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; using modsecurity_test::UnitTest; using modsecurity_test::ModSecurityTest; using modsecurity_test::ModSecurityTestResults; @@ -91,7 +90,8 @@ void perform_unit_test(ModSecurityTest *test, UnitTest *t, if (test->m_automake_output) { std::cout << t->name << " " - << String::toHexIfNeeded(t->input) << std::endl; + << modsecurity::utils::string::toHexIfNeeded(t->input) + << std::endl; } } diff --git a/test/unit/unit_test.cc b/test/unit/unit_test.cc index 2d884055..7924cb02 100644 --- a/test/unit/unit_test.cc +++ b/test/unit/unit_test.cc @@ -27,9 +27,6 @@ #include "src/utils/string.h" -using modsecurity::utils::String; - - namespace modsecurity_test { @@ -112,9 +109,11 @@ std::string UnitTest::print() { i << this->obtained << "\"" << std::endl; } if (this->output != this->obtainedOutput) { - i << "Expecting: \"" << String::toHexIfNeeded(this->output); + i << "Expecting: \""; + i << modsecurity::utils::string::toHexIfNeeded(this->output); i << "\" - returned: \""; - i << String::toHexIfNeeded(this->obtainedOutput) << "\""; + i << modsecurity::utils::string::toHexIfNeeded(this->obtainedOutput); + i << "\""; i << std::endl; }