package site import ( "testing" "github.com/miaopanel/miaopanel/internal/store" ) // A partial update (older mobile client, scripted API call) must not reset the // fields it never mentioned: security headers used to disappear silently. func TestMergeOptionsKeepsUnsentFields(t *testing.T) { old := store.SiteOptions{ SecurityHead: true, HSTS: true, WAF: true, WAFRate: 20, WAFBurst: 40, ListenPort: 16520, GatewayPort: 10000, IPMode: "deny", IPList: []string{"10.0.0.0/8"}, } in := store.SiteOptions{ListenPort: 8080, Present: map[string]bool{"listen_port": true}} got := mergeOptions(old, in) if !got.SecurityHead || !got.HSTS || !got.WAF { t.Fatalf("security flags lost: %+v", got) } if got.WAFRate != 20 || got.WAFBurst != 40 { t.Fatalf("WAF rates lost: %+v", got) } if got.GatewayPort != 10000 || got.IPMode != "deny" || len(got.IPList) != 1 { t.Fatalf("other options lost: %+v", got) } if got.ListenPort != 8080 { t.Fatalf("listen_port = %d, want 8080", got.ListenPort) } } func TestMergeOptionsHonoursExplicitZeroAndFalse(t *testing.T) { old := store.SiteOptions{SecurityHead: true, HSTS: true, ListenPort: 16520} in := store.SiteOptions{ SecurityHead: false, ListenPort: 0, Present: map[string]bool{"security_headers": true, "hsts": true, "listen_port": true}, } got := mergeOptions(old, in) if got.SecurityHead { t.Fatal("explicit false ignored") } if got.HSTS { t.Fatal("explicit false ignored for hsts") } if got.ListenPort != 0 { t.Fatalf("explicit zero ignored: %d", got.ListenPort) } } func TestMergeOptionsWithoutOptionsKeepsEverything(t *testing.T) { old := store.SiteOptions{GatewayPort: 10001, SecurityHead: true, WAF: true, ListenPort: 10121} got := mergeOptions(old, store.SiteOptions{}) if got.GatewayPort != 10001 || !got.SecurityHead || !got.WAF || got.ListenPort != 10121 { t.Fatalf("options dropped for a request without options: %+v", got) } } func TestValidateRejectsUnsafePHPVersionAndIPList(t *testing.T) { s := &Service{} cases := []Input{ {Name: "a", Kind: "php", Domains: []string{"a.example.com"}, PHPVersion: "8.2-fpm.sock; add_header X 1"}, {Name: "b", Kind: "static", Domains: []string{"b.example.com"}, Options: store.SiteOptions{IPMode: "deny", IPList: []string{"1.2.3.4; }"}}}, {Name: "c", Kind: "static", Domains: []string{"c.example.com"}, Options: store.SiteOptions{AntiLeak: true, AntiLeakHosts: []string{"a.com;"}}}, {Name: "d", Kind: "static", Domains: []string{"d.example.com"}, Options: store.SiteOptions{ListenPort: 443}}, } for i, in := range cases { input := in if err := s.validateInner(nil, &input, 0); err == nil { t.Fatalf("case %d accepted unsafe input: %+v", i, in) } } }