From a55e5530e79c1ca007e9df9aefbbd1252e78ef5e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 11 Oct 2026 03:46:40 +1100 Subject: [PATCH] fix(webui): an unserved /api path is a 404, not the dashboard page --- internal/webui/assets_test.go | 26 ++++++++++++++++++++++++++ internal/webui/server.go | 7 ++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 internal/webui/assets_test.go diff --git a/internal/webui/assets_test.go b/internal/webui/assets_test.go new file mode 100644 index 000000000..c9a8dfc8e --- /dev/null +++ b/internal/webui/assets_test.go @@ -0,0 +1,26 @@ +package webui + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestAssetsFallbackStopsAtAPI(t *testing.T) { + tests := []struct { + path string + want int + }{ + {"/api/control-plane/unknown", http.StatusNotFound}, + {"/tasks/12", http.StatusOK}, + } + for _, tt := range tests { + t.Run(tt.path, func(t *testing.T) { + rec := httptest.NewRecorder() + new(Server).assets().ServeHTTP(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, tt.path, nil)) + if rec.Code != tt.want { + t.Fatalf("status %d, want %d", rec.Code, tt.want) + } + }) + } +} diff --git a/internal/webui/server.go b/internal/webui/server.go index 7eac7fedf..beda600b5 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -8,6 +8,7 @@ import ( "log/slog" "net/http" "strconv" + "strings" "sync" "time" @@ -400,7 +401,7 @@ func (s *Server) handleHealthz(w http.ResponseWriter, _ *http.Request) { } // assets serves the embedded dashboard, falling back to index.html so client -// routes deep-link correctly. Under the no_ui build tag the assets are absent +// routes deep-link correctly. An unserved /api path is a 404, never the page. Under the no_ui build tag the assets are absent // and a plain explanation is served instead of a confusing 404. func (s *Server) assets() http.Handler { if ui.DistDirFS == nil { @@ -410,6 +411,10 @@ func (s *Server) assets() http.Handler { } files := http.FileServerFS(ui.DistDirFS) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasPrefix(r.URL.Path, "/api/") { + http.Error(w, "this archied does not serve "+r.URL.Path, http.StatusNotFound) + return + } if _, err := fs.Stat(ui.DistDirFS, trimLeadingSlash(r.URL.Path)); err != nil { r = r.Clone(r.Context()) r.URL.Path = "/"