miaopanel
1package webserver
2
3import (
4 "path/filepath"
5 "strings"
6 "testing"
7
8 "github.com/miaopanel/miaopanel/internal/config"
9 "github.com/miaopanel/miaopanel/internal/store"
10)
11
12func testConfig(t *testing.T) *config.Config {
13 t.Helper()
14 dir := t.TempDir()
15 cfg := config.Default()
16 cfg.PanelDir = dir
17 cfg.DataDir = filepath.Join(dir, "data")
18 cfg.WebRoot = filepath.Join(dir, "wwwroot")
19 cfg.LogDir = filepath.Join(dir, "logs")
20 cfg.Nginx = config.Nginx{
21 VhostDir: filepath.Join(dir, "vhost"),
22 CertDir: filepath.Join(dir, "cert"),
23 }
24 if err := cfg.EnsureDirs(); err != nil {
25 t.Fatalf("dirs: %v", err)
26 }
27 return cfg
28}
29
30func staticSite() *store.Site {
31 return &store.Site{
32 ID: 1, Name: "blog", Slug: "blog", Kind: "static",
33 Root: "/www/wwwroot/blog", Port: 80, Status: "running",
34 Domains: []string{"example.com", "www.example.com"},
35 Options: store.SiteOptions{SecurityHead: true, WAF: true},
36 }
37}
38
39func TestRenderStaticSite(t *testing.T) {
40 cfg := testConfig(t)
41 r := RenderSite(cfg, staticSite(), DefaultWAF())
42 main := string(r.Files[cfg.SiteConfPath("blog")])
43 for _, want := range []string{
44 "listen 80;", "server_name example.com www.example.com;",
45 "root /www/wwwroot/blog;", "include " + cfg.WellKnownPath("blog") + ";",
46 "include " + cfg.ExtensionDir("blog") + "/*.conf;",
47 } {
48 if !strings.Contains(main, want) {
49 t.Errorf("main conf missing %q\n%s", want, main)
50 }
51 }
52 headers := string(r.Files[filepath.Join(cfg.ExtensionDir("blog"), "40-headers.conf")])
53 if !strings.Contains(headers, "X-Content-Type-Options") {
54 t.Error("security headers fragment missing")
55 }
56 waf := string(r.Files[filepath.Join(cfg.ExtensionDir("blog"), "35-waf.conf")])
57 if !strings.Contains(waf, "limit_req zone=mp_waf") {
58 t.Error("waf fragment missing rate limit")
59 }
60 if _, ok := r.Files[filepath.Join(cfg.ExtensionDir("blog"), "10-ssl.conf")]; ok {
61 t.Error("ssl fragment rendered for plain site")
62 }
63}
64
65func TestRenderPHPSite(t *testing.T) {
66 cfg := testConfig(t)
67 s := staticSite()
68 s.Kind, s.Slug, s.PHPVersion = "php", "wp", "8.2"
69 s.Root = "/www/wwwroot/wp"
70 r := RenderSite(cfg, s, DefaultWAF())
71 php := string(r.Files[filepath.Join(cfg.ExtensionDir("wp"), "20-php.conf")])
72 if !strings.Contains(php, "fastcgi_pass unix:/run/php/php8.2-fpm.sock") {
73 t.Errorf("unexpected php fragment:\n%s", php)
74 }
75 main := string(r.Files[cfg.SiteConfPath("wp")])
76 if !strings.Contains(main, "index index.php") {
77 t.Error("php site should index index.php")
78 }
79}
80
81func TestRenderSSLSite(t *testing.T) {
82 cfg := testConfig(t)
83 s := staticSite()
84 s.SSLEnabled = true
85 s.Options.HTTPSRedirect = true
86 s.Options.HSTS = true
87 r := RenderSite(cfg, s, DefaultWAF())
88 main := string(r.Files[cfg.SiteConfPath("blog")])
89 if !strings.Contains(main, "listen 443 ssl http2;") {
90 t.Error("missing ssl listener")
91 }
92 if !strings.Contains(main, "return 301 https://$host$request_uri;") {
93 t.Error("missing http to https redirect block")
94 }
95 if !strings.Contains(main, "ssl_certificate "+filepath.Join(cfg.Nginx.CertDir, "blog", "fullchain.pem")) {
96 t.Errorf("certificate path missing from server block:\n%s", main)
97 }
98 ssl := string(r.Files[filepath.Join(cfg.ExtensionDir("blog"), "10-ssl.conf")])
99 if !strings.Contains(ssl, "ssl_protocols TLSv1.2 TLSv1.3;") {
100 t.Errorf("ssl fragment wrong:\n%s", ssl)
101 }
102 if !strings.Contains(ssl, "Strict-Transport-Security") {
103 t.Error("hsts missing")
104 }
105}
106
107func TestRenderSSLSiteKeepsPort80ForACME(t *testing.T) {
108 cfg := testConfig(t)
109 s := staticSite()
110 s.SSLEnabled = true
111 s.Options.HTTPSRedirect = false
112 r := RenderSite(cfg, s, DefaultWAF())
113 main := string(r.Files[cfg.SiteConfPath("blog")])
114 if !strings.Contains(main, "listen 80;") {
115 t.Errorf("SSL site must keep listening on 80 for ACME:\n%s", main)
116 }
117 if !strings.Contains(main, "listen 443 ssl http2;") {
118 t.Errorf("missing 443 listener:\n%s", main)
119 }
120
121 s.Options.HTTPSRedirect = true
122 r = RenderSite(cfg, s, DefaultWAF())
123 main = string(r.Files[cfg.SiteConfPath("blog")])
124 if !strings.Contains(main, "/.well-known/acme-challenge/") {
125 t.Errorf("redirect block must expose ACME path:\n%s", main)
126 }
127 if !strings.Contains(main, "location / { return 301 https://$host$request_uri; }") {
128 t.Errorf("missing redirect location:\n%s", main)
129 }
130}
131
132func TestRenderAppProxy(t *testing.T) {
133 cfg := testConfig(t)
134 s := staticSite()
135 s.Kind, s.Slug, s.Port = "go", "api", 8101
136 s.Upstream = ""
137 r := RenderSite(cfg, s, DefaultWAF())
138 main := string(r.Files[cfg.SiteConfPath("api")])
139 if !strings.Contains(main, "listen 80;") {
140 t.Errorf("application site must listen on 80:\n%s", main)
141 }
142 if strings.Contains(main, "listen 8101") {
143 t.Fatalf("upstream port leaked into listen directive:\n%s", main)
144 }
145 proxy := string(r.Files[filepath.Join(cfg.ExtensionDir("api"), "70-proxy.conf")])
146 for _, want := range []string{"proxy_pass http://127.0.0.1:8101;", "Upgrade $http_upgrade", "proxy_read_timeout 300s"} {
147 if !strings.Contains(proxy, want) {
148 t.Errorf("proxy fragment missing %q", want)
149 }
150 }
151}
152
153func TestRenderRemovesDisabledFragments(t *testing.T) {
154 cfg := testConfig(t)
155 s := staticSite()
156 s.Options.WAF = false
157 r := RenderSite(cfg, s, DefaultWAF())
158 removed := strings.Join(r.Remove, "\n")
159 for _, want := range []string{"10-ssl.conf", "35-waf.conf", "50-ip.conf", "70-proxy.conf"} {
160 if !strings.Contains(removed, want) {
161 t.Errorf("expected %s in removal list: %v", want, r.Remove)
162 }
163 }
164}
165
166func TestRenderGatewayMode(t *testing.T) {
167 cfg := testConfig(t)
168 s := staticSite()
169 s.Options.GatewayPort = 10001
170 w := DefaultWAF()
171 w.Gateway = true
172 w.GatewayAddr = "127.0.0.1:8899"
173
174 r := RenderSite(cfg, s, w)
175 main := string(r.Files[cfg.SiteConfPath("blog")])
176 for _, want := range []string{
177 "proxy_pass http://127.0.0.1:8899;",
178 "proxy_set_header X-MP-Site blog;",
179 "proxy_set_header X-MP-Origin 127.0.0.1:10001;",
180 "include " + filepath.Join(cfg.ExtensionDir("blog"), "35-waf.conf") + ";",
181 } {
182 if !strings.Contains(main, want) {
183 t.Errorf("edge conf missing %q\n%s", want, main)
184 }
185 }
186 if strings.Contains(main, "/*.conf;") {
187 t.Errorf("edge conf must not glob-include origin fragments:\n%s", main)
188 }
189 if strings.Contains(main, cfg.RewritePath("blog")) {
190 t.Errorf("rewrite must move behind the gateway:\n%s", main)
191 }
192
193 origin := string(r.Files[cfg.OriginConfPath("blog")])
194 for _, want := range []string{
195 "listen 127.0.0.1:10001;",
196 "root /www/wwwroot/blog;",
197 "try_files $uri $uri/ =404;",
198 "include " + cfg.RewritePath("blog") + ";",
199 "access_log off;",
200 } {
201 if !strings.Contains(origin, want) {
202 t.Errorf("origin conf missing %q\n%s", want, origin)
203 }
204 }
205 if r.Files[cfg.OriginConfPath("blog")] == nil {
206 t.Fatal("origin conf not rendered")
207 }
208}
209
210func TestRenderGatewayAppUsesOriginProxy(t *testing.T) {
211 cfg := testConfig(t)
212 s := staticSite()
213 s.Kind, s.Slug, s.Port = "node", "app", 8123
214 s.Upstream = "http://127.0.0.1:8123"
215 s.Options.GatewayPort = 10002
216 w := DefaultWAF()
217 w.Gateway = true
218 w.GatewayAddr = "127.0.0.1:8899"
219
220 r := RenderSite(cfg, s, w)
221 origin := string(r.Files[cfg.OriginConfPath("app")])
222 if !strings.Contains(origin, "include "+filepath.Join(cfg.ExtensionDir("app"), "70-proxy.conf")+";") {
223 t.Errorf("app origin must proxy to the upstream:\n%s", origin)
224 }
225 if strings.Contains(origin, "try_files") {
226 t.Errorf("app origin must not serve files directly:\n%s", origin)
227 }
228}
229
230func TestRenderCustomListenPort(t *testing.T) {
231 cfg := testConfig(t)
232 s := staticSite()
233 s.Kind = "php"
234 s.Slug = "pma"
235 s.Root = "/www/wwwroot/phpmyadmin/current"
236 s.Options.ListenPort = 16520
237 s.Options.HTTPSRedirect = false
238 r := RenderSite(cfg, s, DefaultWAF())
239 main := string(r.Files[cfg.SiteConfPath("pma")])
240 if !strings.Contains(main, "listen 16520;") {
241 t.Fatalf("custom listener not rendered:\n%s", main)
242 }
243 if strings.Contains(main, "listen 80;") {
244 t.Fatalf("default listener leaked into custom port site:\n%s", main)
245 }
246}
247
248func TestRenderGatewayOffRemovesOrigin(t *testing.T) {
249 cfg := testConfig(t)
250 s := staticSite()
251 s.Options.GatewayPort = 10001
252 r := RenderSite(cfg, s, DefaultWAF())
253 removed := strings.Join(r.Remove, "\n")
254 if !strings.Contains(removed, cfg.OriginConfPath("blog")) {
255 t.Fatalf("stale origin conf not removed: %v", r.Remove)
256 }
257}
258
259func TestRenderIPAllowList(t *testing.T) {
260 cfg := testConfig(t)
261 s := staticSite()
262 s.Options.IPMode = "allow"
263 s.Options.IPList = []string{"10.0.0.0/8", "192.168.1.10"}
264 r := RenderSite(cfg, s, DefaultWAF())
265 ip := string(r.Files[filepath.Join(cfg.ExtensionDir("blog"), "50-ip.conf")])
266 if !strings.Contains(ip, "allow 10.0.0.0/8;") || !strings.Contains(ip, "deny all;") {
267 t.Errorf("unexpected ip fragment:\n%s", ip)
268 }
269}