Skip to content

Commit fda03c0

Browse files
committed
Yet another refactoring in Rule
1 parent b662248 commit fda03c0

26 files changed

+1017
-930
lines changed

headers/modsecurity/rule.h

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,19 @@ using TransformationResult = std::pair<std::shared_ptr<std::string>,
5656
std::shared_ptr<std::string>>;
5757
using TransformationResults = std::list<TransformationResult>;
5858

59+
using Transformation = actions::transformations::Transformation;
60+
using Transformations = std::vector<Transformation *>;
61+
62+
using Tags = std::vector<actions::Tag *>;
63+
using SetVars = std::vector<actions::SetVar *>;
64+
using MatchActions = std::vector<actions::Action *>;
65+
5966
class Rule {
6067
public:
61-
Rule(operators::Operator *_op,
62-
variables::Variables *_variables,
63-
std::vector<actions::Action *> *_actions,
68+
Rule(operators::Operator *op,
69+
variables::Variables *variables,
70+
std::vector<actions::Action *> *actions,
71+
Transformations *transformations,
6472
std::unique_ptr<std::string> fileName,
6573
int lineNumber);
6674
explicit Rule(const std::string &marker);
@@ -108,37 +116,67 @@ class Rule {
108116
int *nth) const;
109117

110118

111-
actions::Action *m_theDisruptiveAction;
112-
actions::LogData *m_logData;
113-
actions::Msg *m_msg;
114-
actions::Severity *m_severity;
115-
bool m_chained;
116-
bool m_containsCaptureAction;
117-
bool m_containsMultiMatchAction;
118-
bool m_containsStaticBlockAction;
119-
bool m_secMarker;
119+
120+
inline bool isUnconditional() const { return m_operator == NULL; }
121+
122+
virtual bool isMarker() { return m_isSecMarker; }
123+
124+
inline bool isChained() const { return m_isChained == true; }
125+
inline bool hasCaptureAction() const { return m_containsCaptureAction == true; }
126+
inline void setChained(bool b) { m_isChained = b; }
127+
inline bool hasDisruptiveAction() const { return m_disruptiveAction != NULL; }
128+
129+
130+
inline bool hasLogData() const { return m_logData != NULL; }
131+
std::string logData(Transaction *t);
132+
inline bool hasMsg() const { return m_msg != NULL; }
133+
std::string msg(Transaction *t);
134+
inline bool hasSeverity() const { return m_severity != NULL; }
135+
int severity() const;
136+
int getPhase() const { return m_phase; }
137+
void setPhase(int phase) { m_phase = phase; }
138+
139+
std::string getOperatorName() const;
140+
120141
int64_t m_ruleId;
121-
int m_accuracy;
122-
int m_lineNumber;
123-
int m_maturity;
124-
int m_phase;
125-
modsecurity::variables::Variables *m_variables;
126-
operators::Operator *m_op;
127142
std::unique_ptr<Rule> m_chainedRuleChild;
128143
Rule *m_chainedRuleParent;
144+
129145
std::shared_ptr<std::string> m_fileName;
146+
130147
std::string m_marker;
131148
std::string m_rev;
132149
std::string m_ver;
133-
std::vector<actions::Action *> m_actionsRuntimePos;
134-
std::vector<actions::Action *> m_actionsRuntimePre;
135-
std::vector<actions::SetVar *> m_actionsSetVar;
136-
std::vector<actions::Tag *> m_actionsTag;
150+
int m_accuracy;
151+
int m_maturity;
152+
int m_lineNumber;
137153

138154
private:
139-
bool m_unconditional;
140-
};
155+
modsecurity::variables::Variables *m_variables;
156+
operators::Operator *m_operator;
157+
158+
/* actions */
159+
actions::Action *m_disruptiveAction;
160+
actions::LogData *m_logData;
161+
actions::Msg *m_msg;
162+
actions::Severity *m_severity;
163+
MatchActions m_actionsRuntimePos;
164+
SetVars m_actionsSetVar;
165+
Tags m_actionsTag;
166+
167+
/* actions > transformations */
168+
Transformations m_transformations;
141169

170+
bool m_containsCaptureAction:1;
171+
bool m_containsMultiMatchAction:1;
172+
bool m_containsStaticBlockAction:1;
173+
bool m_isChained:1;
174+
bool m_isSecMarker:1;
175+
bool m_unconditional:1;
176+
177+
int m_phase;
178+
179+
};
142180

143181
} // namespace modsecurity
144182
#endif

