Skip to content

Commit b662248

Browse files
committed
Refactoring in Rule: Meaningful structures name
1 parent 96849c0 commit b662248

File tree

3 files changed

+32
-37
lines changed

3 files changed

+32
-37
lines changed

headers/modsecurity/rule.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ class Msg;
4444
class Rev;
4545
class SetVar;
4646
class Tag;
47+
namespace transformations {
48+
class Transformation;
49+
}
4750
}
4851
namespace operators {
4952
class Operator;
5053
}
5154

55+
using TransformationResult = std::pair<std::shared_ptr<std::string>,
56+
std::shared_ptr<std::string>>;
57+
using TransformationResults = std::list<TransformationResult>;
58+
5259
class Rule {
5360
public:
5461
Rule(operators::Operator *_op,
@@ -68,13 +75,6 @@ class Rule {
6875
bool containsBlock, std::shared_ptr<RuleMessage> ruleMessage,
6976
actions::Action *a, bool context);
7077

71-
inline void executeTransformation(actions::Action *a,
72-
std::shared_ptr<std::string> *value,
73-
Transaction *trans,
74-
std::list<std::pair<std::shared_ptr<std::string>,
75-
std::shared_ptr<std::string>>> *ret,
76-
std::string *path,
77-
int *nth) const;
7878

7979
void getVariablesExceptions(Transaction *t,
8080
variables::Variables *exclusion, variables::Variables *addition);
@@ -83,10 +83,6 @@ class Rule {
8383
void executeActionsAfterFullMatch(Transaction *trasn,
8484
bool containsDisruptive, std::shared_ptr<RuleMessage> ruleMessage);
8585

86-
std::list<std::pair<std::shared_ptr<std::string>,
87-
std::shared_ptr<std::string>>> executeDefaultTransformations(
88-
Transaction *trasn, const std::string &value);
89-
9086
bool executeOperatorAt(Transaction *trasn, const std::string &key,
9187
std::string value, std::shared_ptr<RuleMessage> rm);
9288
void executeActionsIndependentOfChainedRuleResult(Transaction *trasn,
@@ -100,15 +96,17 @@ class Rule {
10096
bool containsTag(const std::string& name, Transaction *t);
10197
bool containsMsg(const std::string& name, Transaction *t);
10298

99+
103100
void executeTransformations(
104-
actions::Action *a,
105-
std::shared_ptr<std::string> newValue,
106-
std::shared_ptr<std::string> value,
101+
Transaction *trasn, const std::string &value, TransformationResults &ret);
102+
inline void executeTransformation(
103+
actions::transformations::Transformation *a,
104+
std::shared_ptr<std::string> *value,
107105
Transaction *trans,
108-
std::list<std::pair<std::shared_ptr<std::string>,
109-
std::shared_ptr<std::string>>> *ret,
110-
std::shared_ptr<std::string> transStr,
111-
int nth);
106+
TransformationResults *ret,
107+
std::string *path,
108+
int *nth) const;
109+
112110

113111
actions::Action *m_theDisruptiveAction;
114112
actions::LogData *m_logData;

src/parser/seclang-parser.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ namespace yy {
24252425
}
24262426
checkedActions.push_back(a);
24272427
} else {
2428-
driver.error(yystack_[2].location, "The action '" + a->m_name + "' is not suitable to be part of the SecDefaultActions");
2428+
driver.error(yystack_[2].location, "The action '" + *a->m_name.get() + "' is not suitable to be part of the SecDefaultActions");
24292429
YYERROR;
24302430
}
24312431
}

src/rule.cc

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ using operators::Operator;
4949
using actions::Action;
5050
using variables::Variable;
5151
using actions::transformations::None;
52-
52+
using actions::transformations::Transformation;
5353

5454
Rule::Rule(const std::string &marker)
5555
: m_theDisruptiveAction(nullptr),
@@ -326,11 +326,11 @@ bool Rule::executeOperatorAt(Transaction *trans, const std::string &key,
326326
}
327327

328328

329-
inline void Rule::executeTransformation(actions::Action *a,
329+
inline void Rule::executeTransformation(
330+
actions::transformations::Transformation *a,
330331
std::shared_ptr<std::string> *value,
331332
Transaction *trans,
332-
std::list<std::pair<std::shared_ptr<std::string>,
333-
std::shared_ptr<std::string>>> *ret,
333+
TransformationResults *ret,
334334
std::string *path,
335335
int *nth) const {
336336

@@ -359,15 +359,11 @@ inline void Rule::executeTransformation(actions::Action *a,
359359
}
360360

361361

362-
std::list<std::pair<std::shared_ptr<std::string>,
363-
std::shared_ptr<std::string>>>
364-
Rule::executeDefaultTransformations(
365-
Transaction *trans, const std::string &in) {
362+
void Rule::executeTransformations(
363+
Transaction *trans, const std::string &in, TransformationResults &ret) {
366364
int none = 0;
367365
int transformations = 0;
368366
std::string path("");
369-
std::list<std::pair<std::shared_ptr<std::string>,
370-
std::shared_ptr<std::string>>> ret;
371367
std::shared_ptr<std::string> value =
372368
std::shared_ptr<std::string>(new std::string(in));
373369

@@ -394,14 +390,17 @@ std::list<std::pair<std::shared_ptr<std::string>,
394390
continue;
395391
}
396392

397-
executeTransformation(a.get(), &value, trans, &ret, &path,
393+
// FIXME: here the object needs to be a transformation already.
394+
Transformation *t = dynamic_cast<Transformation *>(a.get());
395+
executeTransformation(t, &value, trans, &ret, &path,
398396
&transformations);
399397
}
400398
}
401399

402400
for (Action *a : this->m_actionsRuntimePre) {
403401
if (none == 0) {
404-
executeTransformation(a, &value, trans, &ret, &path,
402+
Transformation *t = dynamic_cast<Transformation *>(a);
403+
executeTransformation(t, &value, trans, &ret, &path,
405404
&transformations);
406405
}
407406
if (a->m_isNone) {
@@ -427,7 +426,8 @@ std::list<std::pair<std::shared_ptr<std::string>,
427426
}
428427
actions::Action *a = dynamic_cast<actions::Action*>(b.second.get());
429428
if (none == 0) {
430-
executeTransformation(a, &value, trans, &ret, &path,
429+
Transformation *t = dynamic_cast<Transformation *>(a);
430+
executeTransformation(t, &value, trans, &ret, &path,
431431
&transformations);
432432
}
433433
if (a->m_isNone) {
@@ -446,8 +446,6 @@ std::list<std::pair<std::shared_ptr<std::string>,
446446
std::shared_ptr<std::string>(new std::string(*value)),
447447
std::shared_ptr<std::string>(new std::string(path))));
448448
}
449-
450-
return ret;
451449
}
452450

453451

@@ -711,10 +709,9 @@ bool Rule::evaluate(Transaction *trans,
711709
continue;
712710
}
713711

714-
std::list<std::pair<std::shared_ptr<std::string>,
715-
std::shared_ptr<std::string>>> values;
712+
TransformationResults values;
716713

717-
values = executeDefaultTransformations(trans, value);
714+
executeTransformations(trans, value, values);
718715

719716
for (const auto &valueTemp : values) {
720717
bool ret;

0 commit comments

Comments
 (0)