fix(webui): preserve encoded redirect path characters

This commit is contained in:
sam committed 2026-08-12 19:19:48 +10:00
1 parent 0ba27935bd
commit 9b52cc4923
2 files changed
+35 -23

No files matched your search

+8 -23
View File
@@ -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 {
+27
View File
@@ -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) {