Files
archie/internal/webui/auth_test.go
T
sam 80f9d92e51 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
2026-08-09 01:13:20 +10:00

50 lines
1.5 KiB
Go

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)
}
}