Skip to content
Open
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
11 changes: 5 additions & 6 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const chainLevelZero = 0
// Evaluate will evaluate the current rule for the indicated transaction
// If the operator matches, actions will be evaluated, and it will return
// the matched variables, keys and values (MatchData)
func (r *Rule) Evaluate(phase types.RulePhase, tx plugintypes.TransactionState, cache map[transformationKey]*transformationValue) {
func (r *Rule) Evaluate(phase types.RulePhase, tx plugintypes.TransactionState, cache map[transformationKey]transformationValue) {
// collectiveMatchedValues lives across recursive calls of doEvaluate
var collectiveMatchedValues []types.MatchData

Expand All @@ -180,7 +180,7 @@ func (r *Rule) Evaluate(phase types.RulePhase, tx plugintypes.TransactionState,

const noID = 0

func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Transaction, collectiveMatchedValues *[]types.MatchData, chainLevel int, cache map[transformationKey]*transformationValue) []types.MatchData {
func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Transaction, collectiveMatchedValues *[]types.MatchData, chainLevel int, cache map[transformationKey]transformationValue) []types.MatchData {
tx.Capture = r.Capture

if multiphaseEvaluation {
Expand Down Expand Up @@ -397,7 +397,7 @@ func (r *Rule) transformMultiMatchArg(arg types.MatchData) ([]string, []error) {
return r.executeTransformationsMultimatch(arg.Value())
}

func (r *Rule) transformArg(arg types.MatchData, argIdx int, cache map[transformationKey]*transformationValue) (string, []error) {
func (r *Rule) transformArg(arg types.MatchData, argIdx int, cache map[transformationKey]transformationValue) (string, []error) {
switch {
case len(r.transformations) == 0:
return arg.Value(), nil
Expand All @@ -419,12 +419,11 @@ func (r *Rule) transformArg(arg types.MatchData, argIdx int, cache map[transform
return cached.arg, cached.errs
} else {
ars, es := r.executeTransformations(arg.Value())
errs := es
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very curious about this, I think the idea is to avoid mutations of the errors but I am not sure why.

cache[key] = &transformationValue{
cache[key] = transformationValue{
arg: ars,
errs: es,
}
return ars, errs
return ars, es
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/corazawaf/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func TestExecuteTransformationsMultiMatchReturnsMultipleErrors(t *testing.T) {
}

func TestTransformArgSimple(t *testing.T) {
transformationCache := map[transformationKey]*transformationValue{}
transformationCache := map[transformationKey]transformationValue{}
md := &corazarules.MatchData{
Variable_: variables.RequestURI,
Key_: "REQUEST_URI",
Expand Down Expand Up @@ -609,7 +609,7 @@ func TestTransformArgSimple(t *testing.T) {
}

func TestTransformArgNoCacheForTXVariable(t *testing.T) {
transformationCache := map[transformationKey]*transformationValue{}
transformationCache := map[transformationKey]transformationValue{}
md := &corazarules.MatchData{
Variable_: variables.TX,
Key_: "Custom_TX_Variable",
Expand Down
2 changes: 1 addition & 1 deletion internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type Transaction struct {

variables TransactionVariables

transformationCache map[transformationKey]*transformationValue
transformationCache map[transformationKey]transformationValue
}

func (tx *Transaction) ID() string {
Expand Down
44 changes: 44 additions & 0 deletions internal/corazawaf/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"github.com/corazawaf/coraza/v3/internal/collections"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/internal/environment"
"github.com/corazawaf/coraza/v3/internal/operators"
utils "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/internal/transformations"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -1864,3 +1866,45 @@ func TestRequestFilename(t *testing.T) {
})
}
}

func BenchmarkRuleEvalWithTransformations(b *testing.B) {
waf := NewWAF()
op, err := operators.Get("unconditionalMatch", plugintypes.OperatorOptions{})
if err != nil {
b.Fatal(err)
}
lowercaseFn, err := transformations.GetTransformation("lowercase")
if err != nil {
b.Fatal(err)
}

rule := NewRule()
rule.ID_ = 1000
rule.LogID_ = "1000"
rule.Phase_ = types.PhaseRequestHeaders
rule.operator = &ruleOperatorParams{
Operator: op,
Function: "@unconditionalMatch",
}
if err := rule.AddTransformation("lowercase", lowercaseFn); err != nil {
b.Fatal(err)
}
rule.variables = append(rule.variables, ruleVariableParams{
Variable: variables.Args,
})
if err := waf.Rules.Add(rule); err != nil {
b.Fatal(err)
}

tx := waf.NewTransaction()
tx.ProcessURI("/test?a=1&b=2&c=3&d=4&e=5", "GET", "HTTP/1.1")
tx.AddRequestHeader("Host", "example.com")
tx.ProcessRequestHeaders()
defer tx.Close()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
waf.Rules.Eval(types.PhaseRequestHeaders, tx)
}
}
2 changes: 1 addition & 1 deletion internal/corazawaf/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (w *WAF) newTransaction(opts Options) *Transaction {
})

tx.variables = *NewTransactionVariables()
tx.transformationCache = map[transformationKey]*transformationValue{}
tx.transformationCache = map[transformationKey]transformationValue{}
}

// set capture variables
Expand Down
Loading