Skip to content

Remove unnecessary heap allocated copies in Transformation actions #3231

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5d39890
Updated Transformation::evaluate signature to allow for in-place upda…
eduar-hte May 14, 2024
3ff72fb
Perform ParityEven7bit, ParityOdd7bit & ParityZero7bit transformation…
eduar-hte Aug 19, 2024
13203ae
Perform CmdLine transformation in-place
eduar-hte Aug 19, 2024
1236d9a
Perform CompressWhitespace transformation in-place
eduar-hte Aug 19, 2024
1505025
Perform RemoveNulls & RemoveWhitespace transformations in-place
eduar-hte Aug 19, 2024
da775ec
Perform ReplaceNulls transformation in-place
eduar-hte Aug 19, 2024
74d150c
Perform RemoveCommentsChar, RemoveComments & ReplaceComments transfor…
eduar-hte Aug 19, 2024
2915ee6
Perform Trim, TrimLeft & TrimRight transformations in-place
eduar-hte Aug 19, 2024
fd8a979
Perform SqlHexDecode transformation in-place
eduar-hte Aug 19, 2024
4670710
Perform LowerCase & UpperCase transformations in-place
eduar-hte Aug 19, 2024
e687140
Perform HexDecode transformation in-place
eduar-hte Aug 19, 2024
727f2bf
Perform CssDecode transformation in-place
eduar-hte Aug 19, 2024
a520369
Perform EscapeSeqDecode transformation in-place
eduar-hte Aug 19, 2024
7d5c9fa
Perform JsDecode transformation in-place
eduar-hte Aug 19, 2024
8bf4d96
Perform HtmlEntityDecode transformation in-place
eduar-hte Aug 19, 2024
17a2cbd
Perform UrlDecodeUni & UrlDecode transformations in-place
eduar-hte Aug 19, 2024
2c3c228
Perform Utf8ToUnicode transformation in-place
eduar-hte Aug 19, 2024
021d0ca
Perform NormalisePath & NormalisePathWin transformations in-place
eduar-hte Aug 19, 2024
b647dbd
Remove unnecessary heap-allocation & copy in Transaction::extractArgu…
eduar-hte Jun 1, 2024
34da8ee
Pass RuleWithActions::executeTransformation arguments by reference
eduar-hte Aug 9, 2024
fedec96
Refactored base64 utils to share implementation and reduce code dupli…
eduar-hte Aug 19, 2024
7023c0a
Refactored sha1 & md5 utils to share implementation and reduce code d…
eduar-hte Aug 19, 2024
2f5dac5
Simplified initialization of Transformation's action_kind
eduar-hte Aug 19, 2024
a6d64bf
Replaced VALID_HEX, ISODIGIT & NBSP macros in string.h
eduar-hte Aug 17, 2024
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
93 changes: 42 additions & 51 deletions headers/modsecurity/actions/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,66 @@
*
*/

#ifdef __cplusplus

#include <string>
#include <iostream>
#include <memory>

#endif

#include "modsecurity/intervention.h"
#include "modsecurity/rule.h"
#include "modsecurity/rule_with_actions.h"

#ifndef HEADERS_MODSECURITY_ACTIONS_ACTION_H_
#define HEADERS_MODSECURITY_ACTIONS_ACTION_H_

#ifdef __cplusplus

#include <string>
#include <memory>

