package httpapi import ( "net/http" "net/http/httptest" "os" "strings" "testing" ) func fileContent(t *testing.T, path string) string { t.Helper() data, err := os.ReadFile(path) if err != nil { t.Fatalf("read %s: %v", path, err) } return string(data) } func TestWAFOverviewAndSettings(t *testing.T) { h := newHarness(t) csrf := h.login(t) code, env := h.do(t, http.MethodGet, "/api/v1/waf/overview", nil, "") if code != http.StatusOK { t.Fatalf("overview status %d: %v", code, env) } data := env["data"].(map[string]any) if _, ok := data["requests"]; !ok { t.Fatalf("overview payload: %v", data) } code, env = h.do(t, http.MethodGet, "/api/v1/waf/settings", nil, "") if code != http.StatusOK { t.Fatalf("settings status %d", code) } settings := env["data"].(map[string]any) settings["rate"] = 45 settings["whitelist"] = []string{"10.1.2.3"} code, env = h.do(t, http.MethodPut, "/api/v1/waf/settings", settings, csrf) if code != http.StatusOK { t.Fatalf("save settings status %d: %v", code, env) } // Invalid values must be rejected. settings["rate"] = 0 code, _ = h.do(t, http.MethodPut, "/api/v1/waf/settings", settings, csrf) if code != http.StatusBadRequest { t.Fatalf("invalid rate accepted: %d", code) } } func TestWAFIntelSettingsAndSync(t *testing.T) { h := newHarness(t) csrf := h.login(t) code, env := h.do(t, http.MethodGet, "/api/v1/waf/intel", nil, "") if code != http.StatusOK { t.Fatalf("intel status %d: %v", code, env) } st := env["data"].(map[string]any) if st["enabled"] == true { t.Fatalf("intel enabled by default: %v", st) } code, _ = h.do(t, http.MethodPut, "/api/v1/waf/intel", map[string]any{"enabled": true, "url": "", "hour": 4}, csrf) if code != http.StatusBadRequest { t.Fatalf("missing url accepted: %d", code) } feed := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("deny 9.9.9.9;\nallow all;\n")) })) defer feed.Close() code, env = h.do(t, http.MethodPut, "/api/v1/waf/intel", map[string]any{"enabled": true, "url": feed.URL, "hour": 4}, csrf) if code != http.StatusOK { t.Fatalf("save intel status %d: %v", code, env) } code, env = h.do(t, http.MethodPost, "/api/v1/waf/intel/sync", nil, csrf) if code != http.StatusOK { t.Fatalf("sync intel status %d: %v", code, env) } code, env = h.do(t, http.MethodGet, "/api/v1/waf/intel", nil, "") if code != http.StatusOK { t.Fatalf("intel status after sync %d", code) } st = env["data"].(map[string]any) if int(st["count"].(float64)) != 1 || st["last_sync"].(float64) == 0 { t.Fatalf("unexpected intel state: %v", st) } } func TestWAFRuleCRUD(t *testing.T) { h := newHarness(t) csrf := h.login(t) rule := map[string]any{ "name": "block admin", "match_on": "uri", "op": "contains", "pattern": "/admin", "action": "block", "enabled": true, } code, env := h.do(t, http.MethodPost, "/api/v1/waf/rules", rule, csrf) if code != http.StatusOK { t.Fatalf("create rule status %d: %v", code, env) } created := env["data"].(map[string]any) id := int64(created["id"].(float64)) code, env = h.do(t, http.MethodGet, "/api/v1/waf/rules", nil, "") if code != http.StatusOK || len(env["data"].([]any)) != 1 { t.Fatalf("list rules: %d %v", code, env) } // Injection attempt is refused before nginx sees it. bad := map[string]any{ "name": "evil", "match_on": "uri", "op": "regex", "pattern": `x") { return 200; } #`, "action": "block", "enabled": true, } code, _ = h.do(t, http.MethodPost, "/api/v1/waf/rules", bad, csrf) if code != http.StatusBadRequest { t.Fatalf("injection accepted: %d", code) } rule["enabled"] = false code, _ = h.do(t, http.MethodPut, "/api/v1/waf/rules/"+itoa(id), rule, csrf) if code != http.StatusOK { t.Fatalf("update rule status %d", code) } // The generated http level zones must carry the whitelist map. zones := fileContent(t, h.cfg.Nginx.VhostDir+"/http/00-zones.conf") if !strings.Contains(zones, "geo $waf_trusted") { t.Fatalf("zones not rendered:\n%s", zones) } code, _ = h.do(t, http.MethodDelete, "/api/v1/waf/rules/"+itoa(id), nil, csrf) if code != http.StatusOK { t.Fatalf("delete rule status %d", code) } code, env = h.do(t, http.MethodGet, "/api/v1/waf/rules", nil, "") if len(env["data"].([]any)) != 0 { t.Fatalf("rule not deleted: %v", env) } } func TestSiteCreateDerivesSlugAndReadsPackage(t *testing.T) { h := newHarness(t) csrf := h.login(t) // No name: the slug is derived from the first domain. code, env := h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{ "kind": "static", "domains": []string{"Auto.Test"}, }, csrf) if code != http.StatusOK { t.Fatalf("create without name: %d %v", code, env) } created := env["data"].(map[string]any) if created["name"] != "auto-test" { t.Fatalf("derived name = %v", created["name"]) } // package.json scanning for the Node dialog. if code, _ := h.do(t, http.MethodPost, "/api/v1/files/mkdir", map[string]string{"path": "/nodeapp"}, csrf); code != http.StatusOK { t.Fatalf("mkdir status %d", code) } pkg := `{"name":"demo-app","scripts":{"build":"vite build","start":"node server.js","dev":"vite"}}` if code, _ := h.do(t, http.MethodPut, "/api/v1/files/write", map[string]string{"path": "/nodeapp/package.json", "content": pkg}, csrf); code != http.StatusOK { t.Fatalf("write package.json failed") } code, env = h.do(t, http.MethodGet, "/api/v1/sites/package?dir=%2Fnodeapp", nil, "") if code != http.StatusOK { t.Fatalf("package status %d: %v", code, env) } data := env["data"].(map[string]any) if data["name"] != "demo-app" { t.Fatalf("package name = %v", data["name"]) } scripts := data["scripts"].([]any) if len(scripts) != 3 || scripts[0].(map[string]any)["name"] != "start" { t.Fatalf("scripts order = %v", scripts) } // Missing package.json is a clean 404. code, _ = h.do(t, http.MethodGet, "/api/v1/sites/package?dir=%2Fmissing", nil, "") if code != http.StatusNotFound { t.Fatalf("missing package status = %d", code) } } func TestWAFSiteToggle(t *testing.T) { h := newHarness(t) csrf := h.login(t) _, env := h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{ "name": "wafsite", "kind": "static", "domains": []string{"waf.test"}, "options": map[string]any{"waf": true}, }, csrf) id := int64(env["data"].(map[string]any)["id"].(float64)) code, _ := h.do(t, http.MethodPost, "/api/v1/waf/sites/"+itoa(id), map[string]bool{"enabled": false}, csrf) if code != http.StatusOK { t.Fatalf("toggle status %d", code) } code, env = h.do(t, http.MethodGet, "/api/v1/waf/sites", nil, "") if code != http.StatusOK { t.Fatalf("sites status %d", code) } sites := env["data"].([]any) if len(sites) != 1 || sites[0].(map[string]any)["enabled"] != false { t.Fatalf("toggle not applied: %v", sites) } }