Skip to content

Commit

Permalink
Use go-re2 (envoyproxy#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Dec 22, 2022
1 parent ed57eba commit 219bb09
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 196 deletions.
22 changes: 0 additions & 22 deletions buildtools/re2/Dockerfile

This file was deleted.

69 changes: 0 additions & 69 deletions buildtools/re2/cre2.patch

This file was deleted.

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/stretchr/testify v1.8.0
github.com/tetratelabs/proxy-wasm-go-sdk v0.20.1-0.20221031045735-89d180d022a5
github.com/tidwall/gjson v1.14.3
github.com/wasilibs/go-re2 v0.0.0-20221219074959-3ec67f9038f0
)

require (
Expand All @@ -16,7 +17,7 @@ require (
github.com/magefile/mage v1.14.0 // indirect
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tetratelabs/wazero v1.0.0-pre.3 // indirect
github.com/tetratelabs/wazero v1.0.0-pre.4.0.20221213074253-2e13f57f56a1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/net v0.1.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.20.1-0.20221031045735-89d180d022a5 h1:gbsZkzeu+H7oX9xJA97eIuNHCuXfppuJh32mX2Cpeqc=
github.com/tetratelabs/proxy-wasm-go-sdk v0.20.1-0.20221031045735-89d180d022a5/go.mod h1:A0osZ5uU1yRt5ZOdRRzIHxJZf8xzsxvEkeL8Ae698+s=
github.com/tetratelabs/wazero v1.0.0-pre.3 h1:Z5fbogMUGcERzaQb9mQU8+yJSy0bVvv2ce3dfR4wcZg=
github.com/tetratelabs/wazero v1.0.0-pre.3/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc=
github.com/tetratelabs/wazero v1.0.0-pre.4.0.20221213074253-2e13f57f56a1 h1:L+/AG1GzZc8u7tIl7ijAl508T/FHu9esMf+E3hZ1JVA=
github.com/tetratelabs/wazero v1.0.0-pre.4.0.20221213074253-2e13f57f56a1/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo=
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/wasilibs/go-re2 v0.0.0-20221219074959-3ec67f9038f0 h1:+dy0jRJ7Y0sMNJPUkTeZ8qC9qc9tNWJ/Noha+L6w2ZE=
github.com/wasilibs/go-re2 v0.0.0-20221219074959-3ec67f9038f0/go.mod h1:9YbcVrlaRryN9yCvk1fAjJTn5MLKPEd9/LnCJPkGWxY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
Expand Down
2 changes: 1 addition & 1 deletion init_tinygo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ package main

import _ "github.com/corazawaf/coraza-proxy-wasm/internal/gc"

// #cgo LDFLAGS: lib/libinjection.a lib/libre2.a lib/libcre2.a lib/libc++.a lib/libc++abi.a lib/libaho_corasick.a lib/libmimalloc.a lib/libgc.a
// #cgo LDFLAGS: lib/libinjection.a lib/libaho_corasick.a lib/libmimalloc.a lib/libgc.a
import "C"
39 changes: 18 additions & 21 deletions internal/operators/rx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,41 @@
package operators

import (
"fmt"

"github.com/corazawaf/coraza/v3/rules"

"github.com/corazawaf/coraza-proxy-wasm/internal/re2"
re2 "github.com/wasilibs/go-re2"
)

type rx struct {
re re2.RegExp
debug bool
re *re2.Regexp
}

var _ rules.Operator = (*rx)(nil)

func newRX(options rules.OperatorOptions) (rules.Operator, error) {
o := &rx{}
data := options.Arguments

if data == `(?:\$(?:\((?:\(.*\)|.*)\)|\{.*})|\/\w*\[!?.+\]|[<>]\(.*\))` {
o.debug = true
fmt.Println("enabling rx debug!")
}

re, err := re2.Compile(data)
if err != nil {
return nil, err
}

o.re = re
return o, err
return &rx{re: re}, nil
}

func (o *rx) Evaluate(tx rules.TransactionState, value string) bool {
res := o.re.FindStringSubmatch8(value, func(i int, match string) {
tx.CaptureField(i, match)
})
if o.debug {
fmt.Println(res)

if tx.Capturing() {
match := o.re.FindStringSubmatch(value)
if len(match) == 0 {
return false
}
for i, c := range match {
if i == 9 {
return true
}
tx.CaptureField(i, c)
}
return true
} else {
return o.re.MatchString(value)
}
return res
}
79 changes: 0 additions & 79 deletions internal/re2/re2.go

This file was deleted.

Binary file removed lib/libc++.a
Binary file not shown.
Binary file removed lib/libc++abi.a
Binary file not shown.
Binary file removed lib/libcre2.a
Binary file not shown.
2 changes: 1 addition & 1 deletion magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ tinygo build -gc=custom -opt=2 -o %s -scheduler=none -target=wasi %s`, filepath.

// UpdateLibs updates the C++ filter dependencies.
func UpdateLibs() error {
libs := []string{"aho-corasick", "bdwgc", "libinjection", "mimalloc", "re2"}
libs := []string{"aho-corasick", "bdwgc", "libinjection", "mimalloc"}
for _, lib := range libs {
if err := sh.RunV("docker", "build", "-t", "ghcr.io/corazawaf/coraza-proxy-wasm/buildtools-"+lib, filepath.Join("buildtools", lib)); err != nil {
return err
Expand Down

0 comments on commit 219bb09

Please sign in to comment.