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 {