Skip to content

Commit 5d39890

Browse files
committed
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).
1 parent 97c8766 commit 5d39890

File tree

96 files changed

+644
-1643
lines changed

Some content is hidden

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

96 files changed

+644
-1643
lines changed

headers/modsecurity/actions/action.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,19 @@
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

@@ -74,8 +67,6 @@ class Action {
7467

7568
virtual ~Action() { }
7669

77-
virtual std::string evaluate(const std::string &exp,
78-
Transaction *transaction);
7970
virtual bool evaluate(RuleWithActions *rule, Transaction *transaction);
8071
virtual bool evaluate(RuleWithActions *rule, Transaction *transaction,
8172
std::shared_ptr<RuleMessage> ruleMessage) {
@@ -87,9 +78,9 @@ class Action {
8778

8879
void set_name_and_payload(const std::string& data) {
8980
size_t pos = data.find(":");
90-
std::string t = "t:";
81+
const char t[] = "t:";
9182

92-
if (data.compare(0, t.length(), t) == 0) {
83+
if (data.compare(0, std::size(t) - 1, t) == 0) {
9384
pos = data.find(":", 2);
9485
}
9586

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/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/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/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/maturity.cc

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

1616
#include "src/actions/maturity.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 Maturity::init(std::string *error) {
@@ -45,5 +39,4 @@ bool Maturity::evaluate(RuleWithActions *rule, Transaction *transaction) {
4539
}
4640

4741

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

src/actions/phase.cc

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515

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

18-
#include <iostream>
19-
#include <string>
20-
21-
#include "modsecurity/transaction.h"
22-
#include "modsecurity/rule.h"
23-
#include "modsecurity/modsecurity.h"
18+
#include "modsecurity/rule_with_actions.h"
2419
#include "src/utils/string.h"
2520

2621

27-
namespace modsecurity {
28-
namespace actions {
22+
namespace modsecurity::actions {
23+
2924

3025
bool Phase::init(std::string *error) {
31-
std::string a = utils::string::tolower(m_parser_payload);
26+
const auto a = utils::string::tolower(m_parser_payload);
3227
m_phase = -1;
3328

3429
try {
@@ -77,5 +72,5 @@ bool Phase::evaluate(RuleWithActions *rule, Transaction *transaction) {
7772
return true;
7873
}
7974

80-
} // namespace actions
81-
} // namespace modsecurity
75+
76+
} // namespace modsecurity::actions

src/actions/rev.cc

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

1616
#include "src/actions/rev.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 Rev::init(std::string *error) {
@@ -39,5 +33,4 @@ bool Rev::evaluate(RuleWithActions *rule, Transaction *transaction) {
3933
}
4034

4135

42-
} // namespace actions
43-
} // namespace modsecurity
36+
} // namespace modsecurity::actions

src/actions/rule_id.cc

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

1616
#include "src/actions/rule_id.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"
2320

24-
namespace modsecurity {
25-
namespace actions {
21+
namespace modsecurity::actions {
2622

2723

2824
bool RuleId::init(std::string *error) {
@@ -54,5 +50,4 @@ bool RuleId::evaluate(RuleWithActions *rule, Transaction *transaction) {
5450
}
5551

5652

57-
} // namespace actions
58-
} // namespace modsecurity
53+
} // namespace modsecurity::actions

0 commit comments

Comments
 (0)