Skip to content

Commit e9adb6c

Browse files
committed
Computes auditlog during rules load time
1 parent c903250 commit e9adb6c

18 files changed

+195
-92
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
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+
- auditlog: Computes whether or not to save while loading the rules.
5+
[@zimmerle]
46
- actions: Computes Rule association while loading the rules given a
57
performance boost on run time.
68
[@zimmerle, @martinhsv, @WGH-]

headers/modsecurity/actions/action.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ namespace actions {
3333
class Action {
3434
public:
3535
Action()
36-
: m_name(""),
37-
m_parserPayload("")
36+
: m_parserPayload(""),
37+
m_name("")
3838
{ }
3939

4040

4141
explicit Action(const std::string& action)
42-
: m_name(sort_name(action)),
43-
m_parserPayload(sort_payload(action))
42+
: m_parserPayload(sort_payload(action)),
43+
m_name(sort_name(action))
4444
{ }
4545

4646

4747
Action(const Action &a)
48-
: m_name(a.m_name),
49-
m_parserPayload(a.m_parserPayload)
48+
: m_parserPayload(a.m_parserPayload),
49+
m_name(a.m_name)
5050
{ }
5151

5252

@@ -76,7 +76,7 @@ class Action {
7676
}
7777

7878

79-
const std::string *getName() {
79+
const std::string *getName() const {
8080
return &m_name;
8181
}
8282

headers/modsecurity/audit_log.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@ class AuditLog {
170170
bool init(std::string *error);
171171
virtual bool close();
172172

173-
bool saveIfRelevant(Transaction *transaction);
174-
bool saveIfRelevant(Transaction *transaction, int parts);
175-
bool isRelevant(int status);
173+
bool saveIfRelevant(Transaction *transaction) const noexcept;
174+
bool isRelevant(int status) const noexcept;
176175

177176
static int addParts(int parts, const std::string& new_parts);
178177
static int removeParts(int parts, const std::string& new_parts);

headers/modsecurity/rule_message.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class RuleMessage {
130130
std::string getUri() const;
131131
bool isDisruptive() const;
132132

133+
bool toBeAuditLog() const;
134+
133135
int m_severity;
134136
std::list<std::string> m_tags;
135137

headers/modsecurity/transaction.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ class TransactionSecMarkerManagement {
322322
class TransactionRuleMessageManagement {
323323
public:
324324
explicit TransactionRuleMessageManagement(Transaction *t)
325-
: m_transaction(t),
326-
m_noAuditLog(false) {
325+
: m_transaction(t) {
327326
messageNew();
328327
};
329328

@@ -332,22 +331,7 @@ class TransactionRuleMessageManagement {
332331

333332
void logMatchLastRuleOnTheChain(RuleWithActions *rule);
334333

335-
void messageSetNoAuditLog(bool a) {
336-
m_noAuditLog = a;
337-
}
338-
339-
bool messageSaveAuditLog() const {
340-
return m_noAuditLog;
341-
}
342-
343-
std::list<RuleMessage *> messageGetAll() {
344-
std::list<RuleMessage *> messages;
345-
for (RuleMessage *a : m_rulesMessages) {
346-
messages.push_back(a);
347-
}
348-
349-
return messages;
350-
}
334+
std::list<RuleMessage *> messageGetAll();
351335

352336
void messageClear() {
353337
m_rulesMessages.clear();
@@ -362,7 +346,6 @@ class TransactionRuleMessageManagement {
362346
std::list<RuleMessage *> m_rulesMessages;
363347

364348
Transaction *m_transaction;
365-
bool m_noAuditLog;
366349
};
367350

368351

src/actions/audit_log.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@
1515

1616
#include "src/actions/audit_log.h"
1717

18-
#include <string>
19-
20-
#include "modsecurity/transaction.h"
21-
2218

2319
namespace modsecurity {
2420
namespace actions {
2521

2622

27-
bool AuditLog::execute(Transaction *transaction) noexcept {
28-
transaction->messageSetNoAuditLog(false);
29-
return true;
30-
}
31-
32-
3323
} // namespace actions
3424
} // namespace modsecurity

src/actions/audit_log.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
#include "src/actions/action_allowed_in_sec_default_action.h"
1818

19+
#include "src/actions/action_type_rule_metadata.h"
20+
#include "src/actions/action_allowed_in_sec_default_action.h"
21+
22+
1923
#ifndef SRC_ACTIONS_AUDIT_LOG_H_
2024
#define SRC_ACTIONS_AUDIT_LOG_H_
2125

@@ -24,13 +28,16 @@ namespace modsecurity {
2428
namespace actions {
2529

2630

27-
class AuditLog : public ActionAllowedAsSecDefaultAction {
31+
class AuditLog : public ActionTypeRuleMetaData,
32+
public ActionAllowedAsSecDefaultAction {
2833
public:
2934
AuditLog()
3035
: Action("auditLog")
3136
{ }
3237

33-
bool execute(Transaction *transaction) noexcept override;
38+
void configure(RuleWithActions *rule) override {
39+
rule->setHasAuditLogAction(true);
40+
}
3441
};
3542

3643

src/actions/no_audit_log.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,10 @@
1616

1717
#include "src/actions/no_audit_log.h"
1818

19-
#include "modsecurity/transaction.h"
20-
2119

2220
namespace modsecurity {
2321
namespace actions {
2422

2523

26-
bool NoAuditLog::execute(Transaction *transaction) noexcept {
27-
transaction->messageSetNoAuditLog(true);
28-
return true;
29-
}
30-
31-
3224
} // namespace actions
3325
} // namespace modsecurity

src/actions/no_audit_log.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616

1717
#include "modsecurity/actions/action.h"
18-
#include "modsecurity/transaction.h"
18+
19+
#include "src/actions/action_type_rule_metadata.h"
1920
#include "src/actions/action_allowed_in_sec_default_action.h"
2021

2122

@@ -27,13 +28,16 @@ namespace modsecurity {
2728
namespace actions {
2829

2930

30-
class NoAuditLog : public ActionAllowedAsSecDefaultAction {
31+
class NoAuditLog : public ActionTypeRuleMetaData,
32+
public ActionAllowedAsSecDefaultAction {
3133
public:
3234
NoAuditLog()
3335
: Action("noAuditLog")
3436
{ }
3537

36-
bool execute(Transaction *transaction) noexcept override;
38+
void configure(RuleWithActions *rule) override {
39+
rule->setHasNoAuditLogAction(true);
40+
}
3741
};
3842

3943

src/audit_log/audit_log.cc

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,13 @@ bool AuditLog::init(std::string *error) {
266266
}
267267

268268

269-
bool AuditLog::isRelevant(int status) {
269+
bool AuditLog::isRelevant(int status) const noexcept {
270270
std::string sstatus = std::to_string(status);
271271

272272
if (m_relevant.empty()) {
273273
return false;
274274
}
275275

276-
277276
if (sstatus.empty()) {
278277
return true;
279278
}
@@ -283,45 +282,34 @@ bool AuditLog::isRelevant(int status) {
283282
}
284283

285284

286-
bool AuditLog::saveIfRelevant(Transaction *transaction) {
287-
return saveIfRelevant(transaction, -1);
288-
}
289-
290-
291-
bool AuditLog::saveIfRelevant(Transaction *transaction, int parts) {
292-
bool saveAnyway = false;
285+
bool AuditLog::saveIfRelevant(Transaction *transaction) const noexcept {
293286
if (m_status == OffAuditLogStatus || m_status == NotSetLogStatus) {
294287
ms_dbg_a(transaction, 5, "Audit log engine was not set.");
295-
return true;
288+
return false;
296289
}
297290

298-
saveAnyway = transaction->messageSaveAuditLog();
299-
300291
if ((m_status == RelevantOnlyAuditLogStatus
301-
&& this->isRelevant(transaction->m_httpCodeReturned) == false)
302-
&& saveAnyway == false) {
292+
&& isRelevant(transaction->m_httpCodeReturned) == false)) {
303293
ms_dbg_a(transaction, 9, "Return code `" +
304294
std::to_string(transaction->m_httpCodeReturned) + "'" \
305295
" is not interesting to audit logs, relevant code(s): `" +
306296
m_relevant + "'.");
307-
308297
return false;
309298
}
310299

311-
if (parts == -1) {
312-
parts = m_parts;
313-
}
314300
ms_dbg_a(transaction, 5, "Saving this request as part " \
315301
"of the audit logs.");
302+
316303
if (m_writer == NULL) {
317304
ms_dbg_a(transaction, 1, "Internal error, audit log writer is null");
318-
} else {
319-
std::string error;
320-
bool a = m_writer->write(transaction, parts, &error);
321-
if (a == false) {
322-
ms_dbg_a(transaction, 1, "Cannot save the audit log: " + error);
323-
return false;
324-
}
305+
return false;
306+
}
307+
308+
std::string error;
309+
bool a = m_writer->write(transaction, transaction->m_auditLogParts, &error);
310+
if (a == false) {
311+
ms_dbg_a(transaction, 1, "Cannot save the audit log: " + error);
312+
return false;
325313
}
326314

327315
return true;

src/parser/driver.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ int Driver::addSecRule(std::unique_ptr<RuleWithActions> r) {
157157
firstRule->getChainedParent()->setHasLogAction(
158158
firstRule->hasNoLogAction()
159159
);
160+
firstRule->getChainedParent()->setHasAuditLogAction(
161+
firstRule->hasAuditLogAction()
162+
);
163+
firstRule->getChainedParent()->setHasNoAuditLogAction(
164+
firstRule->hasNoAuditLogAction()
165+
);
160166
firstRule = firstRule->getChainedParent();
161167
}
162168
}

src/rule_message.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ int RuleMessage::getAccuracy() const {
176176
}
177177

178178

179+
bool RuleMessage::toBeAuditLog() const {
180+
if (m_rule) {
181+
return m_rule->isItToBeAuditLogged();
182+
}
183+
return false;
184+
}
185+
186+
179187
std::string RuleMessage::getClientIpAddress() const {
180188
if (m_transaction) {
181189
return *m_transaction->m_clientIpAddress.get();

src/rule_with_actions.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ RuleWithActions::RuleWithActions(
9090
m_containsCaptureAction(false),
9191
m_containsLogAction(false),
9292
m_containsNoLogAction(false),
93+
m_containsAuditLogAction(false),
94+
m_containsNoAuditLogAction(false),
9395
m_containsMultiMatchAction(false),
9496
m_containsStaticBlockAction(false),
9597
m_defaultSeverity(SEVERITY_NOT_SET),
@@ -100,6 +102,8 @@ RuleWithActions::RuleWithActions(
100102
m_defaultContainsCaptureAction(false),
101103
m_defaultContainsLogAction(false),
102104
m_defaultContainsNoLogAction(false),
105+
m_defaultContainsAuditLogAction(false),
106+
m_defaultContainsNoAuditLogAction(false),
103107
m_defaultContainsMultiMatchAction(false),
104108
m_defaultContainsStaticBlockAction(false),
105109
m_isChained(false) {

0 commit comments

Comments
 (0)