mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 20:22:41 +09:00
ipn/ipnlocal: fix redirect for nested directories in serve (#22118)
ipn/ipnlocal: resolve serve file redirects against the request path http.FileServer sends a Location relative to the request URL, such as "nested/" or "../file". fixLocationHeaderResponseWriter prefixed that value with the mount point, which dropped every directory between the mount point and the target. A request for /doc/subdir/nested was redirected to /doc/nested/. Resolve the relative Location against the original request path instead. Collapse leading slashes in the result so a request path like //host/dir cannot produce a protocol-relative Location. Fixes #22049 Signed-off-by: Alex Castillo González <alex.castillog33@gmail.com>
This commit is contained in:
1 parent
1d9de5f81a
commit
01fa3caf30
2 files changed
+33
-1
No files matched your search
+22
-1
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user