From 7c75303077d456ae19b56206fcaf3319c99c8d71 Mon Sep 17 00:00:00 2001 From: bjh7242 Date: Thu, 17 Mar 2016 14:27:11 -0400 Subject: [PATCH 1/4] added remove_comments_char to address issue #971 --- .../transformations/remove_comments_char.cc | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/actions/transformations/remove_comments_char.cc b/src/actions/transformations/remove_comments_char.cc index c2f6c14662..80088a17d9 100644 --- a/src/actions/transformations/remove_comments_char.cc +++ b/src/actions/transformations/remove_comments_char.cc @@ -37,16 +37,29 @@ RemoveCommentsChar::RemoveCommentsChar(std::string action) std::string RemoveCommentsChar::evaluate(std::string value, Transaction *transaction) { - /** - * @todo Implement the transformation RemoveCommentsChar - */ - if (transaction) { -#ifndef NO_LOGS - transaction->debug(4, "Transformation RemoveCommentsChar " \ - "is not implemented yet."); -#endif + int64_t i; + + i = 0; + while (i < value.size()) { + if (value.at(i) == '/' && (i+1 < value.size()) && value.at(i+1) == '*') { + value.erase(i, 2); + } else if (value.at(i) == '*' && (i+1 < value.size()) && value.at(i+1) == '/') { + value.erase(i, 2); + } else if (value.at(i) == '<' && (i+1 < value.size()) && value.at(i+1) == '!' && + (i+2 < value.size()) && value.at(i+2) == '-' && (i+3 < value.size()) && + value.at(i+3) == '-') { + value.erase(i, 4); + } else if (value.at(i) == '-' && (i+1 < value.size()) && value.at(i+1) == '-' && + (i+2 < value.size()) && value.at(i+2) == '>') { + value.erase(i, 3); + } else if (value.at(i) == '-' && (i+1 < value.size()) && value.at(i+1) == '-') { + value.erase(i, 2); + } else if (value.at(i) == '#') { + value.erase(i, 1); + } else { + i++; + } } - return value; } } // namespace transformations From e41cd9ec91997bf9fb3ce101d368623eb79436b1 Mon Sep 17 00:00:00 2001 From: bjh7242 Date: Thu, 31 Mar 2016 21:24:54 -0400 Subject: [PATCH 2/4] implemented remove_comments_char to address #971 --- src/actions/transformations/remove_comments_char.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/actions/transformations/remove_comments_char.cc b/src/actions/transformations/remove_comments_char.cc index 80088a17d9..6bca6cd08d 100644 --- a/src/actions/transformations/remove_comments_char.cc +++ b/src/actions/transformations/remove_comments_char.cc @@ -60,6 +60,7 @@ std::string RemoveCommentsChar::evaluate(std::string value, i++; } } + return value; } } // namespace transformations From 974d79b3ebc1db324b86c911baa629f2009b21b8 Mon Sep 17 00:00:00 2001 From: bjh7242 Date: Mon, 4 Apr 2016 23:27:02 +0000 Subject: [PATCH 3/4] adding removeWhitespace transformation --- .../transformations/remove_whitespace.cc | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/actions/transformations/remove_whitespace.cc b/src/actions/transformations/remove_whitespace.cc index 4c3f267cce..d09453fc24 100644 --- a/src/actions/transformations/remove_whitespace.cc +++ b/src/actions/transformations/remove_whitespace.cc @@ -25,6 +25,7 @@ #include "modsecurity/transaction.h" #include "actions/transformations/transformation.h" +#define NBSP 160 // non breaking space char namespace modsecurity { namespace actions { @@ -37,18 +38,27 @@ RemoveWhitespace::RemoveWhitespace(std::string action) std::string RemoveWhitespace::evaluate(std::string value, Transaction *transaction) { - /** - * @todo Implement the transformation RemoveWhitespace - */ - if (transaction) { -#ifndef NO_LOGS - transaction->debug(4, "Transformation RemoveWhitespace is " \ - "not implemented yet."); -#endif + + long int i = 0; + + // loop through all the chars + while(i < value.size()) { + // remove whitespaces and non breaking spaces (NBSP) + if (isspace(value[i])||(value[i] == NBSP)) { + value.erase(i, 1); + } + else { + /* if the space is not a whitespace char, increment counter + counter should not be incremented if a character is erased because + the index erased will be replaced by the following character */ + i++; + } } + return value; } } // namespace transformations } // namespace actions } // namespace modsecurity + From c4f211132d50fe154e823bc4e6f6f0cb59d466a2 Mon Sep 17 00:00:00 2001 From: bjh7242 Date: Tue, 5 Apr 2016 00:48:59 +0000 Subject: [PATCH 4/4] updated transformation file to include removeWhitespace transformation --- src/actions/transformations/transformation.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/actions/transformations/transformation.cc b/src/actions/transformations/transformation.cc index 8f3b5d70f2..ae4cbf72ab 100644 --- a/src/actions/transformations/transformation.cc +++ b/src/actions/transformations/transformation.cc @@ -98,7 +98,7 @@ Transformation* Transformation::instantiate(std::string a) { IF_MATCH(removeCommentsChar) { return new RemoveCommentsChar(a); } IF_MATCH(remove_comments) { return new RemoveComments(a); } IF_MATCH(removeNulls) { return new RemoveNulls(a); } - IF_MATCH(remove_whitespace) { return new RemoveWhitespace(a); } + IF_MATCH(removeWhitespace) { return new RemoveWhitespace(a); } IF_MATCH(compressWhitespace) { return new CompressWhitespace(a); } IF_MATCH(replaceComments) { return new ReplaceComments(a); } IF_MATCH(replaceNulls) { return new ReplaceNulls(a); }