Skip to content

Commit 9148668

Browse files
authored
Merge pull request #3231 from eduar-hte/remove-copies-transformations
Remove unnecessary heap allocated copies in Transformation actions
2 parents 4951702 + a6d64bf commit 9148668

File tree

146 files changed

+1178
-2809
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+1178
-2809
lines changed

headers/modsecurity/actions/action.h

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,66 @@
1313
*
1414
*/
1515

16-
#ifdef __cplusplus
17-
18-
#include <string>
19-
#include <iostream>
20-
#include <memory>
21-
22-
#endif
23-
24-
#include "modsecurity/intervention.h"
25-
#include "modsecurity/rule.h"
26-
#include "modsecurity/rule_with_actions.h"
27-
2816
#ifndef HEADERS_MODSECURITY_ACTIONS_ACTION_H_
2917
#define HEADERS_MODSECURITY_ACTIONS_ACTION_H_
3018

3119
#ifdef __cplusplus
3220

21+
#include <string>
22+
#include <memory>
23+
3324
namespace modsecurity {
3425
class Transaction;
3526
class RuleWithOperator;
27+
class RuleWithActions;
28+
class RuleMessage;
3629

3730
namespace actions {
3831

3932

4033
class Action {
4134
public:
35+
/**
36+
*
37+
* Define the action kind regarding to the execution time.
38+
*
39+
*
40+
*/
41+
enum class Kind {
42+
/**
43+
*
44+
* Action that are executed while loading the configuration. For instance
45+
* the rule ID or the rule phase.
46+
*
47+
*/
48+
ConfigurationKind,
49+
/**
50+
*
51+
* Those are actions that demands to be executed before call the operator.
52+
* For instance the tranformations.
53+
*
54+
*
55+
*/
56+
RunTimeBeforeMatchAttemptKind,
57+
/**
58+
*
59+
* Actions that are executed after the execution of the operator, only if
60+
* the operator returned Match (or True). For instance the disruptive
61+
* actions.
62+
*
63+
*/
64+
RunTimeOnlyIfMatchKind,
65+
};
66+
4267
explicit Action(const std::string& _action)
4368
: m_isNone(false),
4469
temporaryAction(false),
45-
action_kind(2),
70+
action_kind(Kind::RunTimeOnlyIfMatchKind),
4671
m_name(nullptr),
4772
m_parser_payload("") {
4873
set_name_and_payload(_action);
4974
}
50-
explicit Action(const std::string& _action, int kind)
75+
explicit Action(const std::string& _action, Kind kind)
5176
: m_isNone(false),
5277
temporaryAction(false),
5378
action_kind(kind),
@@ -74,8 +99,6 @@ class Action {
7499

75100
virtual ~Action() { }
76101

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

88111
void set_name_and_payload(const std::string& data) {
89112
size_t pos = data.find(":");
90-
std::string t = "t:";
113+
const char t[] = "t:";
91114

92-
if (data.compare(0, t.length(), t) == 0) {
115+
if (data.compare(0, std::size(t) - 1, t) == 0) {
93116
pos = data.find(":", 2);
94117
}
95118

@@ -109,41 +132,9 @@ class Action {
109132

110133
bool m_isNone;
111134
bool temporaryAction;
112-
int action_kind;
135+
Kind action_kind;
113136
std::shared_ptr<std::string> m_name;
114137
std::string m_parser_payload;
115-
116-
/**
117-
*
118-
* Define the action kind regarding to the execution time.
119-
*
120-
*
121-
*/
122-
enum Kind {
123-
/**
124-
*
125-
* Action that are executed while loading the configuration. For instance
126-
* the rule ID or the rule phase.
127-
*
128-
*/
129-
ConfigurationKind,
130-
/**
131-
*
132-
* Those are actions that demands to be executed before call the operator.
133-
* For instance the tranformations.
134-
*
135-
*
136-
*/
137-
RunTimeBeforeMatchAttemptKind,
138-
/**
139-
*
140-
* Actions that are executed after the execution of the operator, only if
141-
* the operator returned Match (or True). For instance the disruptive
142-
* actions.
143-
*
144-
*/
145-
RunTimeOnlyIfMatchKind,
146-
};
147138
};
148139

149140

headers/modsecurity/rule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace operators {
5252
class Operator;
5353
}
5454

55-
using TransformationResult = std::pair<std::shared_ptr<std::string>,
55+
using TransformationResult = std::pair<std::string,
5656
std::shared_ptr<std::string>>;
5757
using TransformationResults = std::list<TransformationResult>;
5858

headers/modsecurity/rule_with_actions.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,7 @@ class RuleWithActions : public Rule {
119119

120120

121121
void executeTransformations(
122-
Transaction *trasn, const std::string &value, TransformationResults &ret);
123-
124-
inline void executeTransformation(
125-
actions::transformations::Transformation *a,
126-
std::shared_ptr<std::string> *value,
127-
Transaction *trans,
128-
TransformationResults *ret,
129-
std::string *path,
130-
int *nth) const;
131-
122+
const Transaction *trasn, const std::string &value, TransformationResults &ret);
132123

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

168159
private:
160+
inline void executeTransformation(
161+
const actions::transformations::Transformation &a,
162+
std::string &value,
163+
const Transaction *trans,
164+
TransformationResults &ret,
165+
std::string &path,
166+
int &nth) const;
167+
169168
/* actions */
170169
actions::Action *m_disruptiveAction;
171170
actions::LogData *m_logData;

src/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,9 @@ UTILS = \
247247
utils/geo_lookup.cc \
248248
utils/https_client.cc \
249249
utils/ip_tree.cc \
250-
utils/md5.cc \
251250
utils/msc_tree.cc \
252251
utils/random.cc \
253252
utils/regex.cc \
254-
utils/sha1.cc \
255253
utils/system.cc \
256254
utils/shared_files.cc
257255

src/actions/accuracy.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@
1515

1616
#include "src/actions/accuracy.h"
1717

18-
#include <iostream>
19-
#include <string>
18+
#include "modsecurity/rule_with_actions.h"
2019

21-
#include "modsecurity/actions/action.h"
22-
#include "modsecurity/transaction.h"
23-
#include "modsecurity/rule.h"
2420

25-
26-
namespace modsecurity {
27-
namespace actions {
21+
namespace modsecurity::actions {
2822

2923

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

4741

48-
} // namespace actions
49-
} // namespace modsecurity
42+
} // namespace modsecurity::actions

src/actions/accuracy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace actions {
3030
class Accuracy : public Action {
3131
public:
3232
explicit Accuracy(const std::string &action)
33-
: Action(action, ConfigurationKind),
33+
: Action(action, Kind::ConfigurationKind),
3434
m_accuracy(0) { }
3535

3636
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;

src/actions/action.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ namespace modsecurity {
4545
namespace actions {
4646

4747

48-
std::string Action::evaluate(const std::string &value,
49-
Transaction *transaction) {
50-
return value;
51-
}
52-
53-
5448
bool Action::evaluate(RuleWithActions *rule, Transaction *transaction) {
5549
return true;
5650
}

src/actions/audit_log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace actions {
3333
class AuditLog : public Action {
3434
public:
3535
explicit AuditLog(const std::string &action)
36-
: Action(action, RunTimeOnlyIfMatchKind) { }
36+
: Action(action) { }
3737

3838
bool evaluate(RuleWithActions *rule, Transaction *transaction,
3939
std::shared_ptr<RuleMessage> rm) override;

src/actions/capture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace actions {
2929
class Capture : public Action {
3030
public:
3131
explicit Capture(const std::string &action)
32-
: Action(action, RunTimeOnlyIfMatchKind) { }
32+
: Action(action) { }
3333

3434
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
3535
};

src/actions/chain.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515

1616
#include "src/actions/chain.h"
1717

18-
#include <iostream>
19-
#include <string>
18+
#include "modsecurity/rule_with_actions.h"
2019

21-
#include "modsecurity/transaction.h"
22-
#include "modsecurity/rule.h"
23-
24-
namespace modsecurity {
25-
namespace actions {
20+
namespace modsecurity::actions {
2621

2722

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

3328

34-
} // namespace actions
35-
} // namespace modsecurity
29+
} // namespace modsecurity::actions

src/actions/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace actions {
3333
class Chain : public Action {
3434
public:
3535
explicit Chain(const std::string &action)
36-
: Action(action, ConfigurationKind) { }
36+
: Action(action, Kind::ConfigurationKind) { }
3737

3838
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
3939
};

src/actions/ctl/audit_engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace ctl {
3434
class AuditEngine : public Action {
3535
public:
3636
explicit AuditEngine(const std::string &action)
37-
: Action(action, RunTimeOnlyIfMatchKind),
37+
: Action(action),
3838
m_auditEngine(audit_log::AuditLog::AuditLogStatus::NotSetLogStatus) { }
3939

4040
bool init(std::string *error) override;

src/actions/ctl/audit_log_parts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ctl {
2929
class AuditLogParts : public Action {
3030
public:
3131
explicit AuditLogParts(const std::string &action)
32-
: Action(action, RunTimeOnlyIfMatchKind),
32+
: Action(action),
3333
mPartsAction(0),
3434
mParts("") { }
3535

src/actions/ctl/request_body_access.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ctl {
3030
class RequestBodyAccess : public Action {
3131
public:
3232
explicit RequestBodyAccess(const std::string &action)
33-
: Action(action, RunTimeOnlyIfMatchKind),
33+
: Action(action),
3434
m_request_body_access(false) { }
3535

3636
bool init(std::string *error) override;

src/actions/ctl/request_body_processor_json.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ctl {
2929
class RequestBodyProcessorJSON : public Action {
3030
public:
3131
explicit RequestBodyProcessorJSON(const std::string &action)
32-
: Action(action, RunTimeOnlyIfMatchKind) { }
32+
: Action(action) { }
3333

3434
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
3535
};

src/actions/ctl/request_body_processor_urlencoded.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ctl {
2929
class RequestBodyProcessorURLENCODED : public Action {
3030
public:
3131
explicit RequestBodyProcessorURLENCODED(const std::string &action)
32-
: Action(action, RunTimeOnlyIfMatchKind) { }
32+
: Action(action) { }
3333

3434
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
3535
};

src/actions/ctl/request_body_processor_xml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ctl {
2929
class RequestBodyProcessorXML : public Action {
3030
public:
3131
explicit RequestBodyProcessorXML(const std::string &action)
32-
: Action(action, RunTimeOnlyIfMatchKind) { }
32+
: Action(action) { }
3333

3434
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
3535
};

src/actions/ctl/rule_engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace ctl {
3131
class RuleEngine : public Action {
3232
public:
3333
explicit RuleEngine(const std::string &action)
34-
: Action(action, RunTimeOnlyIfMatchKind),
34+
: Action(action),
3535
m_ruleEngine(RulesSetProperties::PropertyNotSetRuleEngine) { }
3636

3737
bool init(std::string *error) override;

src/actions/ctl/rule_remove_by_id.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ctl {
3030
class RuleRemoveById : public Action {
3131
public:
3232
explicit RuleRemoveById(const std::string &action)
33-
: Action(action, RunTimeOnlyIfMatchKind) { }
33+
: Action(action) { }
3434

3535
bool init(std::string *error) override;
3636
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;

src/actions/ctl/rule_remove_by_tag.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ctl {
3030
class RuleRemoveByTag : public Action {
3131
public:
3232
explicit RuleRemoveByTag(const std::string &action)
33-
: Action(action, RunTimeOnlyIfMatchKind),
33+
: Action(action),
3434
m_tag("") { }
3535

3636
bool init(std::string *error) override;

src/actions/ctl/rule_remove_target_by_id.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ctl {
3030
class RuleRemoveTargetById : public Action {
3131
public:
3232
explicit RuleRemoveTargetById(const std::string &action)
33-
: Action(action, RunTimeOnlyIfMatchKind),
33+
: Action(action),
3434
m_id(0),
3535
m_target("") { }
3636

src/actions/ctl/rule_remove_target_by_tag.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ctl {
3030
class RuleRemoveTargetByTag : public Action {
3131
public:
3232
explicit RuleRemoveTargetByTag(const std::string &action)
33-
: Action(action, RunTimeOnlyIfMatchKind) { }
33+
: Action(action) { }
3434

3535
bool init(std::string *error) override;
3636
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;

0 commit comments

Comments
 (0)