Skip to content

Commit

Permalink
fix(seclang): merge chained raw rules (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
jptosso authored Feb 6, 2024
1 parent 8993363 commit acd2040
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/seclang/rule_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ func ParseRule(options RuleOptions) (*corazawaf.Rule, error) {
}
}
rule := rp.Rule()
rule.Raw_ = options.Raw
rule.File_ = options.ParserConfig.ConfigFile
rule.Line_ = options.ParserConfig.LastLine

Expand All @@ -392,7 +391,12 @@ func ParseRule(options RuleOptions) (*corazawaf.Rule, error) {
// TODO we must remove defaultactions from chains
rule.Phase_ = 0
lastChain.Chain = rule
// This way we store the raw rule in the parent
parent.Raw_ += " \n" + options.Raw
return nil, nil
} else {
// we only want Raw for the parent
rule.Raw_ = options.Raw
}
return rule, nil
}
Expand Down
23 changes: 23 additions & 0 deletions internal/seclang/rule_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@ func TestInvalidOperatorRuleData(t *testing.T) {
}
}

func TestRawChainedRules(t *testing.T) {
waf := corazawaf.NewWAF()
p := NewParser(waf)
if err := p.FromString(`
SecRule REQUEST_URI "abc" "id:7,phase:2,chain"
SecRule REQUEST_URI "def" "chain"
SecRule REQUEST_URI "ghi" ""
`); err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
raw := waf.Rules.GetRules()[0].Raw()
spl := strings.Split(raw, "\n")
if len(spl) != 3 {
t.Errorf("unexpected number of chained rules, want 3, have %d", len(spl))
}
for i, r := range spl {
// we test that all lines begin with SecRule REQUEST_URI "
if !strings.HasPrefix(r, "SecRule REQUEST_URI ") {
t.Errorf("unexpected rule at line %d: %s", i, r)
}
}
}

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

0 comments on commit acd2040

Please sign in to comment.