Files
selfpost/internal/web/security_test.go
T
mixandClaude Opus 5.5 9ca50d33fd
test / test (push) Canceled after 0s
web: Referrer-Policy same-origin; a password shown once emptied on pagehide
D-B3. Fetch Standard § 3.2 "append a request `Origin` header": under
Referrer-Policy no-referrer a request that is not GET/HEAD and not CORS -
a form POST, a navigation - gets Origin "null". originAllowed() refuses
"null" whenever Sec-Fetch-Site is absent, and Fetch Metadata § 3 sends
Sec-Fetch-Site only to a potentially trustworthy URL: every form,
sign-in included, failed in Safari before 16.4 and in any browser on a
plain-HTTP panel at a name. same-origin nulls Origin only for a request
not same origin with its URL and still sends no referrer to another
site. The comments of the origin check say so.

D-B13. noStore keeps the page out of HTTP caches (RFC 9111 § 5.2.2.5);
a history mechanism "might honor" it (§ 6), and Pragma in a response
"was never specified" (§ 5.4) - it goes. CopyField.Secret marks the
password of out-app-created data-secret, and panel.js empties such
fields on pagehide (HTML § 7.5.9, "unload a Document", step 10.2: fired
whether or not the document is kept for history traversal).

Measured 2026-10-15 in Chrome 152 (the desktop app's browser), a local
probe page with Cache-Control: no-store and a data-secret field: Back
reloads it (pageshow persisted=false, a new load), so this browser keeps
no-store pages out of its back/forward cache; the handler, fired on the
same page, empties the field. Firefox and Safari not measured.

Tests: the header value; the created page has the password data-secret
and the login not, no Pragma. Stand (2026-10-15, make e2e, exit 0): new
form_post_with_the_browsers_origin - Referrer-Policy: same-origin on a
page; a POST carrying the page's own Origin and no Sec-Fetch-Site, what
a browser sends now, passes; one carrying Origin: null, what it sent
before, is 403. (A first run failed where the e2e client and a view test
scraped the old input markup; both read data-secret now.) go vet and go
test ./... pass on the server (24 packages).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 01:04:45 +03:00

130 lines
5.0 KiB
Go

package web
import (
"net/http"
"net/http/httptest"
"testing"
)
// serveSecured runs one request through the security middleware and reports
// what came out. The wrapped handler answers 200, so any other status is the
// middleware's doing.
func serveSecured(cookieSecure bool, r *http.Request) *httptest.ResponseRecorder {
s := &Server{cfg: Config{CookieSecure: cookieSecure}}
h := s.secure(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
rec := httptest.NewRecorder()
h.ServeHTTP(rec, r)
return rec
}
// post builds a request as a browser would send it to the panel's own host.
func post(secFetchSite, origin string) *http.Request {
r := httptest.NewRequest(http.MethodPost, "http://panel.example.com/domains", nil)
r.Host = "panel.example.com"
if secFetchSite != "" {
r.Header.Set("Sec-Fetch-Site", secFetchSite)
}
if origin != "" {
r.Header.Set("Origin", origin)
}
return r
}
// The full matrix the origin check has to get right. The row that
// matters most is "same-site": a neighbouring host on example.com is same-site
// as far as the session cookie's SameSite=Lax is concerned, so this check is
// the only thing standing between it and a forged POST.
func TestOriginCheck(t *testing.T) {
tests := []struct {
name string
req *http.Request
wantRejected bool
}{
{"no headers at all is let through (accepted risk)", post("", ""), false},
{"same-origin", post("same-origin", "https://panel.example.com"), false},
{"same-site, i.e. a neighbouring subdomain", post("same-site", ""), true},
{"cross-site", post("cross-site", "https://evil.example.net"), true},
{"none, a navigation with no initiator", post("none", ""), true},
{"Sec-Fetch-Site outranks a matching Origin", post("cross-site", "https://panel.example.com"), true},
{"Origin host matches", post("", "https://panel.example.com"), false},
{"Origin host differs", post("", "https://evil.example.net"), true},
{"Origin differs from a neighbour only in host", post("", "https://cms.example.com"), true},
{"opaque null Origin", post("", "null"), true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rec := serveSecured(true, tc.req)
rejected := rec.Code == http.StatusForbidden
if rejected != tc.wantRejected {
t.Fatalf("status = %d, rejected = %t, want rejected = %t", rec.Code, rejected, tc.wantRejected)
}
})
}
}
// The proxy terminates TLS and forwards plain HTTP, so the panel never learns
// its own external scheme: comparing it against the https in Origin would
// reject every legitimate form submission in the documented deployment.
func TestOriginCheckIgnoresScheme(t *testing.T) {
rec := serveSecured(true, post("", "https://panel.example.com"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200: an https Origin against a plain-HTTP panel must pass", rec.Code)
}
}
// Every GET route in the panel is a read, so cross-origin reads have nothing
// to change and must not be blocked — the HTMX polling fragments included.
func TestOriginCheckExemptsReads(t *testing.T) {
for _, method := range []string{http.MethodGet, http.MethodHead} {
r := httptest.NewRequest(method, "http://panel.example.com/status/fragment", nil)
r.Host = "panel.example.com"
r.Header.Set("Sec-Fetch-Site", "cross-site")
if rec := serveSecured(true, r); rec.Code != http.StatusOK {
t.Errorf("%s: status = %d, want 200", method, rec.Code)
}
}
}
func TestSecurityHeaders(t *testing.T) {
rec := serveSecured(true, httptest.NewRequest(http.MethodGet, "http://panel.example.com/status", nil))
want := map[string]string{
"Content-Security-Policy": contentSecurityPolicy,
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "same-origin",
"Strict-Transport-Security": strictTransportSecurity,
}
for name, value := range want {
if got := rec.Header().Get(name); got != value {
t.Errorf("%s = %q, want %q", name, got, value)
}
}
}
// HSTS is the one header that must not be sent unconditionally: on the
// plain-HTTP development instance it would pin the browser to a scheme that
// instance does not speak.
func TestHSTSOnlyWhenSecure(t *testing.T) {
rec := serveSecured(false, httptest.NewRequest(http.MethodGet, "http://panel.example.com/status", nil))
if got := rec.Header().Get("Strict-Transport-Security"); got != "" {
t.Errorf("Strict-Transport-Security = %q on a non-secure deployment, want none", got)
}
if got := rec.Header().Get("Content-Security-Policy"); got == "" {
t.Error("the other security headers must still be sent without TLS")
}
}
// A rejected request is still a response the browser renders, so it carries
// the same headers as any other.
func TestRejectedRequestKeepsSecurityHeaders(t *testing.T) {
rec := serveSecured(true, post("cross-site", ""))
if rec.Code != http.StatusForbidden {
t.Fatalf("status = %d, want 403", rec.Code)
}
if got := rec.Header().Get("Content-Security-Policy"); got != contentSecurityPolicy {
t.Errorf("Content-Security-Policy = %q on the 403 response, want the policy", got)
}
}