Skip to content

Commit

Permalink
rework operators
Browse files Browse the repository at this point in the history
  • Loading branch information
jptosso committed Nov 14, 2021
1 parent 7d01d30 commit 43e047a
Show file tree
Hide file tree
Showing 41 changed files with 244 additions and 282 deletions.
6 changes: 3 additions & 3 deletions operators/begins_with.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type BeginsWith struct {
type beginsWith struct {
data string
}

func (o *BeginsWith) Init(data string) error {
func (o *beginsWith) Init(data string) error {
o.data = data
return nil
}

func (o *BeginsWith) Evaluate(tx *engine.Transaction, value string) bool {
func (o *beginsWith) Evaluate(tx *engine.Transaction, value string) bool {
data := tx.MacroExpansion(o.data)
return strings.HasPrefix(value, data)
}
6 changes: 3 additions & 3 deletions operators/contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type Contains struct {
type contains struct {
data string
}

func (o *Contains) Init(data string) error {
func (o *contains) Init(data string) error {
o.data = data
return nil
}

func (o *Contains) Evaluate(tx *engine.Transaction, value string) bool {
func (o *contains) Evaluate(tx *engine.Transaction, value string) bool {
data := tx.MacroExpansion(o.data)
return strings.Contains(value, data)
}
6 changes: 3 additions & 3 deletions operators/ends_with.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type EndsWith struct {
type endsWith struct {
data string
}

func (o *EndsWith) Init(data string) error {
func (o *endsWith) Init(data string) error {
o.data = data
return nil
}

func (o *EndsWith) Evaluate(tx *engine.Transaction, value string) bool {
func (o *endsWith) Evaluate(tx *engine.Transaction, value string) bool {
data := tx.MacroExpansion(o.data)
return strings.HasSuffix(value, data)
}
6 changes: 3 additions & 3 deletions operators/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type Eq struct {
type eq struct {
data string
}

func (o *Eq) Init(data string) error {
func (o *eq) Init(data string) error {
o.data = data
return nil
}

func (o *Eq) Evaluate(tx *engine.Transaction, value string) bool {
func (o *eq) Evaluate(tx *engine.Transaction, value string) bool {
d1, err := strconv.Atoi(tx.MacroExpansion(o.data))
if err != nil {
d1 = 0
Expand Down
6 changes: 3 additions & 3 deletions operators/ge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type Ge struct {
type ge struct {
data string
}

func (o *Ge) Init(data string) error {
func (o *ge) Init(data string) error {
o.data = data
return nil
}

func (o *Ge) Evaluate(tx *engine.Transaction, value string) bool {
func (o *ge) Evaluate(tx *engine.Transaction, value string) bool {
v, err := strconv.Atoi(value)
if err != nil {
v = 0
Expand Down
2 changes: 1 addition & 1 deletion operators/ge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func TestGe(t *testing.T) {
geo := &Ge{}
geo := &ge{}
if err := geo.Init("2500"); err != nil {
t.Error("Cannot init geo")
}
Expand Down
6 changes: 3 additions & 3 deletions operators/gt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type Gt struct {
type gt struct {
data string
}

func (o *Gt) Init(data string) error {
func (o *gt) Init(data string) error {
o.data = data
return nil
}

func (o *Gt) Evaluate(tx *engine.Transaction, value string) bool {
func (o *gt) Evaluate(tx *engine.Transaction, value string) bool {
v, err := strconv.Atoi(value)
if err != nil {
v = 0
Expand Down
2 changes: 1 addition & 1 deletion operators/gt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func TestGt(t *testing.T) {
gto := &Gt{}
gto := &gt{}
if err := gto.Init("2500"); err != nil {
t.Error("Cannot init gto operator")
}
Expand Down
6 changes: 3 additions & 3 deletions operators/inspect_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type InspectFile struct {
type inspectFile struct {
path string
}

func (o *InspectFile) Init(data string) error {
func (o *inspectFile) Init(data string) error {
o.path = data
return nil
}

func (o *InspectFile) Evaluate(tx *engine.Transaction, value string) bool {
func (o *inspectFile) Evaluate(tx *engine.Transaction, value string) bool {
//TODO parametrize timeout
//TODO add relative path capabilities
//TODO add lua special support
Expand Down
2 changes: 1 addition & 1 deletion operators/inspect_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func TestInspectFile(t *testing.T) {
ipf := &InspectFile{}
ipf := &inspectFile{}
if err := ipf.Init("/bin/echo"); err != nil {
t.Error("cannot init inspectfile operator")
}
Expand Down
6 changes: 3 additions & 3 deletions operators/ip_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type IpMatch struct {
type ipMatch struct {
subnets []*net.IPNet
}

func (o *IpMatch) Init(data string) error {
func (o *ipMatch) Init(data string) error {
o.subnets = []*net.IPNet{}
subnets := strings.Split(data, ",")
for _, sb := range subnets {
Expand All @@ -49,7 +49,7 @@ func (o *IpMatch) Init(data string) error {
return nil
}

func (o *IpMatch) Evaluate(tx *engine.Transaction, value string) bool {
func (o *ipMatch) Evaluate(tx *engine.Transaction, value string) bool {
ip := net.ParseIP(value)
for _, subnet := range o.subnets {
if subnet.Contains(ip) {
Expand Down
10 changes: 5 additions & 5 deletions operators/ip_match_from_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"github.com/jptosso/coraza-waf/v2/utils"
)

type IpMatchFromFile struct {
ip *IpMatch
type ipMatchFromFile struct {
ip *ipMatch
}

func (o *IpMatchFromFile) Init(data string) error {
o.ip = &IpMatch{}
func (o *ipMatchFromFile) Init(data string) error {
o.ip = &ipMatch{}
list, err := utils.OpenFile(data, "")
if err != nil {
return fmt.Errorf("error opening %s", data)
Expand All @@ -36,6 +36,6 @@ func (o *IpMatchFromFile) Init(data string) error {
return o.ip.Init(subnets)
}

func (o *IpMatchFromFile) Evaluate(tx *engine.Transaction, value string) bool {
func (o *ipMatchFromFile) Evaluate(tx *engine.Transaction, value string) bool {
return o.ip.Evaluate(tx, value)
}
6 changes: 3 additions & 3 deletions operators/ip_match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestOneAddress(t *testing.T) {
addrok := "127.0.0.1"
addrfail := "127.0.0.2"
cidr := "127.0.0.1/32"
ipm := &IpMatch{}
ipm := &ipMatch{}
if err := ipm.Init(cidr); err != nil {
t.Error("Cannot init ipmatchtest operator")
}
Expand All @@ -39,7 +39,7 @@ func TestMultipleAddress(t *testing.T) {
addrok := []string{"127.0.0.1", "192.168.0.1", "192.168.0.253"}
addrfail := []string{"127.0.0.2", "192.168.1.1"}
cidr := "127.0.0.1, 192.168.0.0/24"
ipm := &IpMatch{}
ipm := &ipMatch{}
if err := ipm.Init(cidr); err != nil {
t.Error("Cannot init ipmatchtest operator")
}
Expand All @@ -60,7 +60,7 @@ func TestFromFile(t *testing.T) {
addrok := []string{"127.0.0.1", "192.168.0.1", "192.168.0.253"}
addrfail := []string{"127.0.0.2", "192.168.1.1"}

ipm := &IpMatchFromFile{}
ipm := &ipMatchFromFile{}
if err := ipm.Init("../testdata/operators/op/netranges.dat"); err != nil {
t.Error("Cannot init ipmatchfromfile operator")
}
Expand Down
6 changes: 3 additions & 3 deletions operators/le.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type Le struct {
type le struct {
data string
}

func (o *Le) Init(data string) error {
func (o *le) Init(data string) error {
o.data = data
return nil
}

func (o *Le) Evaluate(tx *engine.Transaction, value string) bool {
func (o *le) Evaluate(tx *engine.Transaction, value string) bool {
data := tx.MacroExpansion(o.data)
d, _ := strconv.Atoi(data)
v, err := strconv.Atoi(value)
Expand Down
2 changes: 1 addition & 1 deletion operators/le_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestLe(t *testing.T) {
le := &Le{}
le := &le{}
if err := le.Init("2500"); err != nil {
t.Error("failed to init le operator")
}
Expand Down
6 changes: 3 additions & 3 deletions operators/lt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
engine "github.com/jptosso/coraza-waf/v2"
)

type Lt struct {
type lt struct {
data string
}

func (o *Lt) Init(data string) error {
func (o *lt) Init(data string) error {
o.data = data
return nil
}

func (o *Lt) Evaluate(tx *engine.Transaction, value string) bool {
func (o *lt) Evaluate(tx *engine.Transaction, value string) bool {
vv := tx.MacroExpansion(o.data)
data, err := strconv.Atoi(vv)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion operators/lt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestLt(t *testing.T) {
lt := &Lt{}
lt := &lt{}
if err := lt.Init("2500"); err != nil {
t.Error("failed to init le operator")
}
Expand Down
6 changes: 3 additions & 3 deletions operators/no_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ package operators

import engine "github.com/jptosso/coraza-waf/v2"

type NoMatch struct {
type noMatch struct {
}

func (o *NoMatch) Init(data string) error {
func (o *noMatch) Init(data string) error {
// No need to init
return nil
}

func (o *NoMatch) Evaluate(tx *engine.Transaction, value string) bool {
func (o *noMatch) Evaluate(tx *engine.Transaction, value string) bool {
return false
}
68 changes: 42 additions & 26 deletions operators/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,48 @@

package operators

import engine "github.com/jptosso/coraza-waf/v2"
import (
"fmt"

func OperatorsMap() map[string]engine.RuleOperator {
return map[string]engine.RuleOperator{
"beginsWith": &BeginsWith{},
"rx": &Rx{},
"eq": &Eq{},
"contains": &Contains{},
"endsWith": &EndsWith{},
"inspectFile": &InspectFile{},
"ge": &Ge{},
"gt": &Gt{},
"le": &Le{},
"lt": &Lt{},
"unconditionalMatch": &UnconditionalMatch{},
"within": &Within{},
"pmFromFile": &PmFromFile{},
"pm": &Pm{},
"validateByteRange": &ValidateByteRange{},
"validateUrlEncoding": &ValidateUrlEncoding{},
"streq": &Streq{},
"ipMatch": &IpMatch{},
"ipMatchFromFile": &IpMatchFromFile{},
"rbl": &Rbl{},
"validateUtf8Encoding": &ValidateUtf8Encoding{},
"noMatch": &NoMatch{},
"validateNid": &ValidateNid{},
engine "github.com/jptosso/coraza-waf/v2"
)

type operatorsWrapper = func() engine.RuleOperator

var operators = map[string]operatorsWrapper{}

func init() {
RegisterOperator("beginsWith", func() engine.RuleOperator { return &beginsWith{} })
RegisterOperator("rx", func() engine.RuleOperator { return &rx{} })
RegisterOperator("eq", func() engine.RuleOperator { return &eq{} })
RegisterOperator("contains", func() engine.RuleOperator { return &contains{} })
RegisterOperator("endsWith", func() engine.RuleOperator { return &endsWith{} })
RegisterOperator("inspectFile", func() engine.RuleOperator { return &inspectFile{} })
RegisterOperator("ge", func() engine.RuleOperator { return &ge{} })
RegisterOperator("gt", func() engine.RuleOperator { return &gt{} })
RegisterOperator("le", func() engine.RuleOperator { return &le{} })
RegisterOperator("lt", func() engine.RuleOperator { return &lt{} })
RegisterOperator("unconditionalMatch", func() engine.RuleOperator { return &unconditionalMatch{} })
RegisterOperator("within", func() engine.RuleOperator { return &within{} })
RegisterOperator("pmFromFile", func() engine.RuleOperator { return &pmFromFile{} })
RegisterOperator("pm", func() engine.RuleOperator { return &pm{} })
RegisterOperator("validateByteRange", func() engine.RuleOperator { return &validateByteRange{} })
RegisterOperator("validateUrlEncoding", func() engine.RuleOperator { return &validateUrlEncoding{} })
RegisterOperator("streq", func() engine.RuleOperator { return &streq{} })
RegisterOperator("ipMatch", func() engine.RuleOperator { return &ipMatch{} })
RegisterOperator("ipMatchFromFile", func() engine.RuleOperator { return &ipMatchFromFile{} })
RegisterOperator("rbl", func() engine.RuleOperator { return &rbl{} })
RegisterOperator("validateUtf8Encoding", func() engine.RuleOperator { return &validateUtf8Encoding{} })
RegisterOperator("noMatch", func() engine.RuleOperator { return &noMatch{} })
RegisterOperator("validateNid", func() engine.RuleOperator { return &validateNid{} })
}
func GetOperator(name string) (engine.RuleOperator, error) {
if op, ok := operators[name]; ok {
return op(), nil
}
return nil, fmt.Errorf("operator %s not found", name)
}

func RegisterOperator(name string, op func() engine.RuleOperator) {
operators[name] = op
}
Loading

0 comments on commit 43e047a

Please sign in to comment.