Skip to content

Commit 8c714af

Browse files
committed
Actions refactoring: now there is a clear definiation on the action name
1 parent 1b88947 commit 8c714af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+431
-359
lines changed

src/actions/accuracy.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
namespace modsecurity {
2828
namespace actions {
2929

30-
Accuracy::Accuracy(std::string action)
31-
: Action(action, ConfigurationKind),
32-
m_accuracy_str(action) {
33-
if (m_accuracy_str.at(0) == '\'') {
34-
m_accuracy_str.erase(0, 1);
35-
m_accuracy_str.pop_back();
30+
31+
bool Accuracy::init(std::string *error) {
32+
try {
33+
m_accuracy = std::stoi(m_parser_payload);
34+
} catch (...) {
35+
error->assign("Accuracy: The input \"" + m_parser_payload + "\" is " \
36+
"not a number.");
37+
return false;
3638
}
37-
m_accuracy = std::stoi(m_accuracy_str);
39+
return true;
3840
}
3941

4042

@@ -43,5 +45,6 @@ bool Accuracy::evaluate(Rule *rule, Transaction *transaction) {
4345
return true;
4446
}
4547

48+
4649
} // namespace actions
4750
} // namespace modsecurity

src/actions/accuracy.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ namespace actions {
2929

3030
class Accuracy : public Action {
3131
public:
32-
explicit Accuracy(std::string action);
32+
explicit Accuracy(std::string action)
33+
: Action(action, ConfigurationKind),
34+
m_accuracy(0) { }
3335

3436
bool evaluate(Rule *rule, Transaction *transaction) override;
37+
bool init(std::string *error) override;
3538

3639
private:
37-
std::string m_accuracy_str;
3840
int m_accuracy;
3941
};
4042

src/actions/action.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool Action::evaluate(Rule *rule, Transaction *transaction) {
5252
}
5353

5454

55-
void Action::fill_intervention(ModSecurityIntervention *i) {
55+
void Action::fillIntervention(ModSecurityIntervention *i) {
5656
}
5757

5858
Action *Action::instantiate(const std::string& name) {

src/actions/action.h

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,43 @@ class Action {
3535
public:
3636
explicit Action(const std::string& _action)
3737
: action_kind(2),
38-
action(_action),
39-
name(_action),
38+
m_name(""),
39+
m_parser_payload(""),
4040
m_isNone(false),
4141
temporaryAction(false) {
42-
name.erase(0, 2);
42+
set_name_and_payload(_action);
4343
}
4444
explicit Action(const std::string& _action, int kind)
4545
: action_kind(kind),
46-
action(_action),
47-
name(_action),
46+
m_name(""),
47+
m_parser_payload(""),
4848
m_isNone(false),
4949
temporaryAction(false) {
50-
name.erase(0, 2);
50+
set_name_and_payload(_action);
5151
}
5252

53+
void set_name_and_payload(const std::string& data) {
54+
size_t pos = data.find(":");
55+
std::string t = "t:";
56+
57+
if (data.compare(0, t.length(), t) == 0) {
58+
pos = data.find(":", 2);
59+
}
60+
61+
if (pos == std::string::npos) {
62+
m_name = data;
63+
return;
64+
}
65+
66+
m_name = std::string(data, 0, pos);
67+
m_parser_payload = std::string(data, pos + 1, data.length());
68+
69+
if (m_parser_payload.at(0) == '\'' && m_parser_payload.size() > 2) {
70+
m_parser_payload.erase(0, 1);
71+
m_parser_payload.pop_back();
72+
}
73+
}
74+
5375
virtual ~Action() { }
5476
/**
5577
*
@@ -83,9 +105,6 @@ class Action {
83105
RunTimeOnlyIfMatchKind,
84106
};
85107

86-
std::string action;
87-
int action_kind;
88-
std::string name;
89108

90109
virtual std::string evaluate(std::string exp,
91110
Transaction *transaction);
@@ -94,14 +113,20 @@ class Action {
94113
RuleMessage *ruleMessage) {
95114
return evaluate(rule, transaction);
96115
}
116+
97117
virtual bool init(std::string *error) { return true; }
118+
98119
virtual bool isDisruptive() { return false; }
99120

121+
virtual void fillIntervention(ModSecurityIntervention *intervention);
122+
100123
static Action *instantiate(const std::string& name);
101124

102-
virtual void fill_intervention(ModSecurityIntervention *intervention);
103125
bool temporaryAction;
126+
std::string m_name;
127+
std::string m_parser_payload;
104128
bool m_isNone;
129+
int action_kind;
105130
};
106131

107132

src/actions/audit_log.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
namespace modsecurity {
2424
namespace actions {
2525

26+
2627
bool AuditLog::evaluate(Rule *rule, Transaction *transaction) {
2728
transaction->m_toBeSavedInAuditlogs = true;
2829
return true;
2930
}
3031

32+
3133
} // namespace actions
3234
} // namespace modsecurity

src/actions/audit_log.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class AuditLog : public Action {
3737
bool evaluate(Rule *rule, Transaction *transaction) override;
3838
};
3939

40+
4041
} // namespace actions
4142
} // namespace modsecurity
4243
#endif

src/actions/block.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
namespace modsecurity {
2626
namespace actions {
2727

28-
Block::Block(std::string action)
29-
: Action(action) {
30-
this->action = action;
31-
this->action_kind = 2;
32-
}
33-
3428

3529
bool Block::evaluate(Rule *rule, Transaction *transaction) {
3630
#ifndef NO_LOGS
@@ -44,9 +38,11 @@ bool Block::evaluate(Rule *rule, Transaction *transaction) {
4438
return true;
4539
}
4640

47-
void Block::fill_intervention(ModSecurityIntervention *i) {
41+
42+
void Block::fillIntervention(ModSecurityIntervention *i) {
4843
i->disruptive = true;
4944
}
5045

46+
5147
} // namespace actions
5248
} // namespace modsecurity

src/actions/block.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ namespace actions {
3131

3232
class Block : public Action {
3333
public:
34-
explicit Block(std::string action);
34+
explicit Block(std::string action) : Action(action) { }
3535

3636
bool evaluate(Rule *rule, Transaction *transaction) override;
37-
void fill_intervention(ModSecurityIntervention *i) override;
37+
void fillIntervention(ModSecurityIntervention *i) override;
3838
bool isDisruptive() override { return true; }
3939
};
4040

41+
4142
} // namespace actions
4243
} // namespace modsecurity
4344
#endif

src/actions/capture.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
namespace modsecurity {
3232
namespace actions {
3333

34+
3435
bool Capture::evaluate(Rule *rule, Transaction *transaction) {
3536
if (transaction->m_matched.empty()) {
3637
return false;
@@ -46,5 +47,6 @@ bool Capture::evaluate(Rule *rule, Transaction *transaction) {
4647
return true;
4748
}
4849

50+
4951
} // namespace actions
5052
} // namespace modsecurity

src/actions/chain.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ namespace modsecurity {
2525
namespace actions {
2626

2727

28-
2928
bool Chain::evaluate(Rule *rule, Transaction *transaction) {
3029
rule->chained = true;
3130
return true;
3231
}
3332

33+
3434
} // namespace actions
3535
} // namespace modsecurity

0 commit comments

Comments
 (0)