diff --git a/modules/dashboard/dashboard.go b/modules/dashboard/dashboard.go index 3446aeddf..79330d8cc 100644 --- a/modules/dashboard/dashboard.go +++ b/modules/dashboard/dashboard.go @@ -15,18 +15,35 @@ const page = "archie.html" // Routes serves the SPA from dist on mux. A path under /api/ that no module // claimed is a JSON 404, never the page. With no build embedded, the page // says how to build it. +// +// One pattern serves both: a method-scoped fallback beside the /api/ catch-all +// is a ServeMux conflict, since Go matches the more general pattern for the +// methods the other doesn't name. func Routes(mux *http.ServeMux, dist fs.FS) { - mux.HandleFunc("/api/", func(w http.ResponseWriter, _ *http.Request) { - httpjson.Error(w, http.StatusNotFound, "no such route") - }) + mux.Handle("/", handler(dist)) +} + +func handler(dist fs.FS) http.Handler { if dist == nil { - mux.HandleFunc("GET /", func(w http.ResponseWriter, _ *http.Request) { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasPrefix(r.URL.Path, "/api/") { + httpjson.Error(w, http.StatusNotFound, "no such route") + return + } http.Error(w, "this binary was built without the dashboard; run task ui and rebuild", http.StatusNotFound) }) - return } files := http.FileServerFS(dist) - mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasPrefix(r.URL.Path, "/api/") { + httpjson.Error(w, http.StatusNotFound, "no such route") + return + } + if r.Method != http.MethodGet && r.Method != http.MethodHead { + w.Header().Set("Allow", "GET, HEAD") + http.Error(w, "the dashboard is read-only", http.StatusMethodNotAllowed) + return + } name := strings.TrimPrefix(r.URL.Path, "/") if name != "" && name != "index.html" && name != page { if info, err := fs.Stat(dist, name); err == nil && !info.IsDir() { diff --git a/modules/dashboard/dashboard_test.go b/modules/dashboard/dashboard_test.go new file mode 100644 index 000000000..284d8167d --- /dev/null +++ b/modules/dashboard/dashboard_test.go @@ -0,0 +1,51 @@ +package dashboard + +import ( + "io/fs" + "net/http" + "net/http/httptest" + "strings" + "testing" + "testing/fstest" +) + +// TestRoutesServeThePageBesideTheAPICatchAll guards a boot-time panic: a +// method-scoped "GET /" fallback cannot sit beside the "/api/" catch-all on one +// ServeMux, so the binary crashed on start with the dashboard built in. It also +// pins what each path answers: an unclaimed API path is a JSON 404 rather than +// the page, and the page is read-only. +func TestRoutesServeThePageBesideTheAPICatchAll(t *testing.T) { + dist := fstest.MapFS{ + page: &fstest.MapFile{Data: []byte("archie")}, + "assets/app-1.js": &fstest.MapFile{Data: []byte("console.log(1)")}, + } + tests := []struct { + name string + dist fs.FS + method string + path string + wantStatus int + wantContent string + }{ + {name: "the page serves a client route", dist: dist, method: http.MethodGet, path: "/runs/42", wantStatus: http.StatusOK, wantContent: "archie"}, + {name: "an asset serves as itself", dist: dist, method: http.MethodGet, path: "/assets/app-1.js", wantStatus: http.StatusOK, wantContent: "console.log(1)"}, + {name: "an unclaimed API path is JSON", dist: dist, method: http.MethodGet, path: "/api/nope", wantStatus: http.StatusNotFound, wantContent: "no such route"}, + {name: "an API path denies every method", dist: dist, method: http.MethodPost, path: "/api/nope", wantStatus: http.StatusNotFound, wantContent: "no such route"}, + {name: "the page is read-only", dist: dist, method: http.MethodPost, path: "/runs", wantStatus: http.StatusMethodNotAllowed, wantContent: "read-only"}, + {name: "a build without the dashboard says how to make it", method: http.MethodGet, path: "/runs", wantStatus: http.StatusNotFound, wantContent: "task ui"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mux := http.NewServeMux() + Routes(mux, tt.dist) + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, nil)) + if w.Code != tt.wantStatus { + t.Fatalf("status = %d, want %d", w.Code, tt.wantStatus) + } + if body := w.Body.String(); !strings.Contains(body, tt.wantContent) { + t.Fatalf("body = %q, want it to contain %q", body, tt.wantContent) + } + }) + } +}