Perform JsDecode transformation in-place

- Removed inplace helper function from the class, as it's only
  referenced by the implementation.
This commit is contained in:
Eduardo Arias 2024-08-19 08:32:27 -07:00
parent a520369da0
commit 7d5c9faa43
2 changed files with 17 additions and 36 deletions

View File

@ -21,35 +21,13 @@
namespace modsecurity::actions::transformations { namespace modsecurity::actions::transformations {
bool JsDecode::transform(std::string &value, const Transaction *trans) const { static inline int inplace(std::string &value) {
std::string ret; auto d = reinterpret_cast<unsigned char*>(value.data());
unsigned char *input; const unsigned char *input = d;
const auto input_len = value.length();
input = reinterpret_cast<unsigned char *> bool changed = false;
(malloc(sizeof(char) * value.length()+1)); std::string::size_type i = 0;
if (input == NULL) {
return "";
}
memcpy(input, value.c_str(), value.length()+1);
size_t i = inplace(input, value.length());
ret.assign(reinterpret_cast<char *>(input), i);
free(input);
const auto changed = ret != value;
value = ret;
return changed;
}
int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
unsigned char *d = (unsigned char *)input;
int64_t i, count;
i = count = 0;
while (i < input_len) { while (i < input_len) {
if (input[i] == '\\') { if (input[i] == '\\') {
/* Character is an escape. */ /* Character is an escape. */
@ -70,14 +48,14 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
} }
d++; d++;
count++;
i += 6; i += 6;
changed = true;
} else if ((i + 3 < input_len) && (input[i + 1] == 'x') } else if ((i + 3 < input_len) && (input[i + 1] == 'x')
&& VALID_HEX(input[i + 2]) && VALID_HEX(input[i + 3])) { && VALID_HEX(input[i + 2]) && VALID_HEX(input[i + 3])) {
/* \xHH */ /* \xHH */
*d++ = utils::string::x2c(&input[i + 2]); *d++ = utils::string::x2c(&input[i + 2]);
count++;
i += 4; i += 4;
changed = true;
} else if ((i + 1 < input_len) && ISODIGIT(input[i + 1])) { } else if ((i + 1 < input_len) && ISODIGIT(input[i + 1])) {
/* \OOO (only one byte, \000 - \377) */ /* \OOO (only one byte, \000 - \377) */
char buf[4]; char buf[4];
@ -98,7 +76,7 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
} }
*d++ = (unsigned char)strtol(buf, NULL, 8); *d++ = (unsigned char)strtol(buf, NULL, 8);
i += 1 + j; i += 1 + j;
count++; changed = true;
} }
} else if (i + 1 < input_len) { } else if (i + 1 < input_len) {
/* \C */ /* \C */
@ -132,23 +110,27 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
*d++ = c; *d++ = c;
i += 2; i += 2;
count++; changed = true;
} else { } else {
/* Not enough bytes */ /* Not enough bytes */
while (i < input_len) { while (i < input_len) {
*d++ = input[i++]; *d++ = input[i++];
count++;
} }
} }
} else { } else {
*d++ = input[i++]; *d++ = input[i++];
count++;
} }
} }
*d = '\0'; *d = '\0';
return count; value.resize(d - input);
return changed;
}
bool JsDecode::transform(std::string &value, const Transaction *trans) const {
return inplace(value);
} }

View File

@ -26,7 +26,6 @@ class JsDecode : public Transformation {
: Transformation(action) { } : Transformation(action) { }
bool transform(std::string &value, const Transaction *trans) const override; bool transform(std::string &value, const Transaction *trans) const override;
static int inplace(unsigned char *input, uint64_t input_len);
}; };
} // namespace modsecurity::actions::transformations } // namespace modsecurity::actions::transformations