mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-13 13:26:01 +03:00
Improves rules dump for better testing
This commit is contained in:
parent
78d9575dd2
commit
96efe83174
2
CHANGES
2
CHANGES
@ -1,6 +1,8 @@
|
||||
v3.x.y - YYYY-MMM-DD (to be released)
|
||||
-------------------------------------
|
||||
|
||||
- More structured rules dump. Better supporting debugging.
|
||||
[@zimmerle]
|
||||
- Added the basics for supporting better error/warning handling while
|
||||
loading configurations.
|
||||
[@zimmerle]
|
||||
|
@ -46,15 +46,14 @@ class Rule {
|
||||
Rule(std::unique_ptr<std::string> fileName, int lineNumber)
|
||||
: m_fileName(std::move(fileName)),
|
||||
m_lineNumber(lineNumber),
|
||||
m_phase(modsecurity::Phases::RequestHeadersPhase) {
|
||||
}
|
||||
m_phase(modsecurity::Phases::RequestHeadersPhase)
|
||||
{ }
|
||||
|
||||
Rule(const Rule &r)
|
||||
: m_fileName(r.m_fileName),
|
||||
m_lineNumber(r.m_lineNumber),
|
||||
m_phase(r.m_phase) {
|
||||
|
||||
}
|
||||
m_phase(r.m_phase)
|
||||
{ }
|
||||
|
||||
Rule &operator=(const Rule& other) {
|
||||
m_fileName = other.m_fileName;
|
||||
@ -80,6 +79,18 @@ class Rule {
|
||||
return *m_fileName + ":" + std::to_string(m_lineNumber);
|
||||
}
|
||||
|
||||
virtual void dump(std::stringstream &out) {
|
||||
out << getOriginInTextFormat() << std::endl;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::string getOriginInTextFormat() const {
|
||||
std::stringstream ss;
|
||||
ss << "# File name: " << *getFileName() << std::endl;
|
||||
ss << "# Line number: " << getLineNumber();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<std::string> m_fileName;
|
||||
int m_lineNumber;
|
||||
|
@ -61,7 +61,12 @@ class Rules {
|
||||
std::vector<std::shared_ptr<actions::Action> > m_defaultActions;
|
||||
std::vector<std::shared_ptr<actions::transformations::Transformation> > m_defaultTransformations;
|
||||
|
||||
void dump();
|
||||
virtual void dump() {
|
||||
std::stringstream ss;
|
||||
dump(ss);
|
||||
std::cout << ss.str();
|
||||
};
|
||||
virtual void dump(std::stringstream &out);
|
||||
|
||||
inline iterator begin() noexcept { return m_rules.begin(); }
|
||||
inline const_iterator cbegin() const noexcept { return m_rules.cbegin(); }
|
||||
|
@ -44,6 +44,15 @@ class RuleMarker : public Rule {
|
||||
: Rule(std::move(fileName), lineNumber),
|
||||
m_name(std::make_shared<std::string>(name)) { }
|
||||
|
||||
RuleMarker(RuleMarker &&r) :
|
||||
Rule(r),
|
||||
m_name(std::move(r.m_name))
|
||||
{ };
|
||||
|
||||
RuleMarker(const RuleMarker &r) :
|
||||
Rule(r),
|
||||
m_name(std::move(r.m_name))
|
||||
{ };
|
||||
|
||||
virtual bool evaluate(Transaction *transaction) override {
|
||||
if (transaction->isInsideAMarker()) {
|
||||
@ -62,6 +71,11 @@ class RuleMarker : public Rule {
|
||||
return m_name;
|
||||
}
|
||||
|
||||
virtual void dump(std::stringstream &out) override {
|
||||
Rule::dump(out);
|
||||
out << "SecMarker \"" << *getName() << "\"" << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<std::string> m_name;
|
||||
};
|
||||
|
@ -454,6 +454,11 @@ class RuleWithActions : public Rule {
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
virtual void dump(std::stringstream &out) override {
|
||||
out << "RuleWithActions" << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
RuleId m_ruleId;
|
||||
|
||||
|
@ -30,6 +30,8 @@
|
||||
#include "modsecurity/variable_value.h"
|
||||
#include "modsecurity/rule.h"
|
||||
#include "src/rule_with_actions.h"
|
||||
#include "src/variables/variable.h"
|
||||
#include "src/operators/operator.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@ -80,6 +82,15 @@ class RuleWithOperator : public RuleWithActions {
|
||||
return std::to_string(getId());
|
||||
}
|
||||
|
||||
virtual void dump(std::stringstream &out) override {
|
||||
Rule::dump(out);
|
||||
out << "# RuleWithOperator" << std::endl;
|
||||
out << "SecRule ";
|
||||
out << m_variables->getVariableNames() << " ";
|
||||
out << "\"" << "@" << m_operator->m_op << " " << m_operator->m_param << "\"";
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<modsecurity::variables::Variables> m_variables;
|
||||
std::shared_ptr<operators::Operator> m_operator;
|
||||
|
@ -57,13 +57,11 @@ std::shared_ptr<Rule> Rules::at(int index) const {
|
||||
}
|
||||
|
||||
|
||||
void Rules::dump() {
|
||||
for (int j = 0; j < m_rules.size(); j++) {
|
||||
std::cout << " Rule ID: " << m_rules.at(j)->getReference();
|
||||
std::cout << "--" << m_rules.at(j) << std::endl;
|
||||
void Rules::dump(std::stringstream &out) {
|
||||
for (auto &r : m_rules) {
|
||||
r->dump(out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace modsecurity
|
||||
|
||||
|
@ -91,6 +91,7 @@ int main(int argc, char **argv) {
|
||||
if (err.empty() == false) {
|
||||
std::cerr << " " << err << std::endl;
|
||||
}
|
||||
rules->dump();
|
||||
next:
|
||||
args++;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user