Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: deletes content temporary file on close. #924

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math"
"mime"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/corazawaf/coraza/v3/internal/cookies"
"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 @@ -1472,13 +1474,25 @@ func (tx *Transaction) AuditLog() *auditlog.Log {
// It also allows caches the transaction back into the sync.Pool
func (tx *Transaction) Close() error {
defer tx.WAF.txPool.Put(tx)
tx.variables.reset()

var errs []error
if environment.HasAccessToFS {
// TODO(jcchavezs): filesTmpNames should probably be a new kind of collection that
// is aware of the files and then attempt to delete them when the collection
// is resetted or an item is removed.
for _, file := range tx.variables.filesTmpNames.Get("") {
if err := os.Remove(file); err != nil {
errs = append(errs, fmt.Errorf("removing temporary file: %v", err))
}
}
}

tx.variables.reset()
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 tx.IsInterrupted() {
Expand All @@ -1493,14 +1507,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...))
}

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