Skip to content

Commit b185ae4

Browse files
committed
Fix RuleWithAction object copy
EXPERIMENTAL. Missing everything except for SetVar
1 parent ecd7316 commit b185ae4

9 files changed

+274
-11
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ libmodsecurity_la_SOURCES = \
287287
rules.cc \
288288
rule_unconditional.cc \
289289
rule_with_actions.cc \
290+
rule_with_actions_properties.cc \
290291
rule_with_operator.cc \
291292
rule_message.cc \
292293
rule_script.cc \

src/actions/action_with_run_time_string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class ActionWithRunTimeString : public virtual Action {
4343
return *this;
4444
}
4545

46-
virtual void populate(RuleWithActions *rule) {
46+
virtual void populate(const RuleWithActions *rule) {
4747
if (m_string) {
4848
m_string->populate(rule);
4949
}
5050
}
5151

5252
std::string getEvaluatedRunTimeString(const Transaction *transaction) const noexcept {
53-
return (m_string == nullptr)?"":m_string->evaluate(transaction);
53+
return (!m_string)?"":m_string->evaluate(transaction);
5454
}
5555

5656
bool hasRunTimeString() const noexcept {

src/actions/set_var.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class SetVar : public ActionWithRunTimeString, public ActionWithExecution {
8787

8888
bool execute(Transaction *transaction) const noexcept override;
8989

90-
void populate(RuleWithActions *rule) override {
90+
void populate(const RuleWithActions *rule) override {
9191
ActionWithRunTimeString::populate(rule);
9292
variables::RuleVariable *rulev =
9393
dynamic_cast<variables::RuleVariable *>(

src/rule_with_actions.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
namespace modsecurity {
6161

62+
6263
RuleWithActions::RuleWithActions(
6364
Actions *actions,
6465
Transformations *transformations,
@@ -189,7 +190,6 @@ void RuleWithActions::executeActionsIndependentOfChainedRuleResult(Transaction *
189190
for (actions::SetVar *a : getSetVarsActionsPtr()) {
190191
ms_dbg_a(trans, 4, "Running [independent] (non-disruptive) " \
191192
"action: " + *a->getName());
192-
193193
a->execute(trans);
194194
}
195195

src/rule_with_actions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class RuleWithActions : public Rule, public RuleWithActionsProperties {
113113
m_containsCaptureAction(r.m_containsCaptureAction)
114114
{
115115
// TODO: Verify if it is necessary to process any other copy.
116+
populate(this);
117+
m_defaultActions.populate(this);
116118
};
117119

118120
RuleWithActions &operator=(const RuleWithActions& r) {
@@ -133,6 +135,9 @@ class RuleWithActions : public Rule, public RuleWithActionsProperties {
133135
m_actionLogData = r.m_actionLogData;
134136
m_containsCaptureAction = r.m_containsCaptureAction;
135137

138+
populate(this);
139+
m_defaultActions.populate(this);
140+
136141
return *this;
137142
// TODO: Verify if it is necessary to process any other copy.
138143
}

src/rule_with_actions_properties.cc

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include "modsecurity/rule.h"
17+
18+
#include <stdio.h>
19+
20+
#include <algorithm>
21+
#include <cstring>
22+
#include <iostream>
23+
#include <list>
24+
#include <memory>
25+
#include <string>
26+
#include <utility>
27+
28+
#include "modsecurity/actions/action.h"
29+
#include "modsecurity/modsecurity.h"
30+
#include "modsecurity/rule_message.h"
31+
#include "modsecurity/rules_set.h"
32+
#include "src/rule_with_actions.h"
33+
#include "src/actions/accuracy.h"
34+
#include "src/actions/block.h"
35+
#include "src/actions/capture.h"
36+
#include "src/actions/log_data.h"
37+
#include "src/actions/msg.h"
38+
#include "src/actions/maturity.h"
39+
#include "src/actions/multi_match.h"
40+
#include "src/actions/rev.h"
41+
#include "src/actions/log.h"
42+
#include "src/actions/no_log.h"
43+
#include "src/actions/set_var.h"
44+
#include "src/actions/severity.h"
45+
#include "src/actions/tag.h"
46+
#include "src/actions/disruptive/disruptive_action.h"
47+
#include "src/actions/transformations/transformation.h"
48+
#include "src/actions/transformations/none.h"
49+
#include "src/actions/xmlns.h"
50+
#include "src/utils/string.h"
51+
#include "src/actions/action_with_run_time_string.h"
52+
#include "src/actions/phase.h"
53+
#include "src/actions/chain.h"
54+
#include "src/actions/rule_id.h"
55+
#include "src/actions/ver.h"
56+
#include "src/actions/action_type_rule_metadata.h"
57+
#include "src/actions/action_allowed_in_sec_default_action.h"
58+
59+
60+
namespace modsecurity {
61+
62+
void RuleWithActionsProperties::populate(const RuleWithActions *r) {
63+
// FIXME: Populate the rest of the stuff.
64+
for (auto i : m_actionsSetVar) {
65+
/**
66+
*
67+
* ActionWithRunTimeString needs to be aware of the Rule that it
68+
* belongs to. It is necessary to resolve some variables
69+
* (e.g. Rule); Clone and associate are mandatory.
70+
*
71+
*/
72+
actions::ActionWithRunTimeString *arts = dynamic_cast<actions::ActionWithRunTimeString *>(i.get());
73+
if (arts != nullptr) {
74+
arts->populate(r);
75+
}
76+
}
77+
}
78+
79+
80+
RuleWithActionsProperties::RuleWithActionsProperties(const RuleWithActionsProperties &o) :
81+
m_actionsRuntimePos(o.m_actionsRuntimePos),
82+
//m_actionsSetVar(o.m_actionsSetVar),
83+
m_actionsSetVar(),
84+
m_actionsTag(o.m_actionsTag),
85+
m_actionDisruptiveAction(o.m_actionDisruptiveAction),
86+
m_containsAuditLogAction(o.m_containsAuditLogAction),
87+
m_containsLogAction(o.m_containsLogAction),
88+
m_containsMultiMatchAction(o.m_containsMultiMatchAction),
89+
m_containsNoAuditLogAction(o.m_containsNoAuditLogAction),
90+
m_containsNoLogAction(o.m_containsNoAuditLogAction),
91+
m_containsStaticBlockAction(o.m_containsStaticBlockAction),
92+
m_transformations(o.m_transformations)
93+
{
94+
// TODO: Copy the rest of the stuff.
95+
for (auto i : o.m_actionsSetVar) {
96+
actions::ActionWithRunTimeString *arts = dynamic_cast<actions::ActionWithRunTimeString *>(i.get());
97+
if (!arts) {
98+
Action *a = i->clone();
99+
actions::SetVar *aa = dynamic_cast<actions::SetVar *>(a);
100+
aa->populate(nullptr);
101+
m_actionsSetVar.push_back(std::make_shared<actions::SetVar>(*aa));
102+
continue;
103+
}
104+
m_actionsSetVar.push_back(i);
105+
}
106+
};
107+
108+
109+
} // namespace modsecurity

src/rule_with_actions_properties.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
17+
#ifndef SRC_RULE_WITH_ACTIONS_PROPERTIES_H_
18+
#define SRC_RULE_WITH_ACTIONS_PROPERTIES_H_
19+
20+
21+
#include "modsecurity/transaction.h"
22+
#include "modsecurity/modsecurity.h"
23+
#include "modsecurity/variable_value.h"
24+
#include "modsecurity/rule.h"
25+
#include "modsecurity/actions/action.h"
26+
#include "src/actions/action_type_rule_metadata.h"
27+
#include "src/actions/action_with_execution.h"
28+
#include "src/actions/disruptive/disruptive_action.h"
29+
30+
namespace modsecurity {
31+
32+
namespace actions {
33+
class Action;
34+
class Severity;
35+
class LogData;
36+
class Msg;
37+
class Rev;
38+
class SetVar;
39+
class Tag;
40+
class XmlNS;
41+
namespace transformations {
42+
class Transformation;
43+
}
44+
}
45+
46+
using Transformation = actions::transformations::Transformation;
47+
using Transformations = std::vector<std::shared_ptr<Transformation>>;
48+
using TransformationsPtr = std::vector<Transformation *>;
49+
50+
using Action = actions::Action;
51+
using Actions = std::vector<std::shared_ptr<Action>>;
52+
53+
using ActionWithExecution = actions::ActionWithExecution;
54+
using ActionTypeRuleMetaData = actions::ActionTypeRuleMetaData;
55+
using ActionDisruptive = actions::disruptive::ActionDisruptive;
56+
57+
using MatchActions = std::vector<std::shared_ptr<ActionWithExecution > >;
58+
using MatchActionsPtr = std::vector<ActionWithExecution *>;
59+
60+
using Tags = std::vector<std::shared_ptr<actions::Tag> >;
61+
using TagsPtr = std::vector<actions::Tag *>;
62+
63+
using SetVars = std::vector<std::shared_ptr<actions::SetVar> >;
64+
using SetVarsPtr = std::vector<actions::SetVar *>;
65+
66+
using XmlNSs = std::vector<std::shared_ptr<actions::XmlNS> >;
67+
using XmlNSsPtr = std::vector<actions::XmlNS *>;
68+
69+
70+
class RuleWithActionsProperties {
71+
public:
72+
int SEVERITY_NOT_SET = 10;
73+
int ACCURACY_NOT_SET = 10;
74+
int MATURITY_NOT_SET = 10;
75+
76+
RuleWithActionsProperties(Transformations *transformations = nullptr) :
77+
m_actionsRuntimePos(),
78+
m_actionsSetVar(),
79+
m_actionsTag(),
80+
m_actionDisruptiveAction(nullptr),
81+
m_containsAuditLogAction(false),
82+
m_containsLogAction(false),
83+
m_containsMultiMatchAction(false),
84+
m_containsNoAuditLogAction(false),
85+
m_containsNoLogAction(false),
86+
m_containsStaticBlockAction(false),
87+
m_transformations(transformations != nullptr ? *transformations : Transformations())
88+
{ };
89+
90+
RuleWithActionsProperties(const RuleWithActionsProperties &o);
91+
92+
RuleWithActionsProperties &operator=(const RuleWithActionsProperties &o) {
93+
m_actionsRuntimePos = o.m_actionsRuntimePos;
94+
m_actionsSetVar = o.m_actionsSetVar;
95+
m_actionsTag = o.m_actionsTag;
96+
m_actionDisruptiveAction = o.m_actionDisruptiveAction;
97+
m_containsAuditLogAction = o.m_containsAuditLogAction;
98+
m_containsLogAction = o.m_containsLogAction;
99+
m_containsMultiMatchAction = o.m_containsMultiMatchAction;
100+
m_containsNoAuditLogAction = o.m_containsNoAuditLogAction;
101+
m_containsNoLogAction = o.m_containsNoAuditLogAction;
102+
m_containsStaticBlockAction = o.m_containsStaticBlockAction;
103+
m_transformations = o.m_transformations;
104+
105+
return *this;
106+
};
107+
108+
void clear() {
109+
m_containsLogAction = false;
110+
m_containsNoLogAction = false;
111+
m_containsStaticBlockAction = false;
112+
m_actionsSetVar.clear();
113+
m_actionsTag.clear();
114+
m_actionsRuntimePos.clear();
115+
m_actionDisruptiveAction = nullptr;
116+
m_actionsRuntimePos.clear();
117+
m_transformations.clear();
118+
};
119+
120+
void populate(const RuleWithActions *r);
121+
122+
/* m_transformations */
123+
//inline Transformations::const_iterator getTransformations() const noexcept {
124+
// return m_transformations.begin();
125+
//}
126+
127+
public:
128+
MatchActions m_actionsRuntimePos;
129+
SetVars m_actionsSetVar;
130+
Tags m_actionsTag;
131+
std::shared_ptr<ActionDisruptive> m_actionDisruptiveAction;
132+
bool m_containsAuditLogAction:1;
133+
bool m_containsLogAction:1;
134+
bool m_containsMultiMatchAction:1;
135+
bool m_containsNoAuditLogAction:1;
136+
bool m_containsNoLogAction:1;
137+
bool m_containsStaticBlockAction:1;
138+
139+
140+
Transformations m_transformations;
141+
};
142+
143+
} // namespace modsecurity
144+
145+
146+
#endif // SRC_RULE_WITH_ACTIONS_PROPERTIES_H_

src/run_time_string.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class RunTimeString {
7575
}
7676

7777

78-
void populate(RuleWithActions *rule) noexcept {
78+
void populate(const RuleWithActions *rule) noexcept {
7979
for (auto &a : m_elements) {
8080
a->populate(rule);
8181
}
@@ -108,7 +108,6 @@ class RunTimeString {
108108
rv = dynamic_cast<RuleVariable *>(nrv);
109109
rv->populate(nullptr);
110110
m_variable = std::unique_ptr<Variable>(nrv);
111-
/* m_variable = nullptr; */
112111
} else {
113112
m_variable = other.m_variable;
114113
}
@@ -119,7 +118,9 @@ class RunTimeString {
119118
void appendValueTo(const Transaction *transaction, std::string &v) const noexcept {
120119
if (m_variable && transaction) {
121120
VariableValues l;
121+
122122
m_variable->evaluate(transaction, &l);
123+
123124
if (!l.empty()) {
124125
v.append(l[0]->getValue());
125126
}
@@ -130,19 +131,20 @@ class RunTimeString {
130131
}
131132

132133

133-
void populate(RuleWithActions *rule) noexcept {
134+
void populate(const RuleWithActions *rule) noexcept {
134135
if (!m_variable) {
135136
return;
136137
}
137138

138139
RuleVariable *vrule = dynamic_cast<RuleVariable *>(m_variable.get());
139-
if (vrule != nullptr) {
140-
vrule->populate(rule);
140+
if (!vrule) {
141+
return;
141142
}
143+
vrule->populate(rule);
142144
}
143145

144146
private:
145-
std::string m_string;
147+
const std::string m_string;
146148
/*
147149
*
148150
* FIXME: In the current state m_variable should be a unique_ptr. There

src/variables/variable_with_runtime_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class VariableWithRunTimeString : public Variable {
4040
return *this;
4141
}
4242

43-
virtual void populate(RuleWithActions *rule) {
43+
virtual void populate(const RuleWithActions *rule) {
4444
if (m_string) {
4545
m_string->populate(rule);
4646
}

0 commit comments

Comments
 (0)