From 816b661f0dc3ebfc8f3375636cff08070aa1b0b5 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Mon, 21 Sep 2026 22:19:28 +0000 Subject: [PATCH] client/web, tsweb/compserve: serve index.html for the root path GET / in the client/web assets handler normalized to an empty fs path. The empty name is invalid, and file systems wrapped in fs.Sub reject it with fs.ErrInvalid instead of fs.ErrNotExist. The index.html fallback only triggers on fs.ErrNotExist, so it never ran and the request failed with a 500 "internal error". This broke the corp smoke test that curls http://100.100.100.100/. Normalize the empty path to index.html before serving. Also make compserve.ServeFile return fs.ErrNotExist for invalid paths, per its documented contract, instead of leaking fs.ErrInvalid. Both layers have regression tests verified to fail without the fix. Updates tailscale/corp#20099 Updates #12170 Signed-off-by: James Tucker --- client/web/assets.go | 4 ++++ client/web/web_test.go | 24 ++++++++++++++++++++++++ tsweb/compserve/compserve.go | 6 ++++++ tsweb/compserve/compserve_test.go | 19 +++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/client/web/assets.go b/client/web/assets.go index 164d44be5..bdbc8a42d 100644 --- a/client/web/assets.go +++ b/client/web/assets.go @@ -33,6 +33,10 @@ func assetsHandler(devMode bool) (_ http.Handler, cleanup func()) { opts := compserve.Options{} return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := strings.TrimPrefix(r.URL.Path, "/") + if path == "" { + // The root path is the SPA entrypoint. + path = "index.html" + } if strings.HasPrefix(path, "assets/") { // Aggressively cache static assets, since we cache-bust our assets with // hashed filenames. diff --git a/client/web/web_test.go b/client/web/web_test.go index 7bf2d8d2a..360744a9e 100644 --- a/client/web/web_test.go +++ b/client/web/web_test.go @@ -32,6 +32,30 @@ import ( "tailscale.com/util/syspolicy/policyclient" ) +// TestServeIndexRootPath verifies that a GET of the root path ("/"), as +// the `curl 100.100.100.100` smoke test issues, serves the SPA +// index.html rather than "internal error". +func TestServeIndexRootPath(t *testing.T) { + s, err := NewServer(ServerOpts{Mode: LoginServerMode}) + if err != nil { + t.Fatal(err) + } + defer s.Shutdown() + + for _, path := range []string{"/", "/index.html"} { + r := httptest.NewRequest(httpm.GET, "http://100.100.100.100"+path, nil) + w := httptest.NewRecorder() + s.ServeHTTP(w, r) + if w.Code != http.StatusOK { + t.Errorf("GET %v: status = %d, want 200; body=%s", path, w.Code, w.Body.String()) + continue + } + if !strings.Contains(w.Body.String(), "") { + t.Errorf("GET %v: body is not index.html: %q", path, w.Body.String()) + } + } +} + func TestQnapAuthnURL(t *testing.T) { query := url.Values{ "qtoken": []string{"token"}, diff --git a/tsweb/compserve/compserve.go b/tsweb/compserve/compserve.go index 771bd364e..12d924c2e 100644 --- a/tsweb/compserve/compserve.go +++ b/tsweb/compserve/compserve.go @@ -85,6 +85,12 @@ func ServeFile(w http.ResponseWriter, r *http.Request, fsys fs.FS, path string, if encodings == nil { encodings = DefaultEncodings } + if !fs.ValidPath(path) { + // An invalid name can never exist. Report not-found rather than + // the fs.ErrInvalid that validating file systems such as fs.Sub + // return, so callers can fall back to another path. + return &fs.PathError{Op: "open", Path: path, Err: fs.ErrNotExist} + } // The served representation (an encoded variant or identity) is // selected from Accept-Encoding, so caches must treat the response as // varying even when the negotiated representation is identity. diff --git a/tsweb/compserve/compserve_test.go b/tsweb/compserve/compserve_test.go index facb852ef..fde710e14 100644 --- a/tsweb/compserve/compserve_test.go +++ b/tsweb/compserve/compserve_test.go @@ -285,6 +285,25 @@ func TestServeFile_Missing(t *testing.T) { } } +func TestServeFile_InvalidPath(t *testing.T) { + // Wrap a map FS in fs.Sub with a non-root directory: the subFS + // rejects invalid names with fs.ErrInvalid, like embedded-asset + // file systems do (prebuilt.FS is fs.Sub(embed.FS, "build")). + sub, err := fs.Sub(mapFS(map[string][]byte{"build/index.html": []byte("html")}), "build") + if err != nil { + t.Fatal(err) + } + for _, path := range []string{"", "../escape"} { + w, notExist := serveFileRequest(t, sub, path, "zstd", Options{}) + if !notExist { + t.Errorf("path %q: expected fs.ErrNotExist", path) + } + if w.Body.Len() != 0 { + t.Errorf("path %q: body = %q, want no response written", path, w.Body.String()) + } + } +} + // modTimeFS is an fs.FS whose files report a fixed modification time, like // vcstime-wrapped file systems do for embedded files. type modTimeFS struct {