auth: the duplicate-cookie line hides a link's token (audit A4)
Security audit 2026-10-17, finding A4 (low), stage 0. The line logged when a request carries two unprefixed session cookies printed r.URL.Path raw, so GET /confirm/<token> wrote the token to the log (reachable only with PANEL_COOKIE_SECURE=false, where a sibling host can plant the bare name). loggedPath moves from internal/web to internal/web/auth as LoggedPath (web imports auth, not the other way) and both lines - the origin check's refusal and the duplicate-cookie one - go through it. Tests: TestDuplicateCookieLineHidesALinksToken; TestLoggedPathHidesTokens moved with the function. go test ./internal/web ./internal/web/auth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,9 +3,11 @@ package auth
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -88,6 +90,24 @@ func TestSessionTokenRejectsDuplicates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The duplicate-cookie line names the request's path, and a link's token is
|
||||
// not written to the log with it (audit 2026-10-17, A4).
|
||||
func TestDuplicateCookieLineHidesALinksToken(t *testing.T) {
|
||||
m := testModule(t, false)
|
||||
var buf strings.Builder
|
||||
log.SetOutput(&buf)
|
||||
t.Cleanup(func() { log.SetOutput(os.Stderr) })
|
||||
r := httptest.NewRequest(http.MethodGet, "http://panel.example.com/confirm/the-secret-token-of-a-link", nil)
|
||||
r.AddCookie(&http.Cookie{Name: "selfpost_session", Value: "planted-by-a-neighbour"})
|
||||
r.AddCookie(&http.Cookie{Name: "selfpost_session", Value: "the-real-session"})
|
||||
if _, ok := m.sessionToken(r); ok {
|
||||
t.Fatal("duplicate cookies accepted")
|
||||
}
|
||||
if got := buf.String(); strings.Contains(got, "the-secret-token-of-a-link") || !strings.Contains(got, "GET /confirm/… carries 2 cookies") {
|
||||
t.Errorf("logged %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionTokenReadsOneCookie(t *testing.T) {
|
||||
m := testModule(t, true)
|
||||
r := httptest.NewRequest(http.MethodGet, "http://panel.example.com/domains", nil)
|
||||
|
||||
@@ -55,7 +55,7 @@ func (m *Module) sessionToken(r *http.Request) (string, bool) {
|
||||
// name cannot be planted that way (see sessionCookiePrefixed).
|
||||
logf("panel: %s %s carries %d cookies named %q — treating the request as signed out; "+
|
||||
"another host on this domain is overwriting the session cookie, clear the cookies for the parent domain",
|
||||
r.Method, r.URL.Path, n, name)
|
||||
r.Method, LoggedPath(r.URL.Path), n, name)
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,3 +400,17 @@ func loginInfo(r *http.Request) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// LoggedPath is a request's path as the log may hold it: the token of a
|
||||
// password-reset or confirmation link is a secret, and the log is read by
|
||||
// more than the person the link went to (security review, 2026-10-17).
|
||||
// Every log line that names a request's path goes through it: the origin
|
||||
// check's refusal and the duplicate-cookie line (audit 2026-10-17, A4).
|
||||
func LoggedPath(p string) string {
|
||||
for _, prefix := range []string{"/login/reset/", "/confirm/"} {
|
||||
if strings.HasPrefix(p, prefix) && len(p) > len(prefix) {
|
||||
return prefix + "…"
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package web
|
||||
package auth
|
||||
|
||||
import "testing"
|
||||
|
||||
@@ -12,8 +12,8 @@ func TestLoggedPathHidesTokens(t *testing.T) {
|
||||
"/confirm/": "/confirm/",
|
||||
"/account/profile": "/account/profile",
|
||||
} {
|
||||
if got := loggedPath(in); got != want {
|
||||
t.Errorf("loggedPath(%q) = %q, want %q", in, got, want)
|
||||
if got := LoggedPath(in); got != want {
|
||||
t.Errorf("LoggedPath(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,8 @@ package web
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/mixeme/selfpost/internal/web/auth"
|
||||
)
|
||||
|
||||
// contentSecurityPolicy is the panel's CSP. Everything the pages load —
|
||||
@@ -97,7 +98,7 @@ func (s *Server) secure(next http.Handler) http.Handler {
|
||||
// saving anything".
|
||||
logf("panel: rejected %s %s: cross-origin request (Sec-Fetch-Site=%q Origin=%q Host=%q); "+
|
||||
"if this was a legitimate request, check that the reverse proxy passes the original Host header through",
|
||||
r.Method, loggedPath(r.URL.Path), r.Header.Get("Sec-Fetch-Site"), r.Header.Get("Origin"), r.Host)
|
||||
r.Method, auth.LoggedPath(r.URL.Path), r.Header.Get("Sec-Fetch-Site"), r.Header.Get("Origin"), r.Host)
|
||||
http.Error(w, "cross-origin request rejected", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
@@ -169,15 +170,3 @@ func originAllowed(r *http.Request) bool {
|
||||
}
|
||||
return u.Host == r.Host
|
||||
}
|
||||
|
||||
// loggedPath is a request's path as the log may hold it: the token of a
|
||||
// password-reset link is a secret, and the log is read by more than the
|
||||
// person the link went to (security review, 2026-10-17).
|
||||
func loggedPath(p string) string {
|
||||
for _, prefix := range []string{"/login/reset/", "/confirm/"} {
|
||||
if strings.HasPrefix(p, prefix) && len(p) > len(prefix) {
|
||||
return prefix + "…"
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user