mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-30 11:44:32 +03:00
Updated Transformation::evaluate signature to allow for in-place updates, removing unnecessary heap allocated copies.
- Renamed Transformation::evaluate to Transformation::transform to avoid confusion with Action's overload methods. - Updated Transformation::transform signature to receive the value by reference and perform the transformation inline, if possible. - Some transformations still need to use a temporary std::string to perform their work, and then copy the result back. - Made Transformation::transform methods const and updated Transaction parameter to be const. - Transaction parameter could not be removed because it's used by just a single transformation, UrlDecodeUni. - Removed std::string Action::evaluate(const std::string &exp, Transaction *transaction); which was only implemented by Transformation but was not used from the base class, but only after downcasting to Transformation, so it can just be declared there (and not pollute other actions with a default member implementation -that does nothing- which is never called).
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include <list>
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "modsecurity/rules_set.h"
|
||||
#include "src/operators/operator.h"
|
||||
@@ -323,49 +324,42 @@ void RuleWithActions::executeAction(Transaction *trans,
|
||||
|
||||
|
||||
inline void RuleWithActions::executeTransformation(
|
||||
actions::transformations::Transformation *a,
|
||||
std::shared_ptr<std::string> *value,
|
||||
Transaction *trans,
|
||||
const actions::transformations::Transformation &a,
|
||||
std::string &value,
|
||||
const Transaction *trans,
|
||||
TransformationResults *ret,
|
||||
std::string *path,
|
||||
int *nth) const {
|
||||
|
||||
std::string *oldValue = (*value).get();
|
||||
std::string newValue = a->evaluate(*oldValue, trans);
|
||||
|
||||
if (newValue != *oldValue) {
|
||||
auto u = std::make_shared<std::string>(newValue);
|
||||
if (m_containsMultiMatchAction) {
|
||||
ret->push_back(std::make_pair(u, a->m_name));
|
||||
(*nth)++;
|
||||
}
|
||||
*value = u;
|
||||
if (a.transform(value, trans) &&
|
||||
m_containsMultiMatchAction) {
|
||||
ret.emplace_back(value, a.m_name);
|
||||
(*nth)++;
|
||||
}
|
||||
|
||||
if (path->empty()) {
|
||||
path->append(*a->m_name.get());
|
||||
path->append(*a.m_name.get());
|
||||
} else {
|
||||
path->append("," + *a->m_name.get());
|
||||
path->append("," + *a.m_name.get());
|
||||
}
|
||||
|
||||
ms_dbg_a(trans, 9, " T (" + \
|
||||
std::to_string(*nth) + ") " + \
|
||||
*a->m_name.get() + ": \"" + \
|
||||
utils::string::limitTo(80, newValue) +"\"");
|
||||
*a.m_name.get() + ": \"" + \
|
||||
utils::string::limitTo(80, value) +"\"");
|
||||
}
|
||||
|
||||
void RuleWithActions::executeTransformations(
|
||||
Transaction *trans, const std::string &in, TransformationResults &ret) {
|
||||
const Transaction *trans, const std::string &in, TransformationResults &ret) {
|
||||
int none = 0;
|
||||
int transformations = 0;
|
||||
std::string path("");
|
||||
auto value = std::make_shared<std::string>(in);
|
||||
auto value = in;
|
||||
|
||||
if (m_containsMultiMatchAction == true) {
|
||||
/* keep the original value */
|
||||
ret.push_back(std::make_pair(
|
||||
std::make_shared<std::string>(*value),
|
||||
std::make_shared<std::string>(path)));
|
||||
ret.emplace_back(value,
|
||||
std::make_shared<std::string>(path));
|
||||
}
|
||||
|
||||
for (Action *a : m_transformations) {
|
||||
@@ -385,15 +379,17 @@ void RuleWithActions::executeTransformations(
|
||||
}
|
||||
|
||||
// FIXME: here the object needs to be a transformation already.
|
||||
Transformation *t = dynamic_cast<Transformation *>(a.get());
|
||||
executeTransformation(t, &value, trans, &ret, &path,
|
||||
auto t = dynamic_cast<const Transformation*>(a.get());
|
||||
assert(t != nullptr);
|
||||
executeTransformation(*t, value, trans, &ret, &path,
|
||||
&transformations);
|
||||
}
|
||||
}
|
||||
|
||||
for (Transformation *a : m_transformations) {
|
||||
for (const Transformation *a : m_transformations) {
|
||||
assert(a != nullptr);
|
||||
if (none == 0) {
|
||||
executeTransformation(a, &value, trans, &ret, &path,
|
||||
executeTransformation(*a, value, trans, &ret, &path,
|
||||
&transformations);
|
||||
}
|
||||
if (a->m_isNone) {
|
||||
@@ -408,7 +404,8 @@ void RuleWithActions::executeTransformations(
|
||||
if (m_ruleId != b.first) {
|
||||
continue;
|
||||
}
|
||||
Transformation *a = dynamic_cast<Transformation*>(b.second.get());
|
||||
auto a = dynamic_cast<const Transformation*>(b.second.get());
|
||||
assert(a != nullptr);
|
||||
if (a->m_isNone) {
|
||||
none++;
|
||||
}
|
||||
@@ -419,9 +416,10 @@ void RuleWithActions::executeTransformations(
|
||||
if (m_ruleId != b.first) {
|
||||
continue;
|
||||
}
|
||||
Transformation *a = dynamic_cast<Transformation*>(b.second.get());
|
||||
auto a = dynamic_cast<const Transformation*>(b.second.get());
|
||||
assert(a != nullptr);
|
||||
if (none == 0) {
|
||||
executeTransformation(a, &value, trans, &ret, &path,
|
||||
executeTransformation(*a, value, trans, &ret, &path,
|
||||
&transformations);
|
||||
}
|
||||
if (a->m_isNone) {
|
||||
@@ -436,9 +434,8 @@ void RuleWithActions::executeTransformations(
|
||||
}
|
||||
|
||||
if (!m_containsMultiMatchAction) {
|
||||
ret.push_back(std::make_pair(
|
||||
std::make_shared<std::string>(*value),
|
||||
std::make_shared<std::string>(path)));
|
||||
ret.emplace_back(value,
|
||||
std::make_shared<std::string>(path));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user