miaopanel
1package webserver
2
3import (
4 "path/filepath"
5 "strings"
6 "testing"
7)
8
9func TestZonesRenderGlobalConfig(t *testing.T) {
10 cfg := DefaultWAF()
11 cfg.Rate = 33
12 cfg.Whitelist = []string{"10.0.0.1", "192.168.0.0/16"}
13 cfg.BadUAExtra = []string{"my-crawler"}
14 out := zonesFor(cfg)
15 for _, want := range []string{
16 "rate=33r/s",
17 "~*my-crawler 1;",
18 "10.0.0.1 1;",
19 "192.168.0.0/16 1;",
20 "geo $waf_trusted",
21 "map $waf_trusted $waf_limit_key",
22 "map $request_uri $mp_sensitive",
23 } {
24 if !strings.Contains(out, want) {
25 t.Errorf("zones missing %q\n%s", want, out)
26 }
27 }
28}
29
30func TestZonesDisableSensitive(t *testing.T) {
31 cfg := DefaultWAF()
32 cfg.Sensitive = false
33 out := zonesFor(cfg)
34 if strings.Contains(out, `\.(git|svn|hg|bzr)`) {
35 t.Error("sensitive map should be empty when disabled")
36 }
37}
38
39func TestSensitiveRulesOnlyCoverDotAndDumpPaths(t *testing.T) {
40 out := zonesFor(DefaultWAF())
41 // Regression: these used to be blocked even when the file existed.
42 for _, forbidden := range []string{"zip", "tar", "well-known", "wp-login", "xmlrpc"} {
43 if strings.Contains(out, forbidden) {
44 t.Errorf("sensitive rules still mention %q:\n%s", forbidden, out)
45 }
46 }
47 for _, want := range []string{
48 `(^|/)\.(git|svn|hg|bzr)(/|$)`,
49 `/(phpmyadmin|adminer)(/|$)`,
50 `\.(sql|bak|swp|swo)$`,
51 } {
52 if !strings.Contains(out, want) {
53 t.Errorf("sensitive rules missing %q", want)
54 }
55 }
56}
57
58func TestBadUAMapIgnoresEmptyAgent(t *testing.T) {
59 out := zonesFor(DefaultWAF())
60 if strings.Contains(out, `"" 1;`) {
61 t.Fatal("empty user agent must not be treated as a scanner")
62 }
63 if !strings.Contains(out, "~*sqlmap 1;") {
64 t.Fatal("scanner signatures missing")
65 }
66}
67
68func TestStaticAssetsExcludedFromRateLimit(t *testing.T) {
69 out := zonesFor(DefaultWAF())
70 if !strings.Contains(out, "map $uri $mp_rate_key") || !strings.Contains(out, "css|js|mjs") {
71 t.Fatalf("static assets are not excluded from the rate limit key:\n%s", out)
72 }
73 if !strings.Contains(out, "limit_req_status 429") || !strings.Contains(out, "limit_conn_status 429") {
74 t.Fatal("throttling should answer 429, not 503")
75 }
76}
77
78func TestWAFFragmentRules(t *testing.T) {
79 site := staticSite()
80 cfg := DefaultWAF()
81 cfg.BlockStatus = 403
82 cfg.Blacklist = []string{"1.2.3.4"}
83 cfg.Rules = []WAFRule{
84 {ID: 1, Name: "block phpmyadmin", MatchOn: "uri", Op: "contains", Pattern: "/phpmyadmin", Action: "block", Enabled: true},
85 {ID: 2, Name: "old browser", MatchOn: "ua", Op: "regex", Pattern: "MSIE [0-9]", Action: "block", Enabled: true},
86 {ID: 3, Name: "disabled", MatchOn: "uri", Op: "contains", Pattern: "/x", Action: "block", Enabled: false},
87 }
88 out := wafFragment(site, cfg)
89 for _, want := range []string{
90 "return 403;",
91 "deny 1.2.3.4;",
92 "if ($mp_ua_block)",
93 "if ($mp_rule_1)",
94 "if ($mp_rule_2)",
95 } {
96 if !strings.Contains(out, want) {
97 t.Errorf("fragment missing %q\n%s", want, out)
98 }
99 }
100 if strings.Contains(out, "/x") {
101 t.Error("disabled rule was rendered")
102 }
103}
104
105func TestWAFFragmentDisabled(t *testing.T) {
106 conf := testConfig(t)
107 site := staticSite()
108 cfg := DefaultWAF()
109 cfg.Enabled = false
110 cfg.Whitelist = []string{"10.0.0.1"}
111 r := RenderSite(conf, site, cfg)
112 if _, ok := r.Files[filepath.Join(conf.ExtensionDir("blog"), "35-waf.conf")]; ok {
113 t.Fatal("waf fragment rendered while disabled")
114 }
115}
116
117func TestRuleInjectionRejected(t *testing.T) {
118 cases := []WAFRule{
119 {ID: 1, MatchOn: "uri", Op: "regex", Pattern: `x") { return 200; } #`, Action: "block", Enabled: true},
120 {ID: 2, MatchOn: "uri", Op: "contains", Pattern: "ok\nreturn 200;", Action: "block", Enabled: true},
121 {ID: 3, MatchOn: "unknown", Op: "contains", Pattern: "x", Action: "block", Enabled: true},
122 }
123 for _, rule := range cases {
124 if _, _, _, ok := ruleMap(rule); ok {
125 t.Fatalf("unsafe rule accepted: %+v", rule)
126 }
127 }
128}
129
130func TestZonesRenderRuleMaps(t *testing.T) {
131 cfg := DefaultWAF()
132 cfg.Rules = []WAFRule{
133 {ID: 7, MatchOn: "uri", Op: "contains", Pattern: "/admin", Action: "block", Enabled: true},
134 {ID: 8, MatchOn: "ua", Op: "regex", Pattern: "(?i)^sqlmap", Action: "block", Enabled: true},
135 }
136 zones := zonesFor(cfg)
137 for _, want := range []string{
138 `map "$waf_trusted$request_uri" $mp_rule_7`,
139 `~*^0.*/admin 1;`,
140 `map "$waf_trusted$http_user_agent" $mp_rule_8`,
141 `~*^0(?:sqlmap) 1;`,
142 } {
143 if !strings.Contains(zones, want) {
144 t.Errorf("zones missing %q\n%s", want, zones)
145 }
146 }
147}
148
149func TestWAFWhitelistBypassExpression(t *testing.T) {
150 // The trusted flag is concatenated with the matched variable, so a
151 // whitelisted IP produces a key starting with "1" and never matches "^0".
152 cfg := DefaultWAF()
153 cfg.Whitelist = []string{"203.0.113.9"}
154 zones := zonesFor(cfg)
155 if !strings.Contains(zones, "default $binary_remote_addr;") || !strings.Contains(zones, `1 "";`) {
156 t.Fatalf("rate limit key map missing whitelist bypass:\n%s", zones)
157 }
158}