fix(webui): close open-redirect in dashboard token-exchange handler
clean.RequestURI() echoed the incoming request's raw path/query straight into http.Redirect's Location header. A path starting with "//" is treated by browsers as protocol-relative, so a crafted token-exchange link could redirect off-host after a legitimate token exchange. sameOriginPath() collapses any leading "//" before the value is ever used. Fixes #440
This commit is contained in:
1 parent
6aa3772ce9
commit
80f9d92e51
2 files changed
+70
-1
No files matched your search
+21
-1
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user