miaopanel
1package site
2
3import (
4 "testing"
5
6 "github.com/miaopanel/miaopanel/internal/store"
7)
8
9// A partial update (older mobile client, scripted API call) must not reset the
10// fields it never mentioned: security headers used to disappear silently.
11func TestMergeOptionsKeepsUnsentFields(t *testing.T) {
12 old := store.SiteOptions{
13 SecurityHead: true, HSTS: true, WAF: true, WAFRate: 20, WAFBurst: 40,
14 ListenPort: 16520, GatewayPort: 10000, IPMode: "deny", IPList: []string{"10.0.0.0/8"},
15 }
16 in := store.SiteOptions{ListenPort: 8080, Present: map[string]bool{"listen_port": true}}
17 got := mergeOptions(old, in)
18 if !got.SecurityHead || !got.HSTS || !got.WAF {
19 t.Fatalf("security flags lost: %+v", got)
20 }
21 if got.WAFRate != 20 || got.WAFBurst != 40 {
22 t.Fatalf("WAF rates lost: %+v", got)
23 }
24 if got.GatewayPort != 10000 || got.IPMode != "deny" || len(got.IPList) != 1 {
25 t.Fatalf("other options lost: %+v", got)
26 }
27 if got.ListenPort != 8080 {
28 t.Fatalf("listen_port = %d, want 8080", got.ListenPort)
29 }
30}
31
32func TestMergeOptionsHonoursExplicitZeroAndFalse(t *testing.T) {
33 old := store.SiteOptions{SecurityHead: true, HSTS: true, ListenPort: 16520}
34 in := store.SiteOptions{
35 SecurityHead: false,
36 ListenPort: 0,
37 Present: map[string]bool{"security_headers": true, "hsts": true, "listen_port": true},
38 }
39 got := mergeOptions(old, in)
40 if got.SecurityHead {
41 t.Fatal("explicit false ignored")
42 }
43 if got.HSTS {
44 t.Fatal("explicit false ignored for hsts")
45 }
46 if got.ListenPort != 0 {
47 t.Fatalf("explicit zero ignored: %d", got.ListenPort)
48 }
49}
50
51func TestMergeOptionsWithoutOptionsKeepsEverything(t *testing.T) {
52 old := store.SiteOptions{GatewayPort: 10001, SecurityHead: true, WAF: true, ListenPort: 10121}
53 got := mergeOptions(old, store.SiteOptions{})
54 if got.GatewayPort != 10001 || !got.SecurityHead || !got.WAF || got.ListenPort != 10121 {
55 t.Fatalf("options dropped for a request without options: %+v", got)
56 }
57}
58
59func TestValidateRejectsUnsafePHPVersionAndIPList(t *testing.T) {
60 s := &Service{}
61 cases := []Input{
62 {Name: "a", Kind: "php", Domains: []string{"a.example.com"}, PHPVersion: "8.2-fpm.sock; add_header X 1"},
63 {Name: "b", Kind: "static", Domains: []string{"b.example.com"},
64 Options: store.SiteOptions{IPMode: "deny", IPList: []string{"1.2.3.4; }"}}},
65 {Name: "c", Kind: "static", Domains: []string{"c.example.com"},
66 Options: store.SiteOptions{AntiLeak: true, AntiLeakHosts: []string{"a.com;"}}},
67 {Name: "d", Kind: "static", Domains: []string{"d.example.com"},
68 Options: store.SiteOptions{ListenPort: 443}},
69 }
70 for i, in := range cases {
71 input := in
72 if err := s.validateInner(nil, &input, 0); err == nil {
73 t.Fatalf("case %d accepted unsafe input: %+v", i, in)
74 }
75 }
76}