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: lazy load regexes to save memory. #900

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package collection

import (
"regexp"
"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/types"
)
Expand Down Expand Up @@ -37,7 +37,7 @@ type Keyed interface {
Get(key string) []string

// FindRegex returns a slice of MatchData for the regex
FindRegex(key *regexp.Regexp) []types.MatchData
FindRegex(key regexp.Regexp) []types.MatchData

// FindString returns a slice of MatchData for the string
FindString(key string) []types.MatchData
Expand Down
5 changes: 3 additions & 2 deletions internal/collections/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package collections

import (
"regexp"
"strings"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (c *ConcatKeyed) Get(key string) []string {
}

// FindRegex returns a slice of MatchData for the regex
func (c *ConcatKeyed) FindRegex(key *regexp.Regexp) []types.MatchData {
func (c *ConcatKeyed) FindRegex(key regexp.Regexp) []types.MatchData {
var res []types.MatchData
for _, d := range c.data {
res = append(res, replaceVariable(c.variable, d.FindRegex(key))...)
Expand Down
3 changes: 2 additions & 1 deletion internal/collections/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package collections

import (
"regexp"
"strings"
"testing"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down
5 changes: 3 additions & 2 deletions internal/collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package collections

import (
"regexp"
"strings"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
Expand Down Expand Up @@ -40,7 +41,7 @@ func (c *Map) Get(key string) []string {
return values
}

func (c *Map) FindRegex(key *regexp.Regexp) []types.MatchData {
func (c *Map) FindRegex(key regexp.Regexp) []types.MatchData {
var result []types.MatchData
for k, data := range c.data {
if key.MatchString(k) {
Expand Down
3 changes: 2 additions & 1 deletion internal/collections/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ package collections

import (
"fmt"
"regexp"
"testing"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/types/variables"
)

Expand Down
5 changes: 3 additions & 2 deletions internal/collections/named.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

import (
"fmt"
"regexp"
"strings"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
Expand Down Expand Up @@ -94,7 +95,7 @@
collection *NamedCollection
}

func (c *NamedCollectionNames) FindRegex(key *regexp.Regexp) []types.MatchData {
func (c *NamedCollectionNames) FindRegex(key regexp.Regexp) []types.MatchData {

Check warning on line 98 in internal/collections/named.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/named.go#L98

Added line #L98 was not covered by tests
panic("selection operator not supported")
}

Expand Down
3 changes: 2 additions & 1 deletion internal/collections/named_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package collections

import (
"fmt"
"regexp"
"testing"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/types/variables"
)

Expand Down
5 changes: 3 additions & 2 deletions internal/collections/sized.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
Expand All @@ -32,7 +33,7 @@
}

// FindRegex returns a slice of MatchData for the regex
func (c *SizeCollection) FindRegex(*regexp.Regexp) []types.MatchData {
func (c *SizeCollection) FindRegex(regexp.Regexp) []types.MatchData {

Check warning on line 36 in internal/collections/sized.go

View check run for this annotation

Codecov / codecov/patch

internal/collections/sized.go#L36

Added line #L36 was not covered by tests
return c.FindAll()
}

Expand Down
15 changes: 8 additions & 7 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ package corazawaf
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"unsafe"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/experimental/plugins/macro"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/corazarules"
Expand Down Expand Up @@ -50,7 +51,7 @@ type ruleVariableException struct {

// The key for the variable that is going to be requested
// If nil, KeyStr is going to be used
KeyRx *regexp.Regexp
KeyRx regexp.Regexp
}

// RuleVariable is compiled during runtime by transactions
Expand All @@ -65,7 +66,7 @@ type ruleVariableParams struct {

// The key for the variable that is going to be requested
// If nil, KeyStr is going to be used
KeyRx *regexp.Regexp
KeyRx regexp.Regexp

// The string key for the variable that is going to be requested
// If KeyRx is not nil, KeyStr is ignored
Expand Down Expand Up @@ -454,14 +455,14 @@ func (r *Rule) AddAction(name string, action plugintypes.Action) error {
// it will be used to match the variable, in case of string it will
// be a fixed match, in case of nil it will match everything
func (r *Rule) AddVariable(v variables.RuleVariable, key string, iscount bool) error {
var re *regexp.Regexp
var re regexp.Regexp
if len(key) > 2 && key[0] == '/' && key[len(key)-1] == '/' {
key = key[1 : len(key)-1]

if vare, err := memoize.Do(key, func() (interface{}, error) { return regexp.Compile(key) }); err != nil {
return err
} else {
re = vare.(*regexp.Regexp)
re = vare.(regexp.Regexp)
}
}

Expand Down Expand Up @@ -524,13 +525,13 @@ func (r *Rule) AddVariable(v variables.RuleVariable, key string, iscount bool) e
// OK: SecRule !ARGS:id "..."
// ERROR: SecRule !ARGS: "..."
func (r *Rule) AddVariableNegation(v variables.RuleVariable, key string) error {
var re *regexp.Regexp
var re regexp.Regexp
if len(key) > 2 && key[0] == '/' && key[len(key)-1] == '/' {
key = key[1 : len(key)-1]
if vare, err := memoize.Do(key, func() (interface{}, error) { return regexp.Compile(key) }); err != nil {
return err
} else {
re = vare.(*regexp.Regexp)
re = vare.(regexp.Regexp)
}
}
// Prevent sigsev
Expand Down
3 changes: 2 additions & 1 deletion internal/corazawaf/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"bytes"
"fmt"
"io"
"regexp"
"runtime/debug"
"strconv"
"strings"
"testing"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/debuglog"
"github.com/corazawaf/coraza/v3/experimental/plugins/macro"
Expand Down
5 changes: 3 additions & 2 deletions internal/corazawaf/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"io"
"io/fs"
"os"
"regexp"
"strconv"
"strings"
"time"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/debuglog"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/auditlog"
Expand Down Expand Up @@ -119,7 +120,7 @@ type WAF struct {
AuditLogParts types.AuditLogParts

// Contains the regular expression for relevant status audit logging
AuditLogRelevantStatus *regexp.Regexp
AuditLogRelevantStatus regexp.Regexp

auditLogWriter plugintypes.AuditLogWriter

Expand Down
7 changes: 4 additions & 3 deletions internal/operators/restpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ package operators

import (
"fmt"
"regexp"
"strings"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/memoize"
)
Expand All @@ -21,7 +22,7 @@ var rePathTokenRe = regexp.MustCompile(`\{([^\}]+)\}`)
// It will later transform the path to a regex and assign the variables to
// ARGS_PATH
type restpath struct {
re *regexp.Regexp
re regexp.Regexp
}

var _ plugintypes.Operator = (*restpath)(nil)
Expand All @@ -36,7 +37,7 @@ func newRESTPath(options plugintypes.OperatorOptions) (plugintypes.Operator, err
if err != nil {
return nil, err
}
return &restpath{re: re.(*regexp.Regexp)}, nil
return &restpath{re: re.(regexp.Regexp)}, nil
}

func (o *restpath) Evaluate(tx plugintypes.TransactionState, value string) bool {
Expand Down
7 changes: 4 additions & 3 deletions internal/operators/rx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ package operators

import (
"fmt"
"regexp"
"strconv"
"unicode/utf8"

"github.com/corazawaf/coraza/v3/internal/regexp"

"rsc.io/binaryregexp"

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/memoize"
)

type rx struct {
re *regexp.Regexp
re regexp.Regexp
}

var _ plugintypes.Operator = (*rx)(nil)
Expand All @@ -40,7 +41,7 @@ func newRX(options plugintypes.OperatorOptions) (plugintypes.Operator, error) {
if err != nil {
return nil, err
}
return &rx{re: re.(*regexp.Regexp)}, nil
return &rx{re: re.(regexp.Regexp)}, nil
}

func (o *rx) Evaluate(tx plugintypes.TransactionState, value string) bool {
Expand Down
3 changes: 2 additions & 1 deletion internal/operators/rx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package operators

import (
"fmt"
"regexp"
"testing"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
)
Expand Down
7 changes: 4 additions & 3 deletions internal/operators/validate_nid.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/corazawaf/coraza/v3/internal/regexp"

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/memoize"
)
Expand All @@ -19,7 +20,7 @@

type validateNid struct {
fn validateNidFunction
re *regexp.Regexp
re regexp.Regexp
}

var _ plugintypes.Operator = (*validateNid)(nil)
Expand All @@ -46,7 +47,7 @@
return nil, err
}

return &validateNid{fn: fn, re: re.(*regexp.Regexp)}, nil
return &validateNid{fn: fn, re: re.(regexp.Regexp)}, nil

Check warning on line 50 in internal/operators/validate_nid.go

View check run for this annotation

Codecov / codecov/patch

internal/operators/validate_nid.go#L50

Added line #L50 was not covered by tests
}

func (o *validateNid) Evaluate(tx plugintypes.TransactionState, value string) bool {
Expand Down
Loading
Loading