Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: variable parsing error #1087

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/seclang/rule_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func (rp *RuleParser) ParseVariables(vars string) error {
isEscaped = !isEscaped
default:
curKey = append(curKey, c)
if isEscaped {
isEscaped = false
}
}
case 3:
// XPATH
Expand Down
29 changes: 29 additions & 0 deletions internal/seclang/rule_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package seclang

import (
"errors"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -274,6 +275,34 @@ func TestRawChainedRules(t *testing.T) {
}
}

func TestParseRule(t *testing.T) {
tests := []struct {
name string
vars string
want int
}{
{"Does not contain escape characters", `ARGS_GET:/(test)/|REQUEST_XML`, 2},
{"The last variable contains escape characters", `ARGS_GET|REQUEST_XML:/(test)\b/`, 2},
{"Contains escape characters", `ARGS_GET:/(test\b)/|REQUEST_XML`, 2},
}

for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
rp := RuleParser{
rule: corazawaf.NewRule(),
}
if err := rp.ParseVariables(tt.vars); err != nil {
t.Error(err)
}
got := reflect.ValueOf(rp.rule).Elem().FieldByName("variables").Len()
if got != tt.want {
t.Error("variables parse error want", tt.want, "got", got)
}
})
}
}

func BenchmarkParseActions(b *testing.B) {
actionsToBeParsed := "id:980170,phase:5,pass,t:none,noauditlog,msg:'Anomaly Scores:Inbound Scores - Outbound Scores',tag:test"
for i := 0; i < b.N; i++ {
Expand Down
Loading