Skip to content

Commit

Permalink
Define enough API for Actions interface
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Sep 12, 2022
1 parent 72a69f9 commit 911e0ad
Show file tree
Hide file tree
Showing 60 changed files with 1,154 additions and 568 deletions.
8 changes: 4 additions & 4 deletions actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
"fmt"
"strings"

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

// ruleActionWrapper is used to wrap a RuleAction so that it can be registered
// and recreated on each call
type ruleActionWrapper = func() corazawaf.RuleAction
type ruleActionWrapper = func() rules.Action

// TODO maybe change it to sync.Map
var actionmap = map[string]ruleActionWrapper{}

// RegisterPlugin registers a new RuleAction
// It can be used also for plugins.
// If you register an action with an existing name, it will be overwritten.
func RegisterPlugin(name string, a func() corazawaf.RuleAction) {
func RegisterPlugin(name string, a func() rules.Action) {
name = strings.ToLower(name)
actionmap[name] = a
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func init() {

// GetAction returns an unwrapped RuleAction from the actionmap based on the name
// If the action does not exist it returns an error
func GetAction(name string) (corazawaf.RuleAction, error) {
func GetAction(name string) (rules.Action, error) {
name = strings.ToLower(name)
if a, ok := actionmap[name]; ok {
return a(), nil
Expand Down
17 changes: 8 additions & 9 deletions actions/allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ package actions
import (
"fmt"

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

// 0 nothing, 1 phase, 2 request
type allowFn struct {
allow int
}

func (a *allowFn) Init(r *corazawaf.Rule, b1 string) error {
func (a *allowFn) Init(r rules.Rule, b1 string) error {
switch b1 {
case "phase":
a.allow = 2 // skip current phase
Expand All @@ -29,7 +28,7 @@ func (a *allowFn) Init(r *corazawaf.Rule, b1 string) error {
return nil
}

func (a *allowFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
func (a *allowFn) Evaluate(r rules.Rule, tx rules.TransactionState) {
// TODO implement this:
/*
if a.allow == 1 {
Expand All @@ -42,15 +41,15 @@ func (a *allowFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
*/
}

func (a *allowFn) Type() types.RuleActionType {
return types.ActionTypeDisruptive
func (a *allowFn) Type() rules.ActionType {
return rules.ActionTypeDisruptive
}

func allow() corazawaf.RuleAction {
func allow() rules.Action {
return &allowFn{}
}

var (
_ corazawaf.RuleAction = (*allowFn)(nil)
_ ruleActionWrapper = allow
_ rules.Action = (*allowFn)(nil)
_ ruleActionWrapper = allow
)
32 changes: 16 additions & 16 deletions actions/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@
package actions

import (
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/macro"
"github.com/corazawaf/coraza/v3/rules"
)

type appendFn struct {
data corazawaf.Macro
data macro.Macro
}

func (a *appendFn) Init(r *corazawaf.Rule, data string) error {
macro, err := corazawaf.NewMacro(data)
func (a *appendFn) Init(r rules.Rule, data string) error {
macro, err := macro.NewMacro(data)
if err != nil {
return err
}
a.data = *macro
a.data = macro
return nil
}

func (a *appendFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
if !tx.WAF.ContentInjection {
tx.WAF.Logger.Debug("append rejected because of ContentInjection")
func (a *appendFn) Evaluate(r rules.Rule, tx rules.TransactionState) {
if !tx.ContentInjection() {
tx.DebugLogger().Debug("append rejected because of ContentInjection")
return
}
data := a.data.Expand(tx)
if _, err := tx.ResponseBodyBuffer.Write([]byte(data)); err != nil {
tx.WAF.Logger.Error("append failed to write to response buffer: %s", err.Error())
if _, err := tx.ResponseBodyWriter().Write([]byte(data)); err != nil {
tx.DebugLogger().Error("append failed to write to response buffer: %s", err.Error())
}
}

func (a *appendFn) Type() types.RuleActionType {
return types.ActionTypeNondisruptive
func (a *appendFn) Type() rules.ActionType {
return rules.ActionTypeNondisruptive
}

func append2() corazawaf.RuleAction {
func append2() rules.Action {
return &appendFn{}
}

var (
_ corazawaf.RuleAction = &appendFn{}
_ ruleActionWrapper = append2
_ rules.Action = &appendFn{}
_ ruleActionWrapper = append2
)
19 changes: 10 additions & 9 deletions actions/auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ package actions

import (
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/rules"
)

type auditlogFn struct{}

func (a *auditlogFn) Init(r *corazawaf.Rule, data string) error {
r.Audit = true
func (a *auditlogFn) Init(r rules.Rule, data string) error {
// TODO(anuraaga): Confirm this is internal implementation detail
r.(*corazawaf.Rule).Audit = true
return nil
}

func (a *auditlogFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
func (a *auditlogFn) Evaluate(r rules.Rule, tx rules.TransactionState) {
// Nothing here
}

func (a *auditlogFn) Type() types.RuleActionType {
return types.ActionTypeNondisruptive
func (a *auditlogFn) Type() rules.ActionType {
return rules.ActionTypeNondisruptive
}

func auditlog() corazawaf.RuleAction {
func auditlog() rules.Action {
return &auditlogFn{}
}

var (
_ corazawaf.RuleAction = (*auditlogFn)(nil)
_ ruleActionWrapper = auditlog
_ rules.Action = (*auditlogFn)(nil)
_ ruleActionWrapper = auditlog
)
17 changes: 8 additions & 9 deletions actions/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
package actions

import (
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/rules"
)

type blockFn struct{}

func (a *blockFn) Init(r *corazawaf.Rule, b1 string) error {
func (a *blockFn) Init(r rules.Rule, b1 string) error {
return nil
}

func (a *blockFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
func (a *blockFn) Evaluate(r rules.Rule, tx rules.TransactionState) {
// This should never run
}

func (a *blockFn) Type() types.RuleActionType {
return types.ActionTypeDisruptive
func (a *blockFn) Type() rules.ActionType {
return rules.ActionTypeDisruptive
}

func block() corazawaf.RuleAction {
func block() rules.Action {
return &blockFn{}
}

var (
_ corazawaf.RuleAction = &blockFn{}
_ ruleActionWrapper = block
_ rules.Action = &blockFn{}
_ ruleActionWrapper = block
)
19 changes: 10 additions & 9 deletions actions/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@ package actions

import (
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/rules"
)

type captureFn struct{}

func (a *captureFn) Init(r *corazawaf.Rule, b1 string) error {
func (a *captureFn) Init(r rules.Rule, b1 string) error {
// this will capture only the current rule
r.Capture = true
// TODO(anuraaga): Confirm this is internal implementation detail
r.(*corazawaf.Rule).Capture = true
return nil
}

func (a *captureFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
func (a *captureFn) Evaluate(r rules.Rule, tx rules.TransactionState) {

}

func (a *captureFn) Type() types.RuleActionType {
return types.ActionTypeNondisruptive
func (a *captureFn) Type() rules.ActionType {
return rules.ActionTypeNondisruptive
}

func capture() corazawaf.RuleAction {
func capture() rules.Action {
return &captureFn{}
}

var (
_ corazawaf.RuleAction = &captureFn{}
_ ruleActionWrapper = capture
_ rules.Action = &captureFn{}
_ ruleActionWrapper = capture
)
19 changes: 10 additions & 9 deletions actions/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ package actions

import (
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/rules"
)

type chainFn struct{}

func (a *chainFn) Init(r *corazawaf.Rule, b1 string) error {
r.HasChain = true
func (a *chainFn) Init(r rules.Rule, b1 string) error {
// TODO(anuraaga): Confirm this is internal implementation detail
r.(*corazawaf.Rule).HasChain = true
return nil
}

func (a *chainFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
func (a *chainFn) Evaluate(r rules.Rule, tx rules.TransactionState) {
// Not evaluated
}

func (a *chainFn) Type() types.RuleActionType {
return types.ActionTypeFlow
func (a *chainFn) Type() rules.ActionType {
return rules.ActionTypeFlow
}

func chain() corazawaf.RuleAction {
func chain() rules.Action {
return &chainFn{}
}

var (
_ corazawaf.RuleAction = &chainFn{}
_ ruleActionWrapper = chain
_ rules.Action = &chainFn{}
_ ruleActionWrapper = chain
)
21 changes: 12 additions & 9 deletions actions/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/corazawaf/coraza/v3/internal/corazawaf"
utils "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/rules"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ type ctlFn struct {
colRx *regexp.Regexp
}

func (a *ctlFn) Init(r *corazawaf.Rule, data string) error {
func (a *ctlFn) Init(r rules.Rule, data string) error {
var err error
a.action, a.value, a.collection, a.colKey, err = a.parseCtl(data)
if len(a.colKey) > 2 && a.colKey[0] == '/' && a.colKey[len(a.colKey)-1] == '/' {
Expand All @@ -58,7 +59,9 @@ func (a *ctlFn) Init(r *corazawaf.Rule, data string) error {
return err
}

func (a *ctlFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
func (a *ctlFn) Evaluate(r rules.Rule, txS rules.TransactionState) {
// TODO(anuraaga): Confirm this is internal implementation detail
tx := txS.(*corazawaf.Transaction)
switch a.action {
case ctlRemoveTargetByID:
ran, err := a.rangeToInts(tx.WAF.Rules.GetRules(), a.value)
Expand All @@ -79,7 +82,7 @@ func (a *ctlFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
case ctlRemoveTargetByMsg:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if r.Msg.String() == a.value {
if r.Msg != nil && r.Msg.String() == a.value {
tx.RemoveRuleTargetByID(r.ID, a.collection, a.colKey)
}
}
Expand Down Expand Up @@ -118,7 +121,7 @@ func (a *ctlFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {
case ctlRuleRemoveByMsg:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if r.Msg.String() == a.value {
if r.Msg != nil && r.Msg.String() == a.value {
tx.RemoveRuleByID(r.ID)
}
}
Expand All @@ -144,8 +147,8 @@ func (a *ctlFn) Evaluate(r *corazawaf.Rule, tx *corazawaf.Transaction) {

}

func (a *ctlFn) Type() types.RuleActionType {
return types.ActionTypeNondisruptive
func (a *ctlFn) Type() rules.ActionType {
return rules.ActionTypeNondisruptive
}

func (a *ctlFn) parseCtl(data string) (ctlFunctionType, string, variables.RuleVariable, string, error) {
Expand Down Expand Up @@ -240,11 +243,11 @@ func (a *ctlFn) rangeToInts(rules []*corazawaf.Rule, input string) ([]int, error
return ids, nil
}

func ctl() corazawaf.RuleAction {
func ctl() rules.Action {
return &ctlFn{}
}

var (
_ corazawaf.RuleAction = &ctlFn{}
_ ruleActionWrapper = ctl
_ rules.Action = &ctlFn{}
_ ruleActionWrapper = ctl
)
Loading

0 comments on commit 911e0ad

Please sign in to comment.