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 <[email protected]>
This commit is contained in:
James Tucker authored and James Tucker committed 2026-09-21 15:42:33 -07:00
1 parent 523b626a8e
commit 816b661f0d
4 files changed
+53

No files matched your search

+4
View File
@@ -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.
+24
View File
@@ -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(), "<!doctype html>") {
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"},
+6
View File
@@ -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.
+19
View File
@@ -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 {