From 9b52cc49231387937fb207da847c2436df4a03e8 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 12 Aug 2026 19:19:48 +1000 Subject: [PATCH] fix(webui): preserve encoded redirect path characters --- internal/webui/auth.go | 31 ++++++++----------------------- internal/webui/auth_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/internal/webui/auth.go b/internal/webui/auth.go index b4ba7348f..ab738a29a 100644 --- a/internal/webui/auth.go +++ b/internal/webui/auth.go @@ -139,14 +139,17 @@ func (s *Server) requireToken(h http.Handler) http.Handler { } // sameOriginPath rebuilds a redirect target from a request path and raw -// query, collapsing any leading "//" down to a single slash first, and -// validating that the resulting target is a local (same-origin) redirect. +// query, collapsing any leading "//" down to a single slash first. // // A path of "//evil.example/x" is a valid http.Redirect Location that // browsers treat as protocol-relative, sending the client off-host. Since // this path is echoed back from the incoming request URL, an attacker can // choose it, so it must never reach http.Redirect verbatim. func sameOriginPath(path, rawQuery string) string { + // URL.Path has already been decoded by net/http. Treat backslashes as + // separators before checking for a protocol-relative path, because browsers + // do the same when interpreting a redirect Location. + path = strings.ReplaceAll(path, "\\", "/") for strings.HasPrefix(path, "//") { path = path[1:] } @@ -154,27 +157,9 @@ func sameOriginPath(path, rawQuery string) string { path = "/" + path } - target := path - if rawQuery != "" { - target += "?" + rawQuery - } - - // Some clients treat '\' as '/', so normalize before parsing. - target = strings.ReplaceAll(target, "\\", "/") - - u, err := url.Parse(target) - if err != nil { - return "/" - } - // Only allow local redirects. - if u.Scheme != "" || u.Host != "" || u.User != nil { - return "/" - } - - if u.RawQuery != "" { - return u.Path + "?" + u.RawQuery - } - return u.Path + // Re-encoding through URL.Path preserves decoded path characters such as + // '%', '?' and '#' as path data instead of reparsing them as URL syntax. + return (&url.URL{Path: path, RawQuery: rawQuery}).RequestURI() } func tokenEqual(got, want string) bool { diff --git a/internal/webui/auth_test.go b/internal/webui/auth_test.go index 448c764c1..8541b30ef 100644 --- a/internal/webui/auth_test.go +++ b/internal/webui/auth_test.go @@ -26,6 +26,33 @@ func TestRequireToken_RedirectRejectsProtocolRelativePath(t *testing.T) { } } +func TestSameOriginPath(t *testing.T) { + tests := []struct { + name string + path string + rawQuery string + want string + }{ + {name: "protocol relative", path: "//evil.example/x", want: "/evil.example/x"}, + {name: "backslash protocol relative", path: "/\\\\evil.example/x", want: "/evil.example/x"}, + {name: "double backslash protocol relative", path: "\\\\evil.example\\x", want: "/evil.example/x"}, + {name: "ordinary path and query", path: "/dashboard", rawQuery: "foo=bar", want: "/dashboard?foo=bar"}, + {name: "encoded percent", path: "/foo%bar", want: "/foo%25bar"}, + {name: "encoded question mark", path: "/foo?bar", want: "/foo%3Fbar"}, + {name: "encoded hash", path: "/foo#bar", want: "/foo%23bar"}, + {name: "encoded slash", path: "/foo/bar", want: "/foo/bar"}, + {name: "encoded backslash", path: "/foo\\bar", want: "/foo/bar"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := sameOriginPath(tt.path, tt.rawQuery); got != tt.want { + t.Fatalf("sameOriginPath(%q, %q) = %q, want %q", tt.path, tt.rawQuery, got, tt.want) + } + }) + } +} + func TestRequireToken_RedirectStripsTokenFromCleanURL(t *testing.T) { s := &Server{Token: "secret-token"} h := s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {