Skip to content

Commit

Permalink
Convert rulematch types to interfaces (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Oct 28, 2022
1 parent 635fad4 commit 9d003ed
Show file tree
Hide file tree
Showing 37 changed files with 573 additions and 379 deletions.
16 changes: 8 additions & 8 deletions actions/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func (a *ctlFn) Evaluate(r rules.RuleMetadata, txS rules.TransactionState) {
case ctlRuleRemoveTargetByTag:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if utils.InSlice(a.value, r.Tags) {
tx.RemoveRuleTargetByID(r.GetID(), a.collection, a.colKey)
if utils.InSlice(a.value, r.Tags_) {
tx.RemoveRuleTargetByID(r.ID(), a.collection, a.colKey)
}
}
case ctlRuleRemoveTargetByMsg:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if r.Msg != nil && r.Msg.String() == a.value {
tx.RemoveRuleTargetByID(r.GetID(), a.collection, a.colKey)
tx.RemoveRuleTargetByID(r.ID(), a.collection, a.colKey)
}
}
case ctlAuditEngine:
Expand Down Expand Up @@ -151,14 +151,14 @@ func (a *ctlFn) Evaluate(r rules.RuleMetadata, txS rules.TransactionState) {
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if r.Msg != nil && r.Msg.String() == a.value {
tx.RemoveRuleByID(r.ID)
tx.RemoveRuleByID(r.ID_)
}
}
case ctlRuleRemoveByTag:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if utils.InSlice(a.value, r.Tags) {
tx.RemoveRuleByID(r.ID)
if utils.InSlice(a.value, r.Tags_) {
tx.RemoveRuleByID(r.ID_)
}
}
case ctlRequestBodyProcessor:
Expand Down Expand Up @@ -273,8 +273,8 @@ func rangeToInts(rules []*corazawaf.Rule, input string) ([]int, error) {
}

for _, r := range rules {
if r.ID >= start && r.ID <= end {
ids = append(ids, r.ID)
if r.ID_ >= start && r.ID_ <= end {
ids = append(ids, r.ID_)
}
}
return ids, nil
Expand Down
9 changes: 5 additions & 4 deletions actions/ctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package actions
import (
"testing"

"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/types"
)
Expand Down Expand Up @@ -82,13 +83,13 @@ func TestCtl(t *testing.T) {
func TestCtlParseRange(t *testing.T) {
rules := []*corazawaf.Rule{
{
RuleMetadata: types.RuleMetadata{
ID: 5,
RuleMetadata: corazarules.RuleMetadata{
ID_: 5,
},
},
{
RuleMetadata: types.RuleMetadata{
ID: 15,
RuleMetadata: corazarules.RuleMetadata{
ID_: 15,
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions actions/deny.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func (a *denyFn) Init(r rules.RuleMetadata, data string) error {
}

func (a *denyFn) Evaluate(r rules.RuleMetadata, tx rules.TransactionState) {
rid := r.GetID()
rid := r.ID()
if rid == 0 {
rid = r.GetParentID()
rid = r.ParentID()
}
tx.Interrupt(&types.Interruption{
Status: r.Status(),
Expand Down
4 changes: 2 additions & 2 deletions actions/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func (a *dropFn) Init(r rules.RuleMetadata, data string) error {
}

func (a *dropFn) Evaluate(r rules.RuleMetadata, tx rules.TransactionState) {
rid := r.GetID()
rid := r.ID()
if rid == 0 {
rid = r.GetParentID()
rid = r.ParentID()
}
tx.Interrupt(&types.Interruption{
Status: r.Status(),
Expand Down
10 changes: 5 additions & 5 deletions actions/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (a *idFn) Init(r rules.RuleMetadata, data string) error {
}
// TODO(anuraaga): Confirm this is internal implementation detail
rInt := r.(*corazawaf.Rule)
rInt.ID = int(i)
if rInt.ID < 0 {
return fmt.Errorf("rule id (%d) cannot be negative", rInt.ID)
rInt.ID_ = int(i)
if rInt.ID_ < 0 {
return fmt.Errorf("rule id (%d) cannot be negative", rInt.ID_)
}
if rInt.ID == 0 {
return fmt.Errorf("rule id (%d) cannot be zero", rInt.ID)
if rInt.ID_ == 0 {
return fmt.Errorf("rule id (%d) cannot be zero", rInt.ID_)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion actions/maturity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (a *maturityFn) Init(r rules.RuleMetadata, data string) error {
return fmt.Errorf("maturity must be between 1 and 9, not %d", m)
}
// TODO(anuraaga): Confirm this is internal implementation detail
r.(*corazawaf.Rule).Maturity = m
r.(*corazawaf.Rule).Maturity_ = m
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion actions/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (a *phaseFn) Init(r rules.RuleMetadata, data string) error {
return err
}
// TODO(anuraaga): Confirm this is internal implementation detail
r.(*corazawaf.Rule).Phase = p
r.(*corazawaf.Rule).Phase_ = p
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions actions/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func (a *redirectFn) Init(r rules.RuleMetadata, data string) error {
}

func (a *redirectFn) Evaluate(r rules.RuleMetadata, tx rules.TransactionState) {
rid := r.GetID()
rid := r.ID()
if rid == 0 {
rid = r.GetParentID()
rid = r.ParentID()
}
tx.Interrupt(&types.Interruption{
Status: r.Status(),
Expand Down
2 changes: 1 addition & 1 deletion actions/rev.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type revFn struct {

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

Expand Down
2 changes: 1 addition & 1 deletion actions/setenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (a *setenvFn) Evaluate(r rules.RuleMetadata, tx rules.TransactionState) {
v := a.value.Expand(tx)
// set env variable
if err := os.Setenv(a.key, v); err != nil {
tx.DebugLogger().Error("[%s] Error setting env variable for rule %d: %s", tx.ID(), r.GetID(), err.Error())
tx.DebugLogger().Error("[%s] Error setting env variable for rule %d: %s", tx.ID(), r.ID(), err.Error())
}
// TODO is this ok?
tx.Variables().Env().Set(a.key, []string{v})
Expand Down
6 changes: 3 additions & 3 deletions actions/setvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (a *setvarFn) Init(r rules.RuleMetadata, data string) error {
func (a *setvarFn) Evaluate(r rules.RuleMetadata, tx rules.TransactionState) {
key := a.key.Expand(tx)
value := a.value.Expand(tx)
tx.DebugLogger().Debug("[%s] Setting var %q to %q by rule %d", tx.ID(), key, value, r.GetID())
tx.DebugLogger().Debug("[%s] Setting var %q to %q by rule %d", tx.ID(), key, value, r.ID())
a.evaluateTxCollection(r, tx, strings.ToLower(key), value)
}

Expand Down Expand Up @@ -93,15 +93,15 @@ func (a *setvarFn) evaluateTxCollection(r rules.RuleMetadata, tx rules.Transacti
if len(value) > 1 {
sum, err = strconv.Atoi(value[1:])
if err != nil {
tx.DebugLogger().Error("[%s] Invalid value for setvar %q on rule %d", tx.ID(), value, r.GetID())
tx.DebugLogger().Error("[%s] Invalid value for setvar %q on rule %d", tx.ID(), value, r.ID())
return
}
}
val := 0
if res != "" {
val, err = strconv.Atoi(res)
if err != nil {
tx.DebugLogger().Error("[%s] Invalid value for setvar %q on rule %d", tx.ID(), res, r.GetID())
tx.DebugLogger().Error("[%s] Invalid value for setvar %q on rule %d", tx.ID(), res, r.ID())
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion actions/severity.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (a *severityFn) Init(r rules.RuleMetadata, data string) error {
return err
}
// TODO(anuraaga): Confirm this is internal implementation detail
r.(*corazawaf.Rule).Severity = sev
r.(*corazawaf.Rule).Severity_ = sev
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion actions/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestSeverity(t *testing.T) {
if err := sev.Init(rule, tt.name); err != nil {
t.Error(err)
}
if got := rule.Severity; got != tt.want {
if got := rule.Severity_; got != tt.want {
t.Errorf("Severity = %s, want %s", got.String(), tt.want.String())
}
})
Expand Down
2 changes: 1 addition & 1 deletion actions/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type tagFn struct {

func (a *tagFn) Init(r rules.RuleMetadata, data string) error {
// TODO(anuraaga): Confirm this is internal implementation detail
r.(*corazawaf.Rule).Tags = append(r.(*corazawaf.Rule).Tags, data)
r.(*corazawaf.Rule).Tags_ = append(r.(*corazawaf.Rule).Tags_, data)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion actions/ver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type verFn struct {

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

Expand Down
31 changes: 16 additions & 15 deletions collection/collection_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package collection
import (
"regexp"

"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -35,11 +36,11 @@ func (c *Map) FindRegex(key *regexp.Regexp) []types.MatchData {
for k, data := range c.data {
if key.MatchString(k) {
for _, d := range data {
result = append(result, types.MatchData{
VariableName: c.name,
Variable: c.variable,
Key: d.Name,
Value: d.Value,
result = append(result, &corazarules.MatchData{
VariableName_: c.name,
Variable_: c.variable,
Key_: d.Name,
Value_: d.Value,
})
}
}
Expand All @@ -56,11 +57,11 @@ func (c *Map) FindString(key string) []types.MatchData {
// if key is not empty
if e, ok := c.data[key]; ok {
for _, aVar := range e {
result = append(result, types.MatchData{
VariableName: c.name,
Variable: c.variable,
Key: aVar.Name,
Value: aVar.Value,
result = append(result, &corazarules.MatchData{
VariableName_: c.name,
Variable_: c.variable,
Key_: aVar.Name,
Value_: aVar.Value,
})
}
}
Expand All @@ -72,11 +73,11 @@ func (c *Map) FindAll() []types.MatchData {
var result []types.MatchData
for _, data := range c.data {
for _, d := range data {
result = append(result, types.MatchData{
VariableName: c.name,
Variable: c.variable,
Key: d.Name,
Value: d.Value,
result = append(result, &corazarules.MatchData{
VariableName_: c.name,
Variable_: c.variable,
Key_: d.Name,
Value_: d.Value,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions collection/collection_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCollectionProxy(t *testing.T) {
p := proxy.FindAll()
m := false
for _, v := range p {
if v.Value == "value1" {
if v.Value() == "value1" {
m = true
break
}
Expand All @@ -48,7 +48,7 @@ func TestCollectionProxy(t *testing.T) {
}
var f []string
for _, r := range p {
f = append(f, r.Value)
f = append(f, r.Value())
}
sort.Strings(f)
if f[0] != "value1" || f[1] != "value2" || f[2] != "value3" {
Expand Down
9 changes: 5 additions & 4 deletions collection/collection_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strconv"

"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -34,10 +35,10 @@ func (c *Simple) FindString(key string) []types.MatchData {
// FindAll returns a single MatchData for the current data
func (c *Simple) FindAll() []types.MatchData {
return []types.MatchData{
{
VariableName: c.name,
Variable: c.variable,
Value: c.data,
&corazarules.MatchData{
VariableName_: c.name,
Variable_: c.variable,
Value_: c.data,
},
}
}
Expand Down
9 changes: 5 additions & 4 deletions collection/collection_size_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strconv"

"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand All @@ -32,10 +33,10 @@ func (c *SizeProxy) FindString(string) []types.MatchData {
// FindAll returns a slice of MatchData of all matches
func (c *SizeProxy) FindAll() []types.MatchData {
return []types.MatchData{
{
VariableName: c.name,
Variable: c.variable,
Value: strconv.FormatInt(c.Size(), 10),
&corazarules.MatchData{
VariableName_: c.name,
Variable_: c.variable,
Value_: strconv.FormatInt(c.Size(), 10),
},
}
}
Expand Down
Loading

0 comments on commit 9d003ed

Please sign in to comment.