2626#define HEADERS_MODSECURITY_RULE_H_
2727
2828#include " modsecurity/transaction.h"
29+ #include " modsecurity/modsecurity.h"
2930#include " modsecurity/variable_value.h"
3031
3132
@@ -63,19 +64,94 @@ using Tags = std::vector<actions::Tag *>;
6364using SetVars = std::vector<actions::SetVar *>;
6465using MatchActions = std::vector<actions::Action *>;
6566
66- class Rule {
67+ class RuleBase {
68+ public:
69+ RuleBase (std::unique_ptr<std::string> fileName, int lineNumber)
70+ : m_fileName(std::move(fileName)),
71+ m_lineNumber (lineNumber),
72+ m_phase(modsecurity::Phases::RequestHeadersPhase) {
73+ }
74+
75+ virtual bool evaluate (Transaction *transaction,
76+ std::shared_ptr<RuleMessage> rm) = 0;
77+
78+ std::shared_ptr<std::string> getFileName () const {
79+ return m_fileName;
80+ }
81+
82+ int getLineNumber () const {
83+ return m_lineNumber;
84+ }
85+
86+ int getPhase () const { return m_phase; }
87+ void setPhase (int phase) { m_phase = phase; }
88+
89+ virtual std::string getReference () {
90+ return *m_fileName + " :" + std::to_string (m_lineNumber);
91+ }
92+
93+
94+ virtual bool isMarker () { return false ; }
95+
96+ private:
97+ std::shared_ptr<std::string> m_fileName;
98+ int m_lineNumber;
99+ // FIXME: phase may not be neede to SecMarker.
100+ int m_phase;
101+ };
102+
103+
104+ class RuleMarker : public RuleBase {
105+ public:
106+ RuleMarker (
107+ const std::string &name,
108+ std::unique_ptr<std::string> fileName,
109+ int lineNumber)
110+ : RuleBase(std::move(fileName), lineNumber),
111+ m_name (std::make_shared<std::string>(name)) { }
112+
113+
114+ virtual bool evaluate (Transaction *transaction,
115+ std::shared_ptr<RuleMessage> rm) override {
116+
117+ if (transaction->isInsideAMarker ()) {
118+ if (*transaction->getCurrentMarker () == *m_name) {
119+ transaction->removeMarker ();
120+ // FIXME: Move this to .cc
121+ // ms_dbg_a(transaction, 4, "Out of a SecMarker " + *m_name);
122+ }
123+ }
124+
125+ return true ;
126+ };
127+
128+
129+ std::shared_ptr<std::string> getName () const {
130+ return m_name;
131+ }
132+
133+ bool isMarker () override { return true ; }
134+
135+ private:
136+ std::shared_ptr<std::string> m_name;
137+ };
138+
139+
140+ class Rule : public RuleBase {
67141 public:
68142 Rule (operators::Operator *op,
69143 variables::Variables *variables,
70144 std::vector<actions::Action *> *actions,
71145 Transformations *transformations,
72146 std::unique_ptr<std::string> fileName,
73147 int lineNumber);
74- explicit Rule (const std::string &marker);
148+ explicit Rule (const std::string &marker,
149+ std::unique_ptr<std::string> fileName,
150+ int lineNumber);
75151 virtual ~Rule ();
76152
77153 virtual bool evaluate (Transaction *transaction,
78- std::shared_ptr<RuleMessage> rm);
154+ std::shared_ptr<RuleMessage> rm) override ;
79155
80156 void organizeActions (std::vector<actions::Action *> *actions);
81157 void cleanUpActions ();
@@ -119,8 +195,6 @@ class Rule {
119195
120196 inline bool isUnconditional () const { return m_operator == NULL ; }
121197
122- virtual bool isMarker () { return m_isSecMarker; }
123-
124198 inline bool isChained () const { return m_isChained == true ; }
125199 inline bool hasCaptureAction () const { return m_containsCaptureAction == true ; }
126200 inline void setChained (bool b) { m_isChained = b; }
@@ -133,23 +207,23 @@ class Rule {
133207 std::string msg (Transaction *t);
134208 inline bool hasSeverity () const { return m_severity != NULL ; }
135209 int severity () const ;
136- int getPhase () const { return m_phase; }
137- void setPhase (int phase) { m_phase = phase; }
138210
139211 std::string getOperatorName () const ;
140212
141213 int64_t m_ruleId;
214+
215+ virtual std::string getReference () override {
216+ return std::to_string (m_ruleId);
217+ }
218+
142219 std::unique_ptr<Rule> m_chainedRuleChild;
143220 Rule *m_chainedRuleParent;
144221
145- std::shared_ptr<std::string> m_fileName;
146-
147222 std::string m_marker;
148223 std::string m_rev;
149224 std::string m_ver;
150225 int m_accuracy;
151226 int m_maturity;
152- int m_lineNumber;
153227
154228 private:
155229 modsecurity::variables::Variables *m_variables;
@@ -174,7 +248,7 @@ class Rule {
174248 bool m_isSecMarker:1 ;
175249 bool m_unconditional:1 ;
176250
177- int m_phase;
251+
178252
179253};
180254
0 commit comments