headers/modsecurity/rule_message.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class RuleMessage {
5151
m_maturity(rule->m_maturity),
5252
m_message(""),
5353
m_noAuditLog(false),
54-
m_phase(rule->m_phase - 1),
54+
m_phase(rule->getPhase() - 1),
5555
m_reference(""),
5656
m_rev(rule->m_rev),
5757
m_rule(rule),

headers/modsecurity/rules_set_phases.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ class Rule;
3939
/** @ingroup ModSecurity_CPP_API */
4040
class RulesSetPhases {
4141
public:
42-
~RulesSetPhases();
4342

4443
bool insert(std::shared_ptr<Rule> rule);
4544

4645
int append(RulesSetPhases *from, std::ostringstream *err);
4746
void dump() const;
4847

49-
Rules *operator[](int index) { return &m_rules[index]; }
50-
Rules *at(int index) { return &m_rules[index]; }
48+
Rules *operator[](int index) { return &m_rulesAtPhase[index]; }
49+
Rules *at(int index) { return &m_rulesAtPhase[index]; }
50+
51+
private:
52+
Rules m_rulesAtPhase[8];
5153

52-
Rules m_rules[8];
5354
};
5455

5556

src/actions/block.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool Block::evaluate(Rule *rule, Transaction *transaction,
3333
std::shared_ptr<RuleMessage> rm) {
3434
ms_dbg_a(transaction, 8, "Marking request as disruptive.");
3535

36-
for (auto &a : transaction->m_rules->m_defaultActions[rule->m_phase]) {
36+
for (auto &a : transaction->m_rules->m_defaultActions[rule->getPhase()]) {
3737
if (a->isDisruptive() == false) {
3838
continue;
3939
}

src/actions/chain.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace actions {
2626

2727

2828
bool Chain::evaluate(Rule *rule, Transaction *transaction) {
29-
rule->m_chained = true;
29+
rule->setChained(true);
3030
return true;
3131
}
3232

src/actions/phase.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool Phase::init(std::string *error) {
7373

7474

7575
bool Phase::evaluate(Rule *rule, Transaction *transaction) {
76-
rule->m_phase = m_phase;
76+
rule->setPhase(m_phase);
7777
return true;
7878
}
7979

src/operators/detect_sqli.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bool DetectSQLi::evaluate(Transaction *t, Rule *rule,
3838
ms_dbg_a(t, 4, "detected SQLi using libinjection with " \
3939
"fingerprint '" + std::string(fingerprint) + "' at: '" +
4040
input + "'");
41-
if (rule && t && rule->m_containsCaptureAction) {
41+
if (rule && t && rule->hasCaptureAction()) {
4242
t->m_collections.m_tx_collection->storeOrUpdateFirst(
4343
"0", std::string(fingerprint));
4444
ms_dbg_a(t, 7, "Added DetectSQLi match TX.0: " + \

src/operators/detect_xss.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ bool DetectXSS::evaluate(Transaction *t, Rule *rule,
3434
if (t) {
3535
if (is_xss) {
3636
ms_dbg_a(t, 5, "detected XSS using libinjection.");
37-
if (rule && t && rule->m_containsCaptureAction) {
37+
if (rule && t && rule->hasCaptureAction()) {
3838
t->m_collections.m_tx_collection->storeOrUpdateFirst(
3939
"0", std::string(input));
4040
ms_dbg_a(t, 7, "Added DetectXSS match TX.0: " + \

src/operators/pm.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ bool Pm::evaluate(Transaction *transaction, Rule *rule,
9797
#endif
9898

9999
if (rc >= 0 && transaction) {
100-
std::string match_(match);
100+
std::string match_(match?match:"");
101101
logOffset(ruleMessage, rc - match_.size() + 1, match_.size());
102102
transaction->m_matched.push_back(match_);
103-
}
104103

105-
if (rule && rule->m_containsCaptureAction && transaction && rc >= 0) {
106-
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("0",
107-
std::string(match));
108-
ms_dbg_a(transaction, 7, "Added pm match TX.0: " + \
109-
std::string(match));
104+
if (rule && rule->hasCaptureAction()) {
105+
transaction->m_collections.m_tx_collection->storeOrUpdateFirst("0",
106+
match_);
107+
ms_dbg_a(transaction, 7, "Added pm match TX.0: " + \
108+
match_);
109+
}
110110
}
111111

112112
return rc >= 0;

src/operators/rbl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ bool Rbl::evaluate(Transaction *t, Rule *rule,
226226
furtherInfo(sin, ipStr, t, m_provider);
227227

228228
freeaddrinfo(info);
229-
if (rule && t && rule->m_containsCaptureAction) {
229+
if (rule && t && rule->hasCaptureAction()) {
230230
t->m_collections.m_tx_collection->storeOrUpdateFirst(
231231
"0", std::string(ipStr));
232232
ms_dbg_a(t, 7, "Added RXL match TX.0: " + \

0 commit comments

Comments
 (0)