Skip to content

Implemented removeWhitespace transformation - Issue #972 #1115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/actions/transformations/remove_whitespace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "modsecurity/transaction.h"
#include "actions/transformations/transformation.h"

#define NBSP 160 // non breaking space char

namespace modsecurity {
namespace actions {
Expand All @@ -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

2 changes: 1 addition & 1 deletion src/actions/transformations/transformation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down