Skip to content

Commit

Permalink
chore: improves API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Nov 14, 2023
1 parent aedfc50 commit 22ba28d
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 45 deletions.
1 change: 0 additions & 1 deletion collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package collection

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

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

Expand Down
6 changes: 5 additions & 1 deletion experimental/regexp.go → experimental/regexp/regexp.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package experimental

import (
"fmt"

"github.com/corazawaf/coraza/v3/experimental/regexp/regexptypes"
"github.com/corazawaf/coraza/v3/internal/regexp"
)

// SetRegexpCompiler sets the regex compiler used by the WAF. This is specially
// useful when we want to lazily compile regexes in a mono thread environment as
// we don't need to synchronize the regex compilation.
func SetRegexpCompiler(fn func(expr string) (regexp.Regexp, error)) {
func SetRegexpCompiler(fn func(expr string) (regexptypes.Regexp, error)) {
if fn == nil {
fmt.Println("invalid regex compiler")
return
Expand Down
19 changes: 19 additions & 0 deletions experimental/regexp/regexptypes/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package regexptypes

import "regexp"

// Regexp is the interface that wraps the basic MatchString, FindStringSubmatch,
// FindAllStringSubmatch, SubexpNames, Match and String methods.
type Regexp interface {
MatchString(s string) bool
FindStringSubmatch(s string) []string
FindAllStringSubmatch(s string, n int) [][]string
SubexpNames() []string
Match(s []byte) bool
String() string
}

var _ Regexp = (*regexp.Regexp)(nil)
3 changes: 1 addition & 2 deletions internal/collections/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ package collections
import (
"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/internal/regexp"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down
1 change: 0 additions & 1 deletion internal/collections/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

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

"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down
3 changes: 1 addition & 2 deletions internal/collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ package collections
import (
"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/internal/regexp"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down
1 change: 0 additions & 1 deletion internal/collections/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

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

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

Expand Down
3 changes: 1 addition & 2 deletions internal/collections/named.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"fmt"
"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/internal/regexp"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down
1 change: 0 additions & 1 deletion internal/collections/named_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

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

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

Expand Down
3 changes: 1 addition & 2 deletions internal/collections/sized.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"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/internal/regexp"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down
3 changes: 1 addition & 2 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import (
"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"
"github.com/corazawaf/coraza/v3/internal/memoize"
"github.com/corazawaf/coraza/v3/internal/regexp"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down
3 changes: 1 addition & 2 deletions internal/corazawaf/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import (
"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"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/collections"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/internal/regexp"
utils "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
Expand Down
3 changes: 1 addition & 2 deletions internal/corazawaf/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import (
"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"
"github.com/corazawaf/coraza/v3/internal/environment"
"github.com/corazawaf/coraza/v3/internal/regexp"
stringutils "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/internal/sync"
"github.com/corazawaf/coraza/v3/types"
Expand Down
3 changes: 1 addition & 2 deletions internal/operators/restpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"fmt"
"strings"

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

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

var rePathTokenRe = regexp.MustCompile(`\{([^\}]+)\}`)
Expand Down
3 changes: 1 addition & 2 deletions internal/operators/rx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"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"
"github.com/corazawaf/coraza/v3/internal/regexp"
)

type rx struct {
Expand Down
3 changes: 1 addition & 2 deletions internal/operators/rx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"fmt"
"testing"

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

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

func TestRx(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/operators/validate_nid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"strconv"
"strings"

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

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

type validateNidFunction = func(input string) bool
Expand Down
27 changes: 13 additions & 14 deletions internal/regexp/regex.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package regexp

import (
"regexp"

"github.com/corazawaf/coraza/v3/experimental/regexp/regexptypes"
)

var RegexCompiler func(expr string) (Regexp, error)
var RegexCompiler func(expr string) (regexptypes.Regexp, error)

func init() {
RegexCompiler = func(expr string) (Regexp, error) {
RegexCompiler = func(expr string) (regexptypes.Regexp, error) {
return regexp.Compile(expr)
}
}

type Regexp = regexptypes.Regexp

// MustCompile is like Compile but panics if the expression cannot be parsed.
// It is not intented to use with user input e.g. rules because it panics and
// bypasses whatever logic provided by the users for regex compilation.
func MustCompile(str string) *regexp.Regexp {
return regexp.MustCompile(str)
}

type Regexp interface {
MatchString(s string) bool
FindStringSubmatch(s string) []string
FindAllStringSubmatch(s string, n int) [][]string
SubexpNames() []string
Match(s []byte) bool
String() string
}

func Compile(expr string) (Regexp, error) {
func Compile(expr string) (regexptypes.Regexp, error) {
return RegexCompiler(expr)
}

var _ Regexp = (*regexp.Regexp)(nil)
3 changes: 3 additions & 0 deletions internal/regexp/regex_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package regexp

import (
Expand Down
3 changes: 1 addition & 2 deletions internal/seclang/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"strconv"
"strings"

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

"github.com/corazawaf/coraza/v3/debuglog"
"github.com/corazawaf/coraza/v3/internal/auditlog"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/internal/memoize"
"github.com/corazawaf/coraza/v3/internal/regexp"
utils "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types"
)
Expand Down
3 changes: 1 addition & 2 deletions internal/seclang/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"strings"
"testing"

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

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

Expand Down

0 comments on commit 22ba28d

Please sign in to comment.