Skip to content

Commit 1e9c548

Browse files
committed
Improves rules dump for better testing
1 parent 17c4201 commit 1e9c548

File tree

8 files changed

+53
-9
lines changed

8 files changed

+53
-9
lines changed

CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.x.y - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
- More structured rules dump. Better supporting debugging.
5+
[@zimmerle]
46
- Added the basics for supporting better error/warning handling while
57
loading configurations.
68
[@zimmerle]

headers/modsecurity/rule.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class Rule {
4646
Rule(std::unique_ptr<std::string> fileName, int lineNumber)
4747
: m_fileName(std::make_shared<std::string>(*fileName)),
4848
m_lineNumber(lineNumber),
49-
m_phase(modsecurity::Phases::RequestHeadersPhase) {
50-
}
49+
m_phase(modsecurity::Phases::RequestHeadersPhase)
50+
{ }
5151

5252
Rule(const Rule &other) :
5353
m_fileName(other.m_fileName),
@@ -82,6 +82,18 @@ class Rule {
8282
return "<<no file>>:" + std::to_string(m_lineNumber);
8383
}
8484

85+
virtual void dump(std::stringstream &out) {
86+
out << getOriginInTextFormat() << std::endl;
87+
}
88+
89+
protected:
90+
std::string getOriginInTextFormat() const {
91+
std::stringstream ss;
92+
ss << "# File name: " << *getFileName() << std::endl;
93+
ss << "# Line number: " << getLineNumber();
94+
return ss.str();
95+
}
96+
8597
private:
8698
std::shared_ptr<std::string> m_fileName;
8799
int m_lineNumber;

headers/modsecurity/rules.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ class Rules {
6161
std::vector<std::shared_ptr<actions::Action> > m_defaultActions;
6262
std::vector<std::shared_ptr<actions::transformations::Transformation> > m_defaultTransformations;
6363

64-
void dump();
64+
virtual void dump() {
65+
std::stringstream ss;
66+
dump(ss);
67+
std::cout << ss.str();
68+
};
69+
virtual void dump(std::stringstream &out);
6570

6671
inline iterator begin() noexcept { return m_rules.begin(); }
6772
inline const_iterator cbegin() const noexcept { return m_rules.cbegin(); }

src/rule_marker.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ class RuleMarker : public Rule {
4747
RuleMarker(const RuleMarker& r) :
4848
Rule(r),
4949
m_name(r.m_name)
50-
{ }
50+
{ };
51+
52+
RuleMarker(RuleMarker &&r) :
53+
Rule(r),
54+
m_name(std::move(r.m_name))
55+
{ };
5156

5257
RuleMarker &operator =(const RuleMarker& r) {
5358
Rule::operator = (r);
@@ -72,6 +77,11 @@ class RuleMarker : public Rule {
7277
return m_name;
7378
}
7479

80+
virtual void dump(std::stringstream &out) override {
81+
Rule::dump(out);
82+
out << "SecMarker \"" << *getName() << "\"" << std::endl;
83+
}
84+
7585
private:
7686
std::shared_ptr<std::string> m_name;
7787
};

src/rule_with_actions.h

+5
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ class RuleWithActions : public Rule {
442442
return dst;
443443
}
444444

445+
446+
virtual void dump(std::stringstream &out) override {
447+
out << "RuleWithActions" << std::endl;
448+
}
449+
445450
private:
446451
RuleId m_ruleId;
447452

src/rule_with_operator.h

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "modsecurity/variable_value.h"
3131
#include "modsecurity/rule.h"
3232
#include "src/rule_with_actions.h"
33+
#include "src/variables/variable.h"
34+
#include "src/operators/operator.h"
3335

3436
#ifdef __cplusplus
3537

@@ -73,6 +75,15 @@ class RuleWithOperator : public RuleWithActions {
7375
return std::to_string(getId());
7476
}
7577

78+
virtual void dump(std::stringstream &out) override {
79+
Rule::dump(out);
80+
out << "# RuleWithOperator" << std::endl;
81+
out << "SecRule ";
82+
out << m_variables->getVariableNames() << " ";
83+
out << "\"" << "@" << m_operator->m_op << " " << m_operator->m_param << "\"";
84+
out << std::endl;
85+
}
86+
7687
private:
7788
std::shared_ptr<modsecurity::variables::Variables> m_variables;
7889
std::shared_ptr<operators::Operator> m_operator;

src/rules.cc

+3-5
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ std::shared_ptr<Rule> Rules::at(int index) const {
5757
}
5858

5959

60-
void Rules::dump() {
61-
for (int j = 0; j < m_rules.size(); j++) {
62-
std::cout << " Rule ID: " << m_rules.at(j)->getReference();
63-
std::cout << "--" << m_rules.at(j) << std::endl;
60+
void Rules::dump(std::stringstream &out) {
61+
for (auto &r : m_rules) {
62+
r->dump(out);
6463
}
6564
}
6665

67-
6866
} // namespace modsecurity
6967

tools/rules-check/rules-check.cc

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ int main(int argc, char **argv) {
9191
if (err.empty() == false) {
9292
std::cerr << " " << err << std::endl;
9393
}
94+
rules->dump();
9495
next:
9596
args++;
9697
}

0 commit comments

Comments
 (0)