Skip to content

Commit 0a2dd1a

Browse files
committed
Remove unnecessary heap allocated copies when doing transformations.
- Updated transformations to work inplace on the std::string buffer of the value where the transformation is being applied, instead of copying to a temporary string. - Make inplace transformations methods inline and moved them out of the class interface as almost all of them are not used outside of them. - 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). - 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. - Simplified & shared implementation of several transformations, removing duplicate code.
1 parent 752ab76 commit 0a2dd1a

Some content is hidden

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

95 files changed

+994
-2444
lines changed

headers/modsecurity/actions/action.h

-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ class Action {
7474

7575
virtual ~Action() { }
7676

77-
virtual std::string evaluate(const std::string &exp,
78-
Transaction *transaction);
7977
virtual bool evaluate(RuleWithActions *rule, Transaction *transaction);
8078
virtual bool evaluate(RuleWithActions *rule, Transaction *transaction,
8179
std::shared_ptr<RuleMessage> ruleMessage) {

headers/modsecurity/rule.h

+1-1
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

+9-10
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

-2
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,9 @@ UTILS = \
248248
utils/geo_lookup.cc \
249249
utils/https_client.cc \
250250
utils/ip_tree.cc \
251-
utils/md5.cc \
252251
utils/msc_tree.cc \
253252
utils/random.cc \
254253
utils/regex.cc \
255-
utils/sha1.cc \
256254
utils/system.cc \
257255
utils/shared_files.cc
258256

src/actions/action.cc

-6
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/transformations/base64_decode.cc

+7-21
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,19 @@
1313
*
1414
*/
1515

16-
#include "src/actions/transformations/base64_decode.h"
16+
#include "base64_decode.h"
1717

18-
#include <iostream>
19-
#include <string>
20-
#include <algorithm>
21-
#include <functional>
22-
#include <cctype>
23-
#include <locale>
24-
25-
#include "modsecurity/transaction.h"
26-
#include "src/actions/transformations/transformation.h"
2718
#include "src/utils/base64.h"
2819

2920

30-
namespace modsecurity {
31-
namespace actions {
32-
namespace transformations {
33-
21+
namespace modsecurity::actions::transformations {
3422

35-
std::string Base64Decode::evaluate(const std::string &value,
36-
Transaction *transaction) {
37-
std::string ret = Utils::Base64::decode(value);
3823

39-
return ret;
24+
bool Base64Decode::transform(std::string &value, const Transaction *trans) const {
25+
if (value.empty()) return false;
26+
value = Utils::Base64::decode(value);
27+
return true;
4028
}
4129

4230

43-
} // namespace transformations
44-
} // namespace actions
45-
} // namespace modsecurity
31+
} // namespace modsecurity::actions::transformations

src/actions/transformations/base64_decode.h

+6-18
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,21 @@
1313
*
1414
*/
1515

16-
#include <string>
17-
18-
#include "modsecurity/actions/action.h"
19-
#include "src/actions/transformations/transformation.h"
20-
2116
#ifndef SRC_ACTIONS_TRANSFORMATIONS_BASE64_DECODE_H_
2217
#define SRC_ACTIONS_TRANSFORMATIONS_BASE64_DECODE_H_
2318

24-
#ifdef __cplusplus
25-
namespace modsecurity {
26-
class Transaction;
19+
#include "transformation.h"
2720

28-
namespace actions {
29-
namespace transformations {
21+
namespace modsecurity::actions::transformations {
3022

3123
class Base64Decode : public Transformation {
3224
public:
33-
explicit Base64Decode(const std::string &action) : Transformation(action) { }
25+
explicit Base64Decode(const std::string &action)
26+
: Transformation(action) { }
3427

35-
std::string evaluate(const std::string &exp,
36-
Transaction *transaction) override;
28+
bool transform(std::string &value, const Transaction *trans) const override;
3729
};
3830

39-
} // namespace transformations
40-
} // namespace actions
41-
} // namespace modsecurity
42-
43-
#endif
31+
} // namespace modsecurity::actions::transformations
4432

4533
#endif // SRC_ACTIONS_TRANSFORMATIONS_BASE64_DECODE_H_

src/actions/transformations/base64_decode_ext.cc

+7-21
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,19 @@
1313
*
1414
*/
1515

16-
#include "src/actions/transformations/base64_decode_ext.h"
16+
#include "base64_decode_ext.h"
1717

18-
#include <iostream>
19-
#include <string>
20-
#include <algorithm>
21-
#include <functional>
22-
#include <cctype>
23-
#include <locale>
24-
25-
#include "modsecurity/transaction.h"
26-
#include "src/actions/transformations/transformation.h"
2718
#include "src/utils/base64.h"
2819

2920

30-
namespace modsecurity {
31-
namespace actions {
32-
namespace transformations {
33-
21+
namespace modsecurity::actions::transformations {
3422

35-
std::string Base64DecodeExt::evaluate(const std::string &value,
36-
Transaction *transaction) {
37-
std::string ret = Utils::Base64::decode_forgiven(value);
3823

39-
return ret;
24+
bool Base64DecodeExt::transform(std::string &value, const Transaction *trans) const {
25+
if (value.empty()) return false;
26+
value = Utils::Base64::decode_forgiven(value);
27+
return true;
4028
}
4129

4230

43-
} // namespace transformations
44-
} // namespace actions
45-
} // namespace modsecurity
31+
} // namespace modsecurity::actions::transformations

src/actions/transformations/base64_decode_ext.h

+6-18
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,21 @@
1313
*
1414
*/
1515

16-
#include <string>
17-
18-
#include "modsecurity/actions/action.h"
19-
#include "src/actions/transformations/transformation.h"
20-
2116
#ifndef SRC_ACTIONS_TRANSFORMATIONS_BASE64_DECODE_EXT_H_
2217
#define SRC_ACTIONS_TRANSFORMATIONS_BASE64_DECODE_EXT_H_
2318

24-
#ifdef __cplusplus
25-
namespace modsecurity {
26-
class Transaction;
19+
#include "transformation.h"
2720

28-
namespace actions {
29-
namespace transformations {
21+
namespace modsecurity::actions::transformations {
3022

3123
class Base64DecodeExt : public Transformation {
3224
public:
33-
explicit Base64DecodeExt(const std::string &action) : Transformation(action) { }
25+
explicit Base64DecodeExt(const std::string &action)
26+
: Transformation(action) { }
3427

35-
std::string evaluate(const std::string &exp,
36-
Transaction *transaction) override;
28+
bool transform(std::string &value, const Transaction *trans) const override;
3729
};
3830

39-
} // namespace transformations
40-
} // namespace actions
41-
} // namespace modsecurity
42-
43-
#endif
31+
} // namespace modsecurity::actions::transformations
4432

4533
#endif // SRC_ACTIONS_TRANSFORMATIONS_BASE64_DECODE_EXT_H_

src/actions/transformations/base64_encode.cc

+7-21
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,19 @@
1313
*
1414
*/
1515

16-
#include "src/actions/transformations/base64_encode.h"
16+
#include "base64_encode.h"
1717

18-
#include <iostream>
19-
#include <string>
20-
#include <algorithm>
21-
#include <functional>
22-
#include <cctype>
23-
#include <locale>
24-
25-
#include "modsecurity/transaction.h"
26-
#include "src/actions/transformations/transformation.h"
2718
#include "src/utils/base64.h"
2819

2920

30-
namespace modsecurity {
31-
namespace actions {
32-
namespace transformations {
33-
21+
namespace modsecurity::actions::transformations {
3422

35-
std::string Base64Encode::evaluate(const std::string &value,
36-
Transaction *transaction) {
37-
std::string ret = Utils::Base64::encode(value);
3823

39-
return ret;
24+
bool Base64Encode::transform(std::string &value, const Transaction *trans) const {
25+
if (value.empty()) return false;
26+
value = Utils::Base64::encode(value);
27+
return true;
4028
}
4129

4230

43-
} // namespace transformations
44-
} // namespace actions
45-
} // namespace modsecurity
31+
} // namespace modsecurity::actions::transformations

src/actions/transformations/base64_encode.h

+6-18
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,21 @@
1313
*
1414
*/
1515

16-
#include <string>
17-
18-
#include "modsecurity/actions/action.h"
19-
#include "src/actions/transformations/transformation.h"
20-
2116
#ifndef SRC_ACTIONS_TRANSFORMATIONS_BASE64_ENCODE_H_
2217
#define SRC_ACTIONS_TRANSFORMATIONS_BASE64_ENCODE_H_
2318

24-
#ifdef __cplusplus
25-
namespace modsecurity {
26-
class Transaction;
19+
#include "transformation.h"
2720

28-
namespace actions {
29-
namespace transformations {
21+
namespace modsecurity::actions::transformations {
3022

3123
class Base64Encode : public Transformation {
3224
public:
33-
explicit Base64Encode(const std::string &action) : Transformation(action) { }
25+
explicit Base64Encode(const std::string &action)
26+
: Transformation(action) { }
3427

35-
std::string evaluate(const std::string &exp,
36-
Transaction *transaction) override;
28+
bool transform(std::string &value, const Transaction *trans) const override;
3729
};
3830

39-
} // namespace transformations
40-
} // namespace actions
41-
} // namespace modsecurity
42-
43-
#endif
31+
} // namespace modsecurity::actions::transformations
4432

4533
#endif // SRC_ACTIONS_TRANSFORMATIONS_BASE64_ENCODE_H_

0 commit comments

Comments
 (0)