diff --git a/src/actions/transformations/replace_comments.cc b/src/actions/transformations/replace_comments.cc index 79036961..9303984d 100644 --- a/src/actions/transformations/replace_comments.cc +++ b/src/actions/transformations/replace_comments.cc @@ -24,6 +24,7 @@ #include "modsecurity/assay.h" #include "actions/transformations/transformation.h" +#include "src/utils.h" namespace ModSecurity { @@ -37,16 +38,51 @@ ReplaceComments::ReplaceComments(std::string action) std::string ReplaceComments::evaluate(std::string value, Assay *assay) { - /** - * @todo Implement the transformation ReplaceComments - */ - if (assay) { -#ifndef NO_LOGS - assay->debug(4, "Transformation ReplaceComments " \ - "is not implemented yet."); -#endif + + long int i, j, incomment; + int changed = 0; + + char *input = (char *) malloc(sizeof(char) * value.size() + 1); + memcpy(input, value.c_str(), value.size() + 1); + input[value.size()] = '\0'; + + i = j = incomment = 0; + while(i < value.size()) { + if (incomment == 0) { + if ((input[i] == '/') && (i + 1 < value.size()) + && (input[i + 1] == '*')) { + changed = 1; + incomment = 1; + i += 2; + } else { + input[j] = input[i]; + i++; + j++; + } + } else { + if ((input[i] == '*') && (i + 1 < value.size()) + && (input[i + 1] == '/')) { + incomment = 0; + i += 2; + input[j] = ' '; + j++; + } else { + i++; + } + } } - return value; + + if (incomment) { + input[j++] = ' '; + } + + + std::string resp; + resp.append((char *)input, j); + + free(input); + + return resp; } } // namespace transformations diff --git a/src/actions/transformations/transformation.cc b/src/actions/transformations/transformation.cc index c5a32267..1dfc8177 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(removeNulls) { return new RemoveNulls(a); } IF_MATCH(remove_whitespace) { return new RemoveWhitespace(a); } IF_MATCH(compressWhitespace) { return new CompressWhitespace(a); } - IF_MATCH(replace_comments) { return new ReplaceComments(a); } + IF_MATCH(replaceComments) { return new ReplaceComments(a); } IF_MATCH(replace_nulls) { return new ReplaceNulls(a); } IF_MATCH(sha1) { return new Sha1(a); } IF_MATCH(sql_hex_decode) { return new SqlHexDecode(a); } diff --git a/test/test-cases/secrules-language-tests b/test/test-cases/secrules-language-tests index 26af9651..444dfbea 160000 --- a/test/test-cases/secrules-language-tests +++ b/test/test-cases/secrules-language-tests @@ -1 +1 @@ -Subproject commit 26af965147db5aae11c56bebea9649f4ea7cb1d4 +Subproject commit 444dfbea3390cf54f6d59e331bc4fbcabb77eb4e