miaopanel
1package waf
2
3import "testing"
4
5// A rule with a broken regex used to reach the database, after which every
6// later apply failed until someone deleted the row by hand.
7func TestRuleValidateRejectsBrokenRegex(t *testing.T) {
8 in := RuleInput{Name: "broken", MatchOn: "uri", Op: "regex", Pattern: "foo(#bar", Action: "block"}
9 if err := in.Validate(); err == nil {
10 t.Fatal("broken regex accepted")
11 }
12 ok := RuleInput{Name: "ok", MatchOn: "uri", Op: "regex", Pattern: "^/admin", Action: "block"}
13 if err := ok.Validate(); err != nil {
14 t.Fatalf("valid regex rejected: %v", err)
15 }
16}
17
18func TestSettingsRejectAmbiguousUAKeyword(t *testing.T) {
19 st := Settings{
20 BlockStatus: 444, Rate: 50, Burst: 200, RetentionDays: 7, GatewayThreshold: 8,
21 BadUAExtra: []string{"my bot"},
22 }
23 if err := st.Validate(); err == nil {
24 t.Fatal("keyword with a space accepted")
25 }
26 st.BadUAExtra = []string{"sqlmap"}
27 if err := st.Validate(); err != nil {
28 t.Fatalf("plain keyword rejected: %v", err)
29 }
30}