miaopanel
1package httpapi
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "os"
7 "strings"
8 "testing"
9)
10
11func fileContent(t *testing.T, path string) string {
12 t.Helper()
13 data, err := os.ReadFile(path)
14 if err != nil {
15 t.Fatalf("read %s: %v", path, err)
16 }
17 return string(data)
18}
19
20func TestWAFOverviewAndSettings(t *testing.T) {
21 h := newHarness(t)
22 csrf := h.login(t)
23
24 code, env := h.do(t, http.MethodGet, "/api/v1/waf/overview", nil, "")
25 if code != http.StatusOK {
26 t.Fatalf("overview status %d: %v", code, env)
27 }
28 data := env["data"].(map[string]any)
29 if _, ok := data["requests"]; !ok {
30 t.Fatalf("overview payload: %v", data)
31 }
32
33 code, env = h.do(t, http.MethodGet, "/api/v1/waf/settings", nil, "")
34 if code != http.StatusOK {
35 t.Fatalf("settings status %d", code)
36 }
37 settings := env["data"].(map[string]any)
38 settings["rate"] = 45
39 settings["whitelist"] = []string{"10.1.2.3"}
40 code, env = h.do(t, http.MethodPut, "/api/v1/waf/settings", settings, csrf)
41 if code != http.StatusOK {
42 t.Fatalf("save settings status %d: %v", code, env)
43 }
44
45 // Invalid values must be rejected.
46 settings["rate"] = 0
47 code, _ = h.do(t, http.MethodPut, "/api/v1/waf/settings", settings, csrf)
48 if code != http.StatusBadRequest {
49 t.Fatalf("invalid rate accepted: %d", code)
50 }
51}
52
53func TestWAFIntelSettingsAndSync(t *testing.T) {
54 h := newHarness(t)
55 csrf := h.login(t)
56
57 code, env := h.do(t, http.MethodGet, "/api/v1/waf/intel", nil, "")
58 if code != http.StatusOK {
59 t.Fatalf("intel status %d: %v", code, env)
60 }
61 st := env["data"].(map[string]any)
62 if st["enabled"] == true {
63 t.Fatalf("intel enabled by default: %v", st)
64 }
65
66 code, _ = h.do(t, http.MethodPut, "/api/v1/waf/intel",
67 map[string]any{"enabled": true, "url": "", "hour": 4}, csrf)
68 if code != http.StatusBadRequest {
69 t.Fatalf("missing url accepted: %d", code)
70 }
71
72 feed := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73 _, _ = w.Write([]byte("deny 9.9.9.9;\nallow all;\n"))
74 }))
75 defer feed.Close()
76
77 code, env = h.do(t, http.MethodPut, "/api/v1/waf/intel",
78 map[string]any{"enabled": true, "url": feed.URL, "hour": 4}, csrf)
79 if code != http.StatusOK {
80 t.Fatalf("save intel status %d: %v", code, env)
81 }
82 code, env = h.do(t, http.MethodPost, "/api/v1/waf/intel/sync", nil, csrf)
83 if code != http.StatusOK {
84 t.Fatalf("sync intel status %d: %v", code, env)
85 }
86 code, env = h.do(t, http.MethodGet, "/api/v1/waf/intel", nil, "")
87 if code != http.StatusOK {
88 t.Fatalf("intel status after sync %d", code)
89 }
90 st = env["data"].(map[string]any)
91 if int(st["count"].(float64)) != 1 || st["last_sync"].(float64) == 0 {
92 t.Fatalf("unexpected intel state: %v", st)
93 }
94}
95
96func TestWAFRuleCRUD(t *testing.T) {
97 h := newHarness(t)
98 csrf := h.login(t)
99
100 rule := map[string]any{
101 "name": "block admin", "match_on": "uri", "op": "contains",
102 "pattern": "/admin", "action": "block", "enabled": true,
103 }
104 code, env := h.do(t, http.MethodPost, "/api/v1/waf/rules", rule, csrf)
105 if code != http.StatusOK {
106 t.Fatalf("create rule status %d: %v", code, env)
107 }
108 created := env["data"].(map[string]any)
109 id := int64(created["id"].(float64))
110
111 code, env = h.do(t, http.MethodGet, "/api/v1/waf/rules", nil, "")
112 if code != http.StatusOK || len(env["data"].([]any)) != 1 {
113 t.Fatalf("list rules: %d %v", code, env)
114 }
115
116 // Injection attempt is refused before nginx sees it.
117 bad := map[string]any{
118 "name": "evil", "match_on": "uri", "op": "regex",
119 "pattern": `x") { return 200; } #`, "action": "block", "enabled": true,
120 }
121 code, _ = h.do(t, http.MethodPost, "/api/v1/waf/rules", bad, csrf)
122 if code != http.StatusBadRequest {
123 t.Fatalf("injection accepted: %d", code)
124 }
125
126 rule["enabled"] = false
127 code, _ = h.do(t, http.MethodPut, "/api/v1/waf/rules/"+itoa(id), rule, csrf)
128 if code != http.StatusOK {
129 t.Fatalf("update rule status %d", code)
130 }
131
132 // The generated http level zones must carry the whitelist map.
133 zones := fileContent(t, h.cfg.Nginx.VhostDir+"/http/00-zones.conf")
134 if !strings.Contains(zones, "geo $waf_trusted") {
135 t.Fatalf("zones not rendered:\n%s", zones)
136 }
137
138 code, _ = h.do(t, http.MethodDelete, "/api/v1/waf/rules/"+itoa(id), nil, csrf)
139 if code != http.StatusOK {
140 t.Fatalf("delete rule status %d", code)
141 }
142 code, env = h.do(t, http.MethodGet, "/api/v1/waf/rules", nil, "")
143 if len(env["data"].([]any)) != 0 {
144 t.Fatalf("rule not deleted: %v", env)
145 }
146}
147
148func TestSiteCreateDerivesSlugAndReadsPackage(t *testing.T) {
149 h := newHarness(t)
150 csrf := h.login(t)
151
152 // No name: the slug is derived from the first domain.
153 code, env := h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{
154 "kind": "static", "domains": []string{"Auto.Test"},
155 }, csrf)
156 if code != http.StatusOK {
157 t.Fatalf("create without name: %d %v", code, env)
158 }
159 created := env["data"].(map[string]any)
160 if created["name"] != "auto-test" {
161 t.Fatalf("derived name = %v", created["name"])
162 }
163
164 // package.json scanning for the Node dialog.
165 if code, _ := h.do(t, http.MethodPost, "/api/v1/files/mkdir",
166 map[string]string{"path": "/nodeapp"}, csrf); code != http.StatusOK {
167 t.Fatalf("mkdir status %d", code)
168 }
169 pkg := `{"name":"demo-app","scripts":{"build":"vite build","start":"node server.js","dev":"vite"}}`
170 if code, _ := h.do(t, http.MethodPut, "/api/v1/files/write",
171 map[string]string{"path": "/nodeapp/package.json", "content": pkg}, csrf); code != http.StatusOK {
172 t.Fatalf("write package.json failed")
173 }
174 code, env = h.do(t, http.MethodGet, "/api/v1/sites/package?dir=%2Fnodeapp", nil, "")
175 if code != http.StatusOK {
176 t.Fatalf("package status %d: %v", code, env)
177 }
178 data := env["data"].(map[string]any)
179 if data["name"] != "demo-app" {
180 t.Fatalf("package name = %v", data["name"])
181 }
182 scripts := data["scripts"].([]any)
183 if len(scripts) != 3 || scripts[0].(map[string]any)["name"] != "start" {
184 t.Fatalf("scripts order = %v", scripts)
185 }
186
187 // Missing package.json is a clean 404.
188 code, _ = h.do(t, http.MethodGet, "/api/v1/sites/package?dir=%2Fmissing", nil, "")
189 if code != http.StatusNotFound {
190 t.Fatalf("missing package status = %d", code)
191 }
192}
193
194func TestWAFSiteToggle(t *testing.T) {
195 h := newHarness(t)
196 csrf := h.login(t)
197 _, env := h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{
198 "name": "wafsite", "kind": "static", "domains": []string{"waf.test"},
199 "options": map[string]any{"waf": true},
200 }, csrf)
201 id := int64(env["data"].(map[string]any)["id"].(float64))
202
203 code, _ := h.do(t, http.MethodPost, "/api/v1/waf/sites/"+itoa(id),
204 map[string]bool{"enabled": false}, csrf)
205 if code != http.StatusOK {
206 t.Fatalf("toggle status %d", code)
207 }
208 code, env = h.do(t, http.MethodGet, "/api/v1/waf/sites", nil, "")
209 if code != http.StatusOK {
210 t.Fatalf("sites status %d", code)
211 }
212 sites := env["data"].([]any)
213 if len(sites) != 1 || sites[0].(map[string]any)["enabled"] != false {
214 t.Fatalf("toggle not applied: %v", sites)
215 }
216}