diff --git a/internal/webui/auth.go b/internal/webui/auth.go index fc74106dc..97dcc9dff 100644 --- a/internal/webui/auth.go +++ b/internal/webui/auth.go @@ -118,7 +118,7 @@ func (s *Server) requireToken(h http.Handler) http.Handler { q := clean.Query() q.Del("t") clean.RawQuery = q.Encode() - http.Redirect(w, r, clean.RequestURI(), http.StatusSeeOther) + http.Redirect(w, r, sameOriginPath(clean.Path, clean.RawQuery), http.StatusSeeOther) return } @@ -137,6 +137,26 @@ 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. +// +// 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 { + for strings.HasPrefix(path, "//") { + path = path[1:] + } + if !strings.HasPrefix(path, "/") { + path = "/" + path + } + if rawQuery != "" { + return path + "?" + rawQuery + } + return path +} + func tokenEqual(got, want string) bool { return subtle.ConstantTimeCompare([]byte(got), []byte(want)) == 1 } diff --git a/internal/webui/auth_test.go b/internal/webui/auth_test.go new file mode 100644 index 000000000..448c764c1 --- /dev/null +++ b/internal/webui/auth_test.go @@ -0,0 +1,49 @@ +package webui + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestRequireToken_RedirectRejectsProtocolRelativePath(t *testing.T) { + s := &Server{Token: "secret-token"} + h := s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + + req := httptest.NewRequest(http.MethodGet, "http://archie.local//evil.example//x?t=secret-token", nil) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + + if rec.Code != http.StatusSeeOther { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusSeeOther) + } + loc := rec.Header().Get("Location") + if strings.HasPrefix(loc, "//") || strings.HasPrefix(loc, "/\\") { + t.Fatalf("Location %q is protocol-relative: open redirect to an attacker-controlled host", loc) + } +} + +func TestRequireToken_RedirectStripsTokenFromCleanURL(t *testing.T) { + s := &Server{Token: "secret-token"} + h := s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + + req := httptest.NewRequest(http.MethodGet, "http://archie.local/dashboard?t=secret-token&foo=bar", nil) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + + if rec.Code != http.StatusSeeOther { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusSeeOther) + } + loc := rec.Header().Get("Location") + if strings.Contains(loc, "t=secret-token") { + t.Fatalf("Location %q still carries the token", loc) + } + if loc != "/dashboard?foo=bar" { + t.Fatalf("Location = %q, want /dashboard?foo=bar", loc) + } +}