Moves string related functions from utils' to utils/string'

This commit is contained in:
Felipe Zimmerle 2016-11-01 14:59:06 -03:00
parent 9733cacd4d
commit 73c4d69174
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
25 changed files with 403 additions and 236 deletions

View File

@ -219,7 +219,8 @@ UTILS = \
utils/md5.cc \
utils/msc_tree.cc \
utils/regex.cc \
utils/sha1.cc
utils/sha1.cc \
utils/msc_string.cc
COLLECTION = \
collection/collections.cc \

View File

@ -21,13 +21,16 @@
#include "modsecurity/transaction.h"
#include "modsecurity/rule.h"
#include "src/utils.h"
#include "utils/msc_string.h"
#include "modsecurity/modsecurity.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace actions {
bool Allow::init(std::string *error) {
std::string a = tolower(m_parser_payload);
std::string a = String::tolower(m_parser_payload);
if (a == "phase") {
m_allowType = PhaseAllowType;

View File

@ -20,6 +20,10 @@
#include <vector>
#include "modsecurity/transaction.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace actions {
@ -28,7 +32,7 @@ namespace ctl {
bool RuleRemoveTargetById::init(std::string *error) {
std::string what(m_parser_payload, 21, m_parser_payload.size() - 21);
std::vector<std::string> param = split(what, ';');
std::vector<std::string> param = String::split(what, ';');
if (param.size() < 2) {
error->assign(what + " is not a valid `ID;VARIABLE'");

View File

@ -20,6 +20,9 @@
#include <vector>
#include "modsecurity/transaction.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace actions {
@ -28,7 +31,7 @@ namespace ctl {
bool RuleRemoveTargetByTag::init(std::string *error) {
std::string what(m_parser_payload, 22, m_parser_payload.size() - 22);
std::vector<std::string> param = split(what, ';');
std::vector<std::string> param = String::split(what, ';');
if (param.size() < 2) {
error->assign(what + " is not a valid `TAG;VARIABLE'");

View File

@ -22,12 +22,16 @@
#include "modsecurity/rule.h"
#include "src/utils.h"
#include "modsecurity/modsecurity.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace actions {
bool Phase::init(std::string *error) {
std::string a = tolower(m_parser_payload);
std::string a = String::tolower(m_parser_payload);
m_phase = -1;
try {

View File

@ -22,6 +22,10 @@
#include "modsecurity/rule.h"
#include "src/macro_expansion.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace actions {
@ -49,7 +53,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 = toupper(m_collectionName);
m_collectionName = String::toupper(m_collectionName);
} else {
error->assign("Missing the collection and/or variable name");
return false;

View File

@ -22,13 +22,17 @@
#include "modsecurity/transaction.h"
#include "modsecurity/rule.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace actions {
bool Severity::init(std::string *error) {
std::string a = tolower(m_parser_payload);
std::string a = String::tolower(m_parser_payload);
if (a == "emergency") {
m_severity = 0;
return true;

View File

@ -26,6 +26,10 @@
#include "modsecurity/collection/variable.h"
#include "src/utils.h"
#include "src/utils/regex.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace collection {
@ -99,8 +103,8 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
if (x.first.at(keySize) != ':') {
continue;
}
std::string fu = toupper(x.first);
std::string fvar = toupper(var);
std::string fu = String::toupper(x.first);
std::string fvar = String::toupper(var);
if (fu.compare(0, keySize, fvar) != 0) {
continue;
}

View File

@ -28,6 +28,10 @@
#include "modsecurity/collection/collection.h"
#include "src/collection/backend/in_memory-per_process.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace collection {
@ -60,28 +64,28 @@ Collections::~Collections() {
void Collections::storeOrUpdateFirst(const std::string& collectionName,
const std::string& variableName,
const std::string& targetValue) {
if (tolower(collectionName) == "ip"
if (String::tolower(collectionName) == "ip"
&& !m_ip_collection_key.empty()) {
m_ip_collection->storeOrUpdateFirst(collectionName + ":"
+ variableName, m_ip_collection_key, targetValue);
return;
}
if (tolower(collectionName) == "global"
if (String::tolower(collectionName) == "global"
&& !m_global_collection_key.empty()) {
m_global_collection->storeOrUpdateFirst(collectionName + ":"
+ variableName, m_global_collection_key, targetValue);
return;
}
if (tolower(collectionName) == "resource"
if (String::tolower(collectionName) == "resource"
&& !m_resource_collection_key.empty()) {
m_resource_collection->storeOrUpdateFirst(collectionName + ":"
+ variableName, m_resource_collection_key, targetValue);
return;
}
if (tolower(collectionName) == "session"
if (String::tolower(collectionName) == "session"
&& !m_session_collection_key.empty()) {
m_session_collection->storeOrUpdateFirst(collectionName + ":"
+ variableName, m_session_collection_key, targetValue);
@ -132,7 +136,8 @@ std::string* Collections::resolveFirst(const std::string& var) {
}
for (auto &a : *this) {
std::string *res = a.second->resolveFirst(toupper(a.first) + ":" + var);
std::string *res = a.second->resolveFirst(
String::toupper(a.first) + ":" + var);
if (res != NULL) {
return res;
}
@ -144,33 +149,38 @@ std::string* Collections::resolveFirst(const std::string& var) {
std::string* Collections::resolveFirst(const std::string& collectionName,
const std::string& var) {
if (tolower(collectionName) == "ip"
if (String::tolower(collectionName) == "ip"
&& !m_ip_collection_key.empty()) {
return m_ip_collection->resolveFirst(toupper(collectionName)
+ ":" + var, m_ip_collection_key);
return m_ip_collection->resolveFirst(
String::toupper(collectionName)
+ ":" + var, m_ip_collection_key);
}
if (tolower(collectionName) == "global"
if (String::tolower(collectionName) == "global"
&& !m_global_collection_key.empty()) {
return m_global_collection->resolveFirst(toupper(collectionName)
+ ":" + var, m_global_collection_key);
return m_global_collection->resolveFirst(
String::toupper(collectionName)
+ ":" + var, m_global_collection_key);
}
if (tolower(collectionName) == "resource"
if (String::tolower(collectionName) == "resource"
&& !m_resource_collection_key.empty()) {
return m_resource_collection->resolveFirst(toupper(collectionName)
+ ":" + var, m_resource_collection_key);
return m_resource_collection->resolveFirst(
String::toupper(collectionName)
+ ":" + var, m_resource_collection_key);
}
if (tolower(collectionName) == "session"
if (String::tolower(collectionName) == "session"
&& !m_session_collection_key.empty()) {
return m_session_collection->resolveFirst(toupper(collectionName)
+ ":" + var, m_session_collection_key);
return m_session_collection->resolveFirst(
String::toupper(collectionName)
+ ":" + var, m_session_collection_key);
}
for (auto &a : *this) {
if (tolower(a.first) == tolower(collectionName)) {
std::string *res = a.second->resolveFirst(toupper(a.first)
if (String::tolower(a.first) == String::tolower(collectionName)) {
std::string *res = a.second->resolveFirst(
String::toupper(a.first)
+ ":" + var);
if (res != NULL) {
return res;
@ -190,7 +200,7 @@ std::string Collections::resolveFirstCopy(const std::string& var) {
}
for (auto &a : *this) {
std::string res = a.second->resolveFirstCopy(toupper(a.first) +
std::string res = a.second->resolveFirstCopy(String::toupper(a.first) +
":" + var);
if (res.empty() == false) {
return res;
@ -203,37 +213,38 @@ std::string Collections::resolveFirstCopy(const std::string& var) {
std::string Collections::resolveFirstCopy(const std::string& collectionName,
const std::string& var) {
if (tolower(collectionName) == "ip"
if (String::tolower(collectionName) == "ip"
&& !m_ip_collection_key.empty()) {
return m_ip_collection->resolveFirstCopy(toupper(collectionName)
+ ":" + var, m_ip_collection_key);
return m_ip_collection->resolveFirstCopy(
String::toupper(collectionName)
+ ":" + var, m_ip_collection_key);
}
if (tolower(collectionName) == "global"
if (String::tolower(collectionName) == "global"
&& !m_global_collection_key.empty()) {
return m_global_collection->resolveFirstCopy(
toupper(collectionName) + ":" + var,
String::toupper(collectionName) + ":" + var,
m_global_collection_key);
}
if (tolower(collectionName) == "resource"
if (String::tolower(collectionName) == "resource"
&& !m_resource_collection_key.empty()) {
return m_resource_collection->resolveFirstCopy(
toupper(collectionName) + ":" + var,
String::toupper(collectionName) + ":" + var,
m_resource_collection_key);
}
if (tolower(collectionName) == "session"
if (String::tolower(collectionName) == "session"
&& !m_session_collection_key.empty()) {
return m_session_collection->resolveFirstCopy(
toupper(collectionName) + ":" + var,
String::toupper(collectionName) + ":" + var,
m_session_collection_key);
}
for (auto &a : *this) {
if (tolower(a.first) == tolower(collectionName)) {
std::string res = a.second->resolveFirstCopy(toupper(a.first)
+ ":" + var);
if (String::tolower(a.first) == String::tolower(collectionName)) {
std::string res = a.second->resolveFirstCopy(
String::toupper(a.first) + ":" + var);
if (res.empty() == false) {
return res;
}
@ -255,27 +266,27 @@ void Collections::resolveSingleMatch(const std::string& var,
const std::string& collection,
std::vector<const Variable *> *l) {
if (tolower(collection) == "ip"
if (String::tolower(collection) == "ip"
&& !m_ip_collection_key.empty()) {
m_ip_collection->resolveSingleMatch(var, m_ip_collection_key, l);
return;
}
if (tolower(collection) == "global"
if (String::tolower(collection) == "global"
&& !m_global_collection_key.empty()) {
m_global_collection->resolveSingleMatch(var,
m_global_collection_key, l);
return;
}
if (tolower(collection) == "resource"
if (String::tolower(collection) == "resource"
&& !m_resource_collection_key.empty()) {
m_resource_collection->resolveSingleMatch(var,
m_resource_collection_key, l);
return;
}
if (tolower(collection) == "session"
if (String::tolower(collection) == "session"
&& !m_session_collection_key.empty()) {
m_session_collection->resolveSingleMatch(var,
m_session_collection_key, l);
@ -297,27 +308,27 @@ void Collections::resolveMultiMatches(const std::string& var,
void Collections::resolveMultiMatches(const std::string& var,
const std::string& collection,
std::vector<const Variable *> *l) {
if (tolower(collection) == "ip"
if (String::tolower(collection) == "ip"
&& !m_ip_collection_key.empty()) {
m_ip_collection->resolveMultiMatches(var, m_ip_collection_key, l);
return;
}
if (tolower(collection) == "global"
if (String::tolower(collection) == "global"
&& !m_global_collection_key.empty()) {
m_global_collection->resolveMultiMatches(var,
m_global_collection_key, l);
return;
}
if (tolower(collection) == "resource"
if (String::tolower(collection) == "resource"
&& !m_resource_collection_key.empty()) {
m_resource_collection->resolveMultiMatches(var,
m_resource_collection_key, l);
return;
}
if (tolower(collection) == "session"
if (String::tolower(collection) == "session"
&& !m_session_collection_key.empty()) {
m_session_collection->resolveMultiMatches(var,
m_session_collection_key, l);
@ -338,30 +349,34 @@ void Collections::resolveRegularExpression(const std::string& var,
void Collections::resolveRegularExpression(const std::string& var,
const std::string& collection,
std::vector<const Variable *> *l) {
if (tolower(collection) == "ip"
if (String::tolower(collection) == "ip"
&& !m_ip_collection_key.empty()) {
m_ip_collection->resolveRegularExpression(toupper(collection)
m_ip_collection->resolveRegularExpression(
String::toupper(collection)
+ ":" + var, m_ip_collection_key, l);
return;
}
if (tolower(collection) == "global"
if (String::tolower(collection) == "global"
&& !m_global_collection_key.empty()) {
m_global_collection->resolveRegularExpression(toupper(collection)
m_global_collection->resolveRegularExpression(
String::toupper(collection)
+ ":" + var, m_global_collection_key, l);
return;
}
if (tolower(collection) == "resource"
if (String::tolower(collection) == "resource"
&& !m_resource_collection_key.empty()) {
m_resource_collection->resolveRegularExpression(toupper(collection)
m_resource_collection->resolveRegularExpression(
String::toupper(collection)
+ ":" + var, m_resource_collection_key, l);
return;
}
if (tolower(collection) == "session"
if (String::tolower(collection) == "session"
&& !m_session_collection_key.empty()) {
m_session_collection->resolveRegularExpression(toupper(collection)
m_session_collection->resolveRegularExpression(
String::toupper(collection)
+ ":" + var, m_session_collection_key, l);
return;
}

View File

@ -20,8 +20,11 @@
#include "src/variables/tx.h"
#include "src/variables/highest_severity.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
MacroExpansion::MacroExpansion() { }
@ -73,7 +76,7 @@ std::string MacroExpansion::expand(const std::string& input,
std::string var = std::string(variable, collection + 1,
variable.length() - (collection + 1));
if (toupper(col) == "RULE") {
if (String::toupper(col) == "RULE") {
if (rule == NULL) {
transaction->debug(9, "macro expansion: cannot resolve " \
"RULE variable without the Rule object");

View File

@ -56,6 +56,7 @@ class Driver;
#include "operators/operator.h"
#include "utils.h"
#include "utils/geo_lookup.h"
#include "utils/msc_string.h"
#include "variables/xml.h"
#include "variables/duration.h"
#include "variables/env.h"
@ -121,7 +122,7 @@ using modsecurity::actions::Ver;
using modsecurity::actions::transformations::None;
using modsecurity::actions::transformations::Transformation;
using modsecurity::operators::Operator;
using modsecurity::removeBracketsIfNeeded;
using modsecurity::utils::String;
@ -577,7 +578,7 @@ expression:
}
| CONFIG_DIR_SEC_MARKER
{
driver.addSecMarker(removeBracketsIfNeeded($1));
driver.addSecMarker(String::removeBracketsIfNeeded($1));
}
| CONFIG_DIR_RULE_ENG CONFIG_VALUE_OFF
{

View File

@ -8,10 +8,11 @@
#include "seclang-parser.hh"
#include "utils/https_client.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::Parser::Driver;
using modsecurity::Utils::HttpsClient;
using modsecurity::split;
using modsecurity::utils::String;
typedef yy::seclang_parser p;
@ -436,7 +437,7 @@ VAR_FREE_TEXT_SPACE_COMMA [^, \t\"]+
std::string key;
std::string url;
std::vector<std::string> conf = split(yytext, ' ');
std::vector<std::string> conf = String::split(yytext, ' ');
key = conf[1];
url = conf[2];
c.setKey(key);

View File

@ -29,6 +29,9 @@
#include "modsecurity/collection/collections.h"
#include "modsecurity/rules.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace RequestBodyProcessor {
@ -716,7 +719,7 @@ int Multipart::process_part_header(std::string *error) {
}
new_value = std::string(data);
chomp(&new_value);
String::chomp(&new_value);
/* update the header value in the table */
header_value = m_mpp->m_headers.at(m_mpp->m_last_header_name);
@ -766,7 +769,7 @@ int Multipart::process_part_header(std::string *error) {
data++;
}
header_value = std::string(data);
chomp(&header_value);
String::chomp(&header_value);
/* error if the name already exists */
if (m_mpp->m_headers.count(header_name) > 0) {
@ -1036,7 +1039,7 @@ int Multipart::multipart_complete(std::string *error) {
int Multipart::count_boundary_params(const std::string& str_header_value) {
std::string lower = tolower(str_header_value);
std::string lower = String::tolower(str_header_value);
const char *header_value = lower.c_str();
char *duplicate = NULL;
char *s = NULL;

View File

@ -31,11 +31,13 @@
#include "actions/tag.h"
#include "variables/variations/exclusion.h"
#include "src/utils.h"
#include "utils/msc_string.h"
#include "modsecurity/rules.h"
#include "src/macro_expansion.h"
using modsecurity::Variables::Variations::Exclusion;
using modsecurity::utils::String;
namespace modsecurity {
@ -426,8 +428,9 @@ bool Rule::evaluate(Transaction *trasn) {
}
#ifndef NO_LOGS
trasn->debug(9, "Target value: \"" + limitTo(80,
toHexIfNeeded(value)) + "\" (Variable: " + v->m_key + ")");
trasn->debug(9, "Target value: \"" + String::limitTo(80,
String::toHexIfNeeded(value)) \
+ "\" (Variable: " + v->m_key + ")");
#endif
ret = this->op->evaluateInternal(trasn, value);
@ -450,10 +453,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 `" +
limitTo(200, this->op->m_param) +
String::limitTo(200, this->op->m_param) +
"' against variable `" + v->m_key + "' (Value: `" +
limitTo(100, toHexIfNeeded(value)) + "' ) \" at " +
v->m_key;
String::limitTo(100, String::toHexIfNeeded(value)) +
"' ) \" at " + v->m_key;
} else {
ruleMessage->m_match = this->op->m_match_message;
}

View File

@ -18,14 +18,17 @@
#include <string>
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
bool RulesExceptions::load(const std::string &a, std::string *error) {
std::vector<std::string> toRemove = modsecurity::split(a, ' ');
std::vector<std::string> toRemove = String::split(a, ' ');
for (std::string &a : toRemove) {
std::string b = removeBracketsIfNeeded(a);
std::string b = String::removeBracketsIfNeeded(a);
size_t dash = b.find('-');
if (dash != std::string::npos) {

View File

@ -42,6 +42,7 @@
#include "audit_log/audit_log.h"
#include "src/unique_id.h"
#include "src/utils.h"
#include "utils/msc_string.h"
#include "modsecurity/rule.h"
#include "modsecurity/rules_properties.h"
#include "src/actions/allow.h"
@ -50,6 +51,7 @@
using modsecurity::actions::Action;
using modsecurity::RequestBodyProcessor::Multipart;
using modsecurity::RequestBodyProcessor::XML;
using modsecurity::utils::String;
namespace modsecurity {
@ -245,7 +247,7 @@ int Transaction::processConnection(const char *client, int cPort,
bool Transaction::extractArguments(const std::string &orig,
const std::string& buf) {
char sep1 = '&';
std::vector<std::string> key_value_sets = split(buf, sep1);
std::vector<std::string> key_value_sets = String::split(buf, sep1);
for (std::string t : key_value_sets) {
char sep2 = '=';
@ -257,7 +259,7 @@ bool Transaction::extractArguments(const std::string &orig,
std::string key;
std::string value;
std::vector<std::string> key_value = split(t, sep2);
std::vector<std::string> key_value = String::split(t, sep2);
for (auto& a : key_value) {
if (i == 0) {
key = a;
@ -492,16 +494,16 @@ int Transaction::addRequestHeader(const std::string& key,
this->m_collections.store("REQUEST_HEADERS:" + key, value);
std::string keyl = tolower(key);
std::string keyl = String::tolower(key);
if (keyl == "authorization") {
std::vector<std::string> type = split(value, ' ');
std::vector<std::string> type = String::split(value, ' ');
this->m_collections.store("AUTH_TYPE", type[0]);
}
if (keyl == "cookie") {
std::vector<std::string> cookies = split(value, ';');
std::vector<std::string> cookies = String::split(value, ';');
while (cookies.empty() == false) {
std::vector<std::string> s = split(cookies.back(), '=');
std::vector<std::string> s = String::split(cookies.back(), '=');
if (s.size() > 1) {
if (s[0].at(0) == ' ') {
s[0].erase(0, 1);
@ -523,7 +525,7 @@ int Transaction::addRequestHeader(const std::string& key,
if (keyl == "content-type") {
std::string multipart("multipart/form-data");
std::string l = tolower(value);
std::string l = String::tolower(value);
if (l.compare(0, multipart.length(), multipart) == 0) {
this->m_requestBodyType = MultiPartRequestBody;
m_collections.store("REQBODY_PROCESSOR", "MULTIPART");
@ -536,7 +538,7 @@ int Transaction::addRequestHeader(const std::string& key,
}
if (keyl == "host") {
std::vector<std::string> host = split(value, ':');
std::vector<std::string> host = String::split(value, ':');
m_collections.store("SERVER_NAME", host[0]);
}
return 1;
@ -941,7 +943,7 @@ int Transaction::addResponseHeader(const std::string& key,
this->m_collections.store("RESPONSE_HEADERS:" + key, value);
if (tolower(key) == "content-type") {
if (String::tolower(key) == "content-type") {
this->m_responseContentType->assign(value);
}
return 1;
@ -1300,14 +1302,16 @@ std::string Transaction::toOldAuditLogFormatIndex(const std::string &filename,
strftime(tstr, 299, "[%d/%b/%Y:%H:%M:%S %z]", &timeinfo);
ss << dash_if_empty(
ss << String::dash_if_empty(
this->m_collections.resolveFirst("REQUEST_HEADERS:Host")) << " ";
ss << dash_if_empty(this->m_clientIpAddress) << " ";
ss << String::dash_if_empty(this->m_clientIpAddress) << " ";
/** TODO: Check variable */
ss << dash_if_empty(this->m_collections.resolveFirst("REMOTE_USER"));
ss << String::dash_if_empty(
this->m_collections.resolveFirst("REMOTE_USER"));
ss << " ";
/** TODO: Check variable */
ss << dash_if_empty(this->m_collections.resolveFirst("LOCAL_USER"));
ss << String::dash_if_empty(
this->m_collections.resolveFirst("LOCAL_USER"));
ss << " ";
ss << tstr << " ";
@ -1320,14 +1324,16 @@ std::string Transaction::toOldAuditLogFormatIndex(const std::string &filename,
ss << this->m_httpCodeReturned << " ";
ss << this->m_responseBody.tellp();
/** TODO: Check variable */
ss << dash_if_empty(this->m_collections.resolveFirst("REFERER")) << " ";
ss << String::dash_if_empty(
this->m_collections.resolveFirst("REFERER")) << " ";
ss << "\"";
ss << dash_if_empty(
ss << String::dash_if_empty(
this->m_collections.resolveFirst("REQUEST_HEADERS:User-Agent"));
ss << "\" ";
ss << this->m_id << " ";
/** TODO: Check variable */
ss << dash_if_empty(this->m_collections.resolveFirst("REFERER")) << " ";
ss << String::dash_if_empty(
this->m_collections.resolveFirst("REFERER")) << " ";
ss << filename << " ";
ss << "0" << " ";
@ -1424,7 +1430,7 @@ std::string Transaction::toJSON(int parts) {
const unsigned char *buf;
size_t len;
yajl_gen g = NULL;
std::string ts = ascTime(&m_timeStamp).c_str();
std::string ts = String::ascTime(&m_timeStamp).c_str();
std::string uniqueId = UniqueId::uniqueId();
g = yajl_gen_alloc(NULL);

View File

@ -133,28 +133,6 @@ int urldecode_nonstrict_inplace(unsigned char *input,
}
std::string removeBracketsIfNeeded(std::string a) {
if ((a.at(0) == '"') && (a.at(a.length()-1) == '"')) {
a.pop_back();
a.erase(0, 1);
}
return a;
}
std::vector<std::string> split(std::string str, char delimiter) {
std::vector<std::string> internal;
std::stringstream ss(str); // Turn the string into a stream.
std::string tok;
while (getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
double random_number(const double from, const double to) {
std::random_device rd;
std::mt19937 mt(rd());
@ -164,22 +142,6 @@ double random_number(const double from, const double to) {
}
std::string dash_if_empty(const std::string *str) {
if (str == NULL || str->empty()) {
return "-";
}
return *str;
}
std::string dash_if_empty(const char *str) {
if (str == NULL || strlen(str) == 0) {
return "-";
}
return std::string(str);
}
double generate_transaction_unique_id() {
@ -187,44 +149,6 @@ double generate_transaction_unique_id() {
}
std::string ascTime(time_t *t) {
std::string ts = std::ctime(t);
ts.pop_back();
return ts;
}
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);
}
}
std::string tolower(std::string str) {
std::string value;
value.resize(str.length());
std::transform(str.begin(),
str.end(),
value.begin(),
::tolower);
return value;
}
std::string toupper(std::string str) {
std::locale loc;
std::string value;
for (std::string::size_type i=0; i < str.length(); ++i) {
value.assign(value + std::toupper(str[i], loc));
}
return value;
}
const char SAFE[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -678,48 +602,6 @@ unsigned char *c2x(unsigned what, unsigned char *where) {
}
std::string string_to_hex(const std::string& input) {
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
std::string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i) {
const unsigned char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
std::string limitTo(int amount, const std::string &str) {
std::string ret;
if (str.length() > amount) {
ret.assign(str, 0, amount);
ret = ret + " (" + std::to_string(str.length() - amount) + " " \
"characters omitted)";
return ret;
}
return str;
}
std::string toHexIfNeeded(const std::string &str) {
std::stringstream res;
for (int i = 0; i < str.size(); i++) {
int c = str.at(i);
if (c < 32 || c > 126) {
res << "\\x" << std::setw(2) << std::setfill('0') << std::hex << c;
} else {
res << str.at(i);
}
}
return res.str();
}
std::vector<std::string> expandEnv(const std::string& var, int flags) {

View File

@ -30,19 +30,12 @@
namespace modsecurity {
std::vector<std::string> split(std::string str, char delimiter);
int urldecode_nonstrict_inplace(unsigned char *input,
uint64_t input_len, int *invalid_count, int *changed);
double random_number(const double from, const double to);
double generate_transaction_unique_id();
std::string ascTime(time_t *t);
void createDir(std::string dir, int mode);
std::string dash_if_empty(const std::string *str);
std::string dash_if_empty(const char *str);
void chomp(std::string *str);
std::string uri_decode(const std::string & sSrc);
std::string tolower(std::string str);
std::string toupper(std::string str);
double cpu_seconds(void);
int js_decode_nonstrict_inplace(unsigned char *input, int64_t input_len);
unsigned char x2c(unsigned char *what);
@ -52,16 +45,12 @@ namespace modsecurity {
int html_entities_decode_inplace(unsigned char *input, int input_len);
int normalize_path_inplace(unsigned char *input, int input_len,
int win, int *changed);
std::string string_to_hex(const std::string& input);
int urldecode_uni_nonstrict_inplace_ex(Transaction *transaction,
unsigned char *input, int64_t input_len, int *changed);
std::string phase_name(int x);
std::string limitTo(int amount, const std::string &str);
std::string toHexIfNeeded(const std::string &str);
std::vector<std::string> expandEnv(const std::string& var, int flags);
std::string find_resource(const std::string& file,
const std::string& param);
std::string removeBracketsIfNeeded(std::string a);
} // namespace modsecurity

View File

@ -869,7 +869,7 @@
*
* Enable the checkup functions (*_self_test).
*/
#define MBEDTLS_SELF_TEST
//#define MBEDTLS_SELF_TEST
/**
* \def MBEDTLS_SHA256_SMALLER

175
src/utils/msc_string.cc Normal file
View File

@ -0,0 +1,175 @@
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <wordexp.h>
#include <stdint.h>
#include <inttypes.h>
#include <algorithm>
#include <random>
#include <memory>
#include <functional>
#include <string>
#include <iostream>
#include <sstream>
#include <cstring>
#if defined _MSC_VER
#include <direct.h>
#elif defined __GNUC__
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include "modsecurity/modsecurity.h"
#include "utils/msc_string.h"
namespace modsecurity {
namespace utils {
std::string 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) {
if (str == NULL || str->empty()) {
return "-";
}
return *str;
}
std::string String::dash_if_empty(const char *str) {
if (str == NULL || strlen(str) == 0) {
return "-";
}
return std::string(str);
}
std::string String::limitTo(int amount, const std::string &str) {
std::string ret;
if (str.length() > amount) {
ret.assign(str, 0, amount);
ret = ret + " (" + std::to_string(str.length() - amount) + " " \
"characters omitted)";
return ret;
}
return str;
}
std::string String::removeBracketsIfNeeded(std::string a) {
if ((a.at(0) == '"') && (a.at(a.length()-1) == '"')) {
a.pop_back();
a.erase(0, 1);
}
return a;
}
std::string String::string_to_hex(const std::string& input) {
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
std::string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i) {
const unsigned char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
std::string String::toHexIfNeeded(const std::string &str) {
std::stringstream res;
for (int i = 0; i < str.size(); i++) {
int c = str.at(i);
if (c < 32 || c > 126) {
res << "\\x" << std::setw(2) << std::setfill('0') << std::hex << c;
} else {
res << str.at(i);
}
}
return res.str();
}
std::string String::tolower(std::string str) {
std::string value;
value.resize(str.length());
std::transform(str.begin(),
str.end(),
value.begin(),
::tolower);
return value;
}
std::string String::toupper(std::string str) {
std::locale loc;
std::string value;
for (std::string::size_type i=0; i < str.length(); ++i) {
value.assign(value + std::toupper(str[i], loc));
}
return value;
}
std::vector<std::string> String::split(std::string str, char delimiter) {
std::vector<std::string> internal;
std::stringstream ss(str); // Turn the string into a stream.
std::string tok;
while (getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
void String::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);
}
}
} // namespace utils
} // namespace modsecurity

47
src/utils/msc_string.h Normal file
View File

@ -0,0 +1,47 @@
/*
* 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 <ctime>
#include <iostream>
#include <string>
#include <vector>
#ifndef SRC_UTILS_MSC_STRING_H_
#define SRC_UTILS_MSC_STRING_H_
namespace modsecurity {
namespace utils {
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<std::string> split(std::string str, char delimiter);
static void chomp(std::string *str);
};
} // namespace utils
} // namespace modsecurity
#endif // SRC_UTILS_MSC_STRING_H_

View File

@ -45,6 +45,9 @@
#include "src/actions/log_data.h"
#include "src/actions/msg.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity {
namespace Variables {
@ -53,7 +56,7 @@ void Rule::evaluateInternal(Transaction *t,
modsecurity::Rule *rule,
std::vector<const collection::Variable *> *l) {
std::map<std::string, std::string> envs;
std::string m_name_upper = toupper(m_name);
std::string m_name_upper = String::toupper(m_name);
// id
envs.insert(std::pair<std::string, std::string>("RULE:id",
@ -95,7 +98,7 @@ void Rule::evaluateInternal(Transaction *t,
}
for (auto& x : envs) {
std::string xup = toupper(x.first);
std::string xup = String::toupper(x.first);
if ((xup.substr(0, m_name_upper.size() + 1)
.compare(m_name_upper + ":") != 0)
&& (xup != m_name_upper)) {

View File

@ -23,7 +23,9 @@
#include "modsecurity/transaction.h"
#include "variations/exclusion.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
using modsecurity::Variables::Variations::Exclusion;
namespace modsecurity {
@ -36,7 +38,8 @@ Variable::Variable(std::string name)
m_isExclusion(false),
m_isCount(false) {
if (m_name.find(":") != std::string::npos) {
std::string col = toupper(std::string(m_name, 0, m_name.find(":")));
std::string col = String::toupper(
std::string(m_name, 0, m_name.find(":")));
std::string name = std::string(m_name, m_name.find(":") + 1,
m_name.size());
if (col == "TX" || col == "IP" || col == "GLOBAL"
@ -52,19 +55,19 @@ Variable::Variable(std::string name)
m_type = MultipleMatches;
}
if (tolower(m_name) == "tx") {
if (String::tolower(m_name) == "tx") {
m_collectionName = "TX";
m_type = MultipleMatches;
} else if (tolower(m_name) == "ip") {
} else if (String::tolower(m_name) == "ip") {
m_collectionName = "IP";
m_type = MultipleMatches;
} else if (tolower(m_name) == "global") {
} else if (String::tolower(m_name) == "global") {
m_collectionName = "GLOBAL";
m_type = MultipleMatches;
} else if (tolower(m_name) == "resource") {
} else if (String::tolower(m_name) == "resource") {
m_collectionName = "RESOURCE";
m_type = MultipleMatches;
} else if (tolower(m_name) == "session") {
} else if (String::tolower(m_name) == "session") {
m_collectionName = "SESSION";
m_type = MultipleMatches;
} else if (m_name.find(".") != std::string::npos) {
@ -83,7 +86,8 @@ Variable::Variable(std::string name, VariableKind kind)
m_isExclusion(false),
m_isCount(false) {
if (m_name.find(":") != std::string::npos) {
std::string col = toupper(std::string(m_name, 0, m_name.find(":")));
std::string col = String::toupper(
std::string(m_name, 0, m_name.find(":")));
std::string name = std::string(m_name, m_name.find(":") + 1,
m_name.size());
if (col == "TX" || col == "IP" || col == "GLOBAL"
@ -99,19 +103,19 @@ Variable::Variable(std::string name, VariableKind kind)
m_type = MultipleMatches;
}
if (tolower(m_name) == "tx") {
if (String::tolower(m_name) == "tx") {
m_collectionName = "TX";
m_type = MultipleMatches;
} else if (tolower(m_name) == "ip") {
} else if (String::tolower(m_name) == "ip") {
m_collectionName = "IP";
m_type = MultipleMatches;
} else if (tolower(m_name) == "global") {
} else if (String::tolower(m_name) == "global") {
m_collectionName = "GLOBAL";
m_type = MultipleMatches;
} else if (tolower(m_name) == "resource") {
} else if (String::tolower(m_name) == "resource") {
m_collectionName = "RESOURCE";
m_type = MultipleMatches;
} else if (tolower(m_name) == "session") {
} else if (String::tolower(m_name) == "session") {
m_collectionName = "SESSION";
m_type = MultipleMatches;
} else if (m_name.find(".") != std::string::npos) {

View File

@ -29,7 +29,9 @@
#include "common/colors.h"
#include "unit/unit_test.h"
#include "src/utils.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
using modsecurity_test::UnitTest;
using modsecurity_test::ModSecurityTest;
using modsecurity_test::ModSecurityTestResults;
@ -89,7 +91,7 @@ void perform_unit_test(ModSecurityTest<UnitTest> *test, UnitTest *t,
if (test->m_automake_output) {
std::cout << t->name << " "
<< modsecurity::toHexIfNeeded(t->input) << std::endl;
<< String::toHexIfNeeded(t->input) << std::endl;
}
}

View File

@ -25,6 +25,9 @@
#include "common/colors.h"
#include "src/utils.h"
#include "src/utils/regex.h"
#include "utils/msc_string.h"
using modsecurity::utils::String;
namespace modsecurity_test {
@ -108,9 +111,9 @@ std::string UnitTest::print() {
i << this->obtained << "\"" << std::endl;
}
if (this->output != this->obtainedOutput) {
i << "Expecting: \"" << modsecurity::toHexIfNeeded(this->output);
i << "Expecting: \"" << String::toHexIfNeeded(this->output);
i << "\" - returned: \"";
i << modsecurity::toHexIfNeeded(this->obtainedOutput) << "\"";
i << String::toHexIfNeeded(this->obtainedOutput) << "\"";
i << std::endl;
}