diff --git a/ipn/ipnlocal/serve.go b/ipn/ipnlocal/serve.go index 5a0413632..133d89da6 100644 --- a/ipn/ipnlocal/serve.go +++ b/ipn/ipnlocal/serve.go @@ -1363,20 +1363,41 @@ func (b *LocalBackend) serveFileOrDirectory(w http.ResponseWriter, r *http.Reque fs.ServeHTTP(&fixLocationHeaderResponseWriter{ ResponseWriter: w, mountPoint: mountPoint, + reqPath: r.URL.Path, }, r) } // fixLocationHeaderResponseWriter is an http.ResponseWriter wrapper that, upon -// flushing HTTP headers, prefixes any Location header with the mount point. +// flushing HTTP headers, rewrites any Location header to be under the mount +// point. http.FileServer sends redirects relative to the (prefix-stripped) +// request URL, so a relative Location is resolved against the original request +// path. type fixLocationHeaderResponseWriter struct { http.ResponseWriter mountPoint string + reqPath string // original request path, before the mount point is stripped fixOnce sync.Once // guards call to fix } func (w *fixLocationHeaderResponseWriter) fix() { h := w.ResponseWriter.Header() if v := h.Get("Location"); v != "" { + if ref, err := url.Parse(v); err == nil && ref.Scheme == "" && ref.Host == "" && !strings.HasPrefix(v, "/") && strings.HasPrefix(w.reqPath, w.mountPoint) { + // Resolve against the original request path, which keeps any + // parent directories below the mount point and removes dot + // segments. The query string, if any, is carried along. + u := (&url.URL{Path: w.reqPath}).ResolveReference(ref) + loc := u.EscapedPath() + if u.RawQuery != "" { + loc += "?" + u.RawQuery + } + // Collapse leading slashes so a request path like "//foo" + // can't produce a protocol-relative Location. + h.Set("Location", "/"+strings.TrimLeft(loc, "/")) + return + } + // Not expected from http.FileServer, which only sends relative + // Locations; kept as a safety net. h.Set("Location", w.mountPoint+v) } } diff --git a/ipn/ipnlocal/serve_test.go b/ipn/ipnlocal/serve_test.go index f1c1d720a..6e3bba7c6 100644 --- a/ipn/ipnlocal/serve_test.go +++ b/ipn/ipnlocal/serve_test.go @@ -1424,9 +1424,11 @@ func TestServeFileOrDirectory(t *testing.T) { writeFile("foo", "this is foo") writeFile("bar", "this is bar") os.MkdirAll(filepath.Join(td, "subdir"), 0o700) + os.MkdirAll(filepath.Join(td, "subdir", "nested"), 0o700) writeFile("subdir/file-a", "this is A") writeFile("subdir/file-b", "this is B") writeFile("subdir/file-c", "this is C") + writeFile("subdir/nested/index.html", "this is nested index") contains := func(subs ...string) func([]byte, *http.Response) error { return func(resBody []byte, res *http.Response) error { @@ -1478,7 +1480,12 @@ func TestServeFileOrDirectory(t *testing.T) { {"/bar/inside-file", "/", isStatus(404)}, {"/subdir", "/", isRedirect("/subdir/")}, {"/subdir/", "/", contains("file-a", "file-b", "file-c")}, + {"/subdir/nested", "/", isRedirect("/subdir/nested/")}, + {"//subdir/nested", "/", isRedirect("/subdir/nested/")}, + {"/subdir/nested/index.html", "/", isRedirect("/subdir/nested/")}, + {"/subdir/nested?x=1", "/", isRedirect("/subdir/nested/?x=1")}, {"/subdir/file-a", "/", contains("this is A")}, + {"/subdir/file-a/", "/", isRedirect("/subdir/file-a")}, {"/subdir/file-z", "/", isStatus(404)}, {"/doc", "/doc/", isRedirect("/doc/")}, @@ -1489,7 +1496,11 @@ func TestServeFileOrDirectory(t *testing.T) { {"/doc/bar/inside-file", "/doc/", isStatus(404)}, {"/doc/subdir", "/doc/", isRedirect("/doc/subdir/")}, {"/doc/subdir/", "/doc/", contains("file-a", "file-b", "file-c")}, + {"/doc/subdir/nested", "/doc/", isRedirect("/doc/subdir/nested/")}, + {"/doc/subdir/nested/index.html", "/doc/", isRedirect("/doc/subdir/nested/")}, + {"/doc/subdir/nested?x=1", "/doc/", isRedirect("/doc/subdir/nested/?x=1")}, {"/doc/subdir/file-a", "/doc/", contains("this is A")}, + {"/doc/subdir/file-a/", "/doc/", isRedirect("/doc/subdir/file-a")}, {"/doc/subdir/file-z", "/doc/", isStatus(404)}, } for _, tt := range tests {