namespace modsecurity {
class Transaction;
class RuleWithOperator;
class RuleWithActions;
class RuleMessage;

namespace actions {


class Action {
public:
/**
*
* Define the action kind regarding to the execution time.
*
*
*/
enum class Kind {
/**
*
* Action that are executed while loading the configuration. For instance
* the rule ID or the rule phase.
*
*/
ConfigurationKind,
/**
*
* Those are actions that demands to be executed before call the operator.
* For instance the tranformations.
*
*
*/
RunTimeBeforeMatchAttemptKind,
/**
*
* Actions that are executed after the execution of the operator, only if
* the operator returned Match (or True). For instance the disruptive
* actions.
*
*/
RunTimeOnlyIfMatchKind,
};

explicit Action(const std::string& _action)
: m_isNone(false),
temporaryAction(false),
action_kind(2),
action_kind(Kind::RunTimeOnlyIfMatchKind),
m_name(nullptr),
m_parser_payload("") {
set_name_and_payload(_action);
}
explicit Action(const std::string& _action, int kind)
explicit Action(const std::string& _action, Kind kind)
: m_isNone(false),
temporaryAction(false),
action_kind(kind),
Expand All @@ -74,8 +99,6 @@ class Action {

virtual ~Action() { }

virtual std::string evaluate(const std::string &exp,
Transaction *transaction);
virtual bool evaluate(RuleWithActions *rule, Transaction *transaction);
virtual bool evaluate(RuleWithActions *rule, Transaction *transaction,
std::shared_ptr<RuleMessage> ruleMessage) {
Expand All @@ -87,9 +110,9 @@ class Action {

void set_name_and_payload(const std::string& data) {
size_t pos = data.find(":");
std::string t = "t:";
const char t[] = "t:";

if (data.compare(0, t.length(), t) == 0) {
if (data.compare(0, std::size(t) - 1, t) == 0) {
pos = data.find(":", 2);
}

Expand All @@ -109,41 +132,9 @@ class Action {

bool m_isNone;
bool temporaryAction;
int action_kind;
Kind action_kind;
std::shared_ptr<std::string> m_name;
std::string m_parser_payload;

/**
*
* Define the action kind regarding to the execution time.
*
*
*/
enum Kind {
/**
*
* Action that are executed while loading the configuration. For instance
* the rule ID or the rule phase.
*
*/
ConfigurationKind,
/**
*
* Those are actions that demands to be executed before call the operator.
* For instance the tranformations.
*
*
*/
RunTimeBeforeMatchAttemptKind,
/**
*
* Actions that are executed after the execution of the operator, only if
* the operator returned Match (or True). For instance the disruptive
* actions.
*
*/
RunTimeOnlyIfMatchKind,
};
};


Expand Down
2 changes: 1 addition & 1 deletion headers/modsecurity/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace operators {
class Operator;
}

using TransformationResult = std::pair<std::shared_ptr<std::string>,
using TransformationResult = std::pair<std::string,
std::shared_ptr<std::string>>;
using TransformationResults = std::list<TransformationResult>;

Expand Down
19 changes: 9 additions & 10 deletions headers/modsecurity/rule_with_actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,7 @@ class RuleWithActions : public Rule {


void executeTransformations(
Transaction *trasn, const std::string &value, TransformationResults &ret);

inline void executeTransformation(
actions::transformations::Transformation *a,
std::shared_ptr<std::string> *value,
Transaction *trans,
TransformationResults *ret,
std::string *path,
int *nth) const;

const Transaction *trasn, const std::string &value, TransformationResults &ret);

void performLogging(Transaction *trans,
std::shared_ptr<RuleMessage> ruleMessage,
Expand Down Expand Up @@ -166,6 +157,14 @@ class RuleWithActions : public Rule {
RuleWithActions *m_chainedRuleParent;

private:
inline void executeTransformation(
const actions::transformations::Transformation &a,
std::string &value,
const Transaction *trans,
TransformationResults &ret,
std::string &path,
int &nth) const;

/* actions */
actions::Action *m_disruptiveAction;
actions::LogData *m_logData;
Expand Down
2 changes: 0 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,9 @@ UTILS = \
utils/geo_lookup.cc \
utils/https_client.cc \
utils/ip_tree.cc \
utils/md5.cc \
utils/msc_tree.cc \
utils/random.cc \
utils/regex.cc \
utils/sha1.cc \
utils/system.cc \
utils/shared_files.cc

Expand Down
13 changes: 3 additions & 10 deletions src/actions/accuracy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@

#include "src/actions/accuracy.h"

#include <iostream>
#include <string>
#include "modsecurity/rule_with_actions.h"

#include "modsecurity/actions/action.h"
#include "modsecurity/transaction.h"
#include "modsecurity/rule.h"


namespace modsecurity {
namespace actions {
namespace modsecurity::actions {


bool Accuracy::init(std::string *error) {
Expand All @@ -45,5 +39,4 @@ bool Accuracy::evaluate(RuleWithActions *rule, Transaction *transaction) {
}


} // namespace actions
} // namespace modsecurity
} // namespace modsecurity::actions
2 changes: 1 addition & 1 deletion src/actions/accuracy.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace actions {
class Accuracy : public Action {
public:
explicit Accuracy(const std::string &action)
: Action(action, ConfigurationKind),
: Action(action, Kind::ConfigurationKind),
m_accuracy(0) { }

bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
Expand Down
6 changes: 0 additions & 6 deletions src/actions/action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ namespace modsecurity {
namespace actions {


std::string Action::evaluate(const std::string &value,
Transaction *transaction) {
return value;
}


bool Action::evaluate(RuleWithActions *rule, Transaction *transaction) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/audit_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace actions {
class AuditLog : public Action {
public:
explicit AuditLog(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind) { }
: Action(action) { }

bool evaluate(RuleWithActions *rule, Transaction *transaction,
std::shared_ptr<RuleMessage> rm) override;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace actions {
class Capture : public Action {
public:
explicit Capture(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind) { }
: Action(action) { }

bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
};
Expand Down
12 changes: 3 additions & 9 deletions src/actions/chain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@

#include "src/actions/chain.h"

#include <iostream>
#include <string>
#include "modsecurity/rule_with_actions.h"

#include "modsecurity/transaction.h"
#include "modsecurity/rule.h"

namespace modsecurity {
namespace actions {
namespace modsecurity::actions {


bool Chain::evaluate(RuleWithActions *rule, Transaction *transaction) {
Expand All @@ -31,5 +26,4 @@ bool Chain::evaluate(RuleWithActions *rule, Transaction *transaction) {
}


} // namespace actions
} // namespace modsecurity
} // namespace modsecurity::actions
2 changes: 1 addition & 1 deletion src/actions/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace actions {
class Chain : public Action {
public:
explicit Chain(const std::string &action)
: Action(action, ConfigurationKind) { }
: Action(action, Kind::ConfigurationKind) { }

bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/audit_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ctl {
class AuditEngine : public Action {
public:
explicit AuditEngine(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind),
: Action(action),
m_auditEngine(audit_log::AuditLog::AuditLogStatus::NotSetLogStatus) { }

bool init(std::string *error) override;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/audit_log_parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ctl {
class AuditLogParts : public Action {
public:
explicit AuditLogParts(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind),
: Action(action),
mPartsAction(0),
mParts("") { }

Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/request_body_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ctl {
class RequestBodyAccess : public Action {
public:
explicit RequestBodyAccess(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind),
: Action(action),
m_request_body_access(false) { }

bool init(std::string *error) override;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/request_body_processor_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ctl {
class RequestBodyProcessorJSON : public Action {
public:
explicit RequestBodyProcessorJSON(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind) { }
: Action(action) { }

bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/request_body_processor_urlencoded.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ctl {
class RequestBodyProcessorURLENCODED : public Action {
public:
explicit RequestBodyProcessorURLENCODED(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind) { }
: Action(action) { }

bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/request_body_processor_xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ctl {
class RequestBodyProcessorXML : public Action {
public:
explicit RequestBodyProcessorXML(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind) { }
: Action(action) { }

bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/rule_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace ctl {
class RuleEngine : public Action {
public:
explicit RuleEngine(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind),
: Action(action),
m_ruleEngine(RulesSetProperties::PropertyNotSetRuleEngine) { }

bool init(std::string *error) override;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/rule_remove_by_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ctl {
class RuleRemoveById : public Action {
public:
explicit RuleRemoveById(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind) { }
: Action(action) { }

bool init(std::string *error) override;
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/rule_remove_by_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ctl {
class RuleRemoveByTag : public Action {
public:
explicit RuleRemoveByTag(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind),
: Action(action),
m_tag("") { }

bool init(std::string *error) override;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/rule_remove_target_by_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ctl {
class RuleRemoveTargetById : public Action {
public:
explicit RuleRemoveTargetById(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind),
: Action(action),
m_id(0),
m_target("") { }

Expand Down
2 changes: 1 addition & 1 deletion src/actions/ctl/rule_remove_target_by_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ctl {
class RuleRemoveTargetByTag : public Action {
public:
explicit RuleRemoveTargetByTag(const std::string &action)
: Action(action, RunTimeOnlyIfMatchKind) { }
: Action(action) { }

bool init(std::string *error) override;
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
Expand Down
Loading
Loading