From e1d33085888ccb17369673c5de72aa3fb4f76be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Tue, 4 Jun 2024 10:13:42 +0200 Subject: [PATCH] fix: eagerly closes transactions when needed. --- main.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 91965e1..3da9717 100644 --- a/main.go +++ b/main.go @@ -155,13 +155,18 @@ func handleRequest(req api.Request, res api.Response) (next bool, reqCtx uint32) // Early return, Coraza is not going to process any rule if tx.IsRuleEngineOff() { - return true, 0 + next = true + tx.Close() + return } defer func() { if tx.IsInterrupted() { // We run phase 5 rules and create audit logs (if enabled) tx.ProcessLogging() + } + + if !next { // we remove temporary files and free some memory if err := tx.Close(); err != nil { tx.DebugLogger().Error().Err(err).Msg("Failed to close the transaction") @@ -204,7 +209,7 @@ func handleRequest(req api.Request, res api.Response) (next bool, reqCtx uint32) it = tx.ProcessRequestHeaders() if it != nil { handleInterruption(it, res) - return false, 0 + return } if tx.IsRequestBodyAccessible() { @@ -213,24 +218,26 @@ func handleRequest(req api.Request, res api.Response) (next bool, reqCtx uint32) // regular flow. it, _, err := tx.ReadRequestBodyFrom(readWriterTo{req.Body()}) if err != nil { - return false, 0 + tx.DebugLogger().Error().Err(err).Msg("Failed to read request body") + return } if it != nil { handleInterruption(it, res) - return false, 0 + return } } var err error it, err = tx.ProcessRequestBody() if err != nil { - return false, 0 + tx.DebugLogger().Error().Err(err).Msg("Failed to process request body") + return } if it != nil { handleInterruption(it, res) - return false, 0 + return } reqCtx = rand.Uint32()