Skip to content

Commit

Permalink
chore: deletes content temporary file on close.
Browse files Browse the repository at this point in the history
Fixes #922.
  • Loading branch information
jcchavezs committed Nov 21, 2023
1 parent 48cf923 commit 5a56bbd
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 107 deletions.
12 changes: 12 additions & 0 deletions internal/collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func (c *Map) Get(key string) []string {
return values
}

func (c *Map) Keys() []string {
if len(c.data) == 0 {
return nil
}

var keys = make(([]string), 0, len(c.data))
for k := range c.data {
keys = append(keys, k)
}
return keys
}

func (c *Map) FindRegex(key *regexp.Regexp) []types.MatchData {
var result []types.MatchData
for k, data := range c.data {
Expand Down
26 changes: 18 additions & 8 deletions internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"mime"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/corazawaf/coraza/v3/internal/collections"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/internal/corazatypes"
"github.com/corazawaf/coraza/v3/internal/environment"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
urlutil "github.com/corazawaf/coraza/v3/internal/url"
"github.com/corazawaf/coraza/v3/types"
Expand Down Expand Up @@ -1448,12 +1450,23 @@ func (tx *Transaction) AuditLog() *auditlog.Log {
func (tx *Transaction) Close() error {
defer tx.WAF.txPool.Put(tx)
tx.variables.reset()

var errs []error
if err := tx.requestBodyBuffer.Reset(); err != nil {
errs = append(errs, err)
errs = append(errs, fmt.Errorf("reseting request body buffer: %v", err))
}
if err := tx.responseBodyBuffer.Reset(); err != nil {
errs = append(errs, err)
errs = append(errs, fmt.Errorf("reseting response body buffer: %v", err))
}

if environment.HasAccessToFS {
for _, k := range tx.variables.filesTmpContent.Keys() {
for _, tmpContent := range tx.variables.filesTmpContent.Get(k) {
if err := os.Remove(tmpContent); err != nil {
errs = append(errs, fmt.Errorf("deleting content temporary file: %v", err))
}
}
}
}

if tx.IsInterrupted() {
Expand All @@ -1468,14 +1481,11 @@ func (tx *Transaction) Close() error {
Msg("Transaction finished")
}

switch {
case len(errs) == 0:
if len(errs) == 0 {
return nil
case len(errs) == 1:
return fmt.Errorf("transaction close failed: %s", errs[0].Error())
default:
return fmt.Errorf("transaction close failed:\n- %s\n- %s", errs[0].Error(), errs[1].Error())
}

return fmt.Errorf("transaction close failed: %v", errors.Join(errs...))

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join) (typecheck)

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join) (typecheck)

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join) (typecheck)

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join

Check failure on line 1488 in internal/corazawaf/transaction.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: errors.Join
}

// String will return a string with the transaction debug information
Expand Down
Loading

0 comments on commit 5a56bbd

Please sign in to comment.