package waf import "testing" // A rule with a broken regex used to reach the database, after which every // later apply failed until someone deleted the row by hand. func TestRuleValidateRejectsBrokenRegex(t *testing.T) { in := RuleInput{Name: "broken", MatchOn: "uri", Op: "regex", Pattern: "foo(#bar", Action: "block"} if err := in.Validate(); err == nil { t.Fatal("broken regex accepted") } ok := RuleInput{Name: "ok", MatchOn: "uri", Op: "regex", Pattern: "^/admin", Action: "block"} if err := ok.Validate(); err != nil { t.Fatalf("valid regex rejected: %v", err) } } func TestSettingsRejectAmbiguousUAKeyword(t *testing.T) { st := Settings{ BlockStatus: 444, Rate: 50, Burst: 200, RetentionDays: 7, GatewayThreshold: 8, BadUAExtra: []string{"my bot"}, } if err := st.Validate(); err == nil { t.Fatal("keyword with a space accepted") } st.BadUAExtra = []string{"sqlmap"} if err := st.Validate(); err != nil { t.Fatalf("plain keyword rejected: %v", err) } }