Skip to content

Commit 2fe6adc

Browse files
committed
Refactoring on the action addition to RuleWithAction
1 parent e85c088 commit 2fe6adc

11 files changed

+981
-1139
lines changed

src/parser/seclang-parser.cc

Lines changed: 774 additions & 779 deletions
Large diffs are not rendered by default.

src/parser/seclang-parser.yy

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,15 +1069,15 @@ expression:
10691069
audit_log
10701070
| DIRECTIVE variables op actions
10711071
{
1072-
std::vector<actions::Action *> *a = new std::vector<actions::Action *>();
1072+
std::vector<std::shared_ptr<actions::Action>> *a = new std::vector<std::shared_ptr<actions::Action>>();
10731073
std::vector<std::shared_ptr<actions::transformations::Transformation> > *t = new std::vector<std::shared_ptr<actions::transformations::Transformation> >();
10741074
for (auto &i : *$4.get()) {
10751075
if (dynamic_cast<actions::transformations::Transformation *>(i.get())) {
10761076
std::shared_ptr<actions::Action> at = std::move(i);
10771077
std::shared_ptr<actions::transformations::Transformation> t2 = std::dynamic_pointer_cast<actions::transformations::Transformation>(std::move(at));
10781078
t->push_back(std::move(t2));
10791079
} else {
1080-
a->push_back(i.release());
1080+
a->push_back(std::move(i));
10811081
}
10821082
}
10831083
variables::Variables *v = new variables::Variables();
@@ -1094,7 +1094,7 @@ expression:
10941094
/* file name */ std::unique_ptr<std::string>(new std::string(*@1.end.filename)),
10951095
/* line number */ @1.end.line
10961096
));
1097-
1097+
// TODO: filename should be a shared_ptr.
10981098
if (driver.addSecRule(std::move(rule)) == false) {
10991099
YYERROR;
11001100
}
@@ -1120,15 +1120,15 @@ expression:
11201120
}
11211121
| CONFIG_DIR_SEC_ACTION actions
11221122
{
1123-
std::vector<actions::Action *> *a = new std::vector<actions::Action *>();
1123+
std::vector<std::shared_ptr<actions::Action>> *a = new std::vector<std::shared_ptr<actions::Action>>();
11241124
std::vector<std::shared_ptr<actions::transformations::Transformation> > *t = new std::vector<std::shared_ptr<actions::transformations::Transformation> >();
11251125
for (auto &i : *$2.get()) {
11261126
if (dynamic_cast<actions::transformations::Transformation *>(i.get())) {
11271127
std::shared_ptr<actions::Action> at = std::move(i);
11281128
std::shared_ptr<actions::transformations::Transformation> t2 = std::dynamic_pointer_cast<actions::transformations::Transformation>(std::move(at));
11291129
t->push_back(std::move(t2));
11301130
} else {
1131-
a->push_back(i.release());
1131+
a->push_back(std::move(i));
11321132
}
11331133
}
11341134
std::unique_ptr<RuleUnconditional> rule(new RuleUnconditional(
@@ -1142,15 +1142,15 @@ expression:
11421142
| DIRECTIVE_SECRULESCRIPT actions
11431143
{
11441144
std::string err;
1145-
std::vector<actions::Action *> *a = new std::vector<actions::Action *>();
1145+
std::vector<std::shared_ptr<actions::Action>> *a = new std::vector<std::shared_ptr<actions::Action>>();
11461146
std::vector<std::shared_ptr<actions::transformations::Transformation> > *t = new std::vector<std::shared_ptr<actions::transformations::Transformation> >();
11471147
for (auto &i : *$2.get()) {
11481148
if (dynamic_cast<actions::transformations::Transformation *>(i.get())) {
11491149
std::shared_ptr<actions::Action> at = std::move(i);
11501150
std::shared_ptr<actions::transformations::Transformation> t2 = std::dynamic_pointer_cast<actions::transformations::Transformation>(std::move(at));
11511151
t->push_back(std::move(t2));
11521152
} else {
1153-
a->push_back(i.release());
1153+
a->push_back(std::move(i));
11541154
}
11551155
}
11561156
std::unique_ptr<RuleScript> r(new RuleScript(
@@ -1172,25 +1172,25 @@ expression:
11721172
| CONFIG_DIR_SEC_DEFAULT_ACTION actions
11731173
{
11741174
bool hasDisruptive = false;
1175-
std::vector<actions::Action *> *actions = new std::vector<actions::Action *>();
1175+
std::vector<std::shared_ptr<actions::Action>> *actions = new std::vector<std::shared_ptr<actions::Action>>();
11761176
for (auto &i : *$2.get()) {
1177-
actions->push_back(i.release());
1177+
actions->push_back(std::move(i));
11781178
}
1179-
std::vector<actions::Action *> checkedActions;
1179+
std::vector<std::shared_ptr<actions::Action>> checkedActions;
11801180
int definedPhase = -1;
11811181
int secRuleDefinedPhase = -1;
1182-
for (actions::Action *a : *actions) {
1183-
actions::Phase *phase = dynamic_cast<actions::Phase *>(a);
1184-
if (dynamic_cast<actions::disruptive::ActionDisruptive *>(a) != NULL
1185-
&& dynamic_cast<actions::Block *>(a) == NULL) {
1182+
for (auto &a : *actions) {
1183+
actions::Phase *phase = dynamic_cast<actions::Phase *>(a.get());
1184+
if (dynamic_cast<actions::disruptive::ActionDisruptive *>(a.get()) != NULL
1185+
&& dynamic_cast<actions::Block *>(a.get()) == NULL) {
11861186
hasDisruptive = true;
11871187
}
11881188
if (phase != NULL) {
11891189
definedPhase = phase->getPhase();
11901190
secRuleDefinedPhase = phase->getSecRulePhase();
11911191
delete phase;
1192-
} else if (dynamic_cast<actions::ActionAllowedAsSecDefaultAction *>(a)
1193-
&& !dynamic_cast<actions::transformations::None *>(a)) {
1192+
} else if (dynamic_cast<actions::ActionAllowedAsSecDefaultAction *>(a.get())
1193+
&& !dynamic_cast<actions::transformations::None *>(a.get())) {
11941194
checkedActions.push_back(a);
11951195
} else {
11961196
driver.error(@0, "The action '" + *a->getName() + "' is not suitable to be part of the SecDefaultActions");
@@ -1200,12 +1200,10 @@ expression:
12001200
if (definedPhase == -1) {
12011201
definedPhase = modsecurity::Phases::RequestHeadersPhase;
12021202
}
1203-
12041203
if (hasDisruptive == false) {
12051204
driver.error(@0, "SecDefaultAction must specify a disruptive action.");
12061205
YYERROR;
12071206
}
1208-
12091207
if (!driver.m_rulesSetPhases[definedPhase]->m_defaultActions.empty()) {
12101208
std::stringstream ss;
12111209
ss << "SecDefaultActions can only be placed once per phase and configuration context. Phase ";
@@ -1214,18 +1212,15 @@ expression:
12141212
driver.error(@0, ss.str());
12151213
YYERROR;
12161214
}
1217-
1218-
for (actions::Action *a : checkedActions) {
1219-
if (dynamic_cast<actions::transformations::Transformation *>(a)) {
1215+
for (auto &a : checkedActions) {
1216+
if (dynamic_cast<actions::transformations::Transformation *>(a.get())) {
12201217
driver.m_rulesSetPhases[definedPhase]->m_defaultTransformations.push_back(
1221-
std::shared_ptr<actions::transformations::Transformation>(
1222-
dynamic_cast<actions::transformations::Transformation *>(a)));
1218+
std::dynamic_pointer_cast<actions::transformations::Transformation>(a));
12231219
} else {
1224-
driver.m_rulesSetPhases[definedPhase]->m_defaultActions.push_back(std::unique_ptr<Action>(a));
1220+
driver.m_rulesSetPhases[definedPhase]->m_defaultActions.push_back(a);
12251221
}
12261222
}
1227-
1228-
delete actions;
1223+
//delete actions;
12291224
}
12301225
| CONFIG_DIR_SEC_MARKER
12311226
{

src/rule_script.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ using actions::Action;
4747
class RuleScript : public RuleWithActions {
4848
public:
4949
RuleScript(const std::string &name,
50-
std::vector<Action *> *actions,
50+
Actions *actions,
5151
Transformations *t,
5252
std::unique_ptr<std::string> fileName,
5353
int lineNumber)

src/rule_unconditional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace modsecurity {
4040
class RuleUnconditional : public RuleWithActions {
4141
public:
4242
RuleUnconditional(
43-
std::vector<actions::Action *> *actions,
43+
Actions *actions,
4444
Transformations *transformations,
4545
std::unique_ptr<std::string> fileName,
4646
int lineNumber)

0 commit comments

Comments
 (0)