Skip to content

Commit 650f02d

Browse files
authored
Fix: audit logs RelevantOnly match if interruption happens (#1025)
* fix: auditlogs relevant only * mod tidy
1 parent dc77812 commit 650f02d

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

internal/corazawaf/transaction.go

+3
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,9 @@ func (tx *Transaction) ProcessLogging() {
12901290
if tx.AuditEngine == types.AuditEngineRelevantOnly && tx.audit {
12911291
re := tx.WAF.AuditLogRelevantStatus
12921292
status := tx.variables.responseStatus.Get()
1293+
if tx.IsInterrupted() {
1294+
status = strconv.Itoa(tx.interruption.Status)
1295+
}
12931296
if re != nil && !re.Match([]byte(status)) {
12941297
// Not relevant status
12951298
tx.debugLogger.Debug().

internal/corazawaf/transaction_test.go

+50-9
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,56 @@ func TestResetCapture(t *testing.T) {
709709
}
710710

711711
func TestRelevantAuditLogging(t *testing.T) {
712-
tx := makeTransaction(t)
713-
tx.WAF.AuditLogRelevantStatus = regexp.MustCompile(`(403)`)
714-
tx.variables.responseStatus.Set("403")
715-
tx.AuditEngine = types.AuditEngineRelevantOnly
716-
// tx.WAF.auditLogger = auditlog.NewAuditLogger()
717-
tx.ProcessLogging()
718-
// TODO how do we check if the log was writen?
719-
if err := tx.Close(); err != nil {
720-
t.Error(err)
712+
tests := []struct {
713+
name string
714+
status string
715+
interruption *types.Interruption
716+
relevantLog bool
717+
}{
718+
{
719+
name: "TestRelevantAuditLogging",
720+
status: "403",
721+
interruption: nil,
722+
relevantLog: true,
723+
},
724+
{
725+
name: "TestNotRelevantAuditLogging",
726+
status: "200",
727+
interruption: nil,
728+
relevantLog: false,
729+
},
730+
{
731+
name: "TestRelevantAuditLoggingWithInterruption",
732+
interruption: &types.Interruption{
733+
Status: 403,
734+
Action: "deny",
735+
},
736+
relevantLog: true,
737+
},
738+
}
739+
740+
for _, tt := range tests {
741+
t.Run(tt.name, func(t *testing.T) {
742+
tx := makeTransaction(t)
743+
debugLog := bytes.Buffer{}
744+
tx.debugLogger = debuglog.Default().WithLevel(debuglog.LevelDebug).WithOutput(&debugLog)
745+
tx.WAF.AuditLogRelevantStatus = regexp.MustCompile(`(403)`)
746+
tx.variables.responseStatus.Set(tt.status)
747+
tx.interruption = tt.interruption
748+
tx.AuditEngine = types.AuditEngineRelevantOnly
749+
tx.audit = true // Mimics that there is something to audit
750+
tx.ProcessLogging()
751+
// TODO how do we check if the log was written?
752+
if err := tx.Close(); err != nil {
753+
t.Error(err)
754+
}
755+
if tt.relevantLog && strings.Contains(debugLog.String(), "Transaction status not marked for audit logging") {
756+
t.Errorf("unexpected debug log: %q. Transaction status should be marked for audit logging", debugLog.String())
757+
}
758+
if !tt.relevantLog && !strings.Contains(debugLog.String(), "Transaction status not marked for audit logging") {
759+
t.Errorf("missing debug log. Transaction status should be not marked for audit logging not being relevant")
760+
}
761+
})
721762
}
722763
}
723764

0 commit comments

Comments
 (0)