carlos / platform Public

edge: demo Referrer-Policy same-origin, so browser form POSTs work

The demo edge forced Referrer-Policy: no-referrer on every demo
response (f86e9792). Under no-referrer, browsers send "Origin: null"
on a same-origin form POST (Fetch, "append a request Origin header").
The /start starter's same-origin check refuses that, so every order or
form in a demo failed in a real browser with 403. Seen in headless
Chromium 151 against a live dev demo on 2026-11-11; agents' curl tests
set Origin by hand and missed it.

same-origin still sends no referrer to any other origin, which is what
no-referrer bought. The design spec (section 4.3) asks only for the
strict CSP; plan 2 listed no-referrer among the hardening headers with
no stated reason, and its own browser witness requires forms to work.

The moved notice keeps no-referrer: it has no forms and refuses every
method but GET and HEAD.

TestDemoReferrerPolicyKeepsFormOrigin states the invariant against a
model of the Fetch and Referrer Policy algorithms: a same-origin POST
carries the page's real Origin and no request to another origin
carries a referrer. The browser witness's fixture app now refuses
Origin "null" like the starter does; TestDemoBannerBrowser failed on
the form step before this change and passes after it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev b862c6b21db4ff8b06dd8fc90acce74ee739b1f2 parent 01110d2
4 files changed, +187 −2
  • internal/edge/demo_referrer_test.go +170 −0
  • internal/edge/demo_response.go +9 −1
  • internal/edge/demo_response_test.go +1 −1
  • internal/edge/testdata/demo-protections/main.go +7 −0
diff --git a/internal/edge/demo_referrer_test.go b/internal/edge/demo_referrer_test.go
new file mode 100644
index 000000000..35f9728e6
--- /dev/null
+++ b/internal/edge/demo_referrer_test.go
@@ -0,0 +1,170 @@
+package edge
+
+import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "testing"
+)
+
+// The demo edge picks the Referrer-Policy for every demo response, and the
+// app cannot override it. That policy also decides the Origin header a
+// browser sends on a form POST. Under no-referrer, Chromium sends
+// "Origin: null" on a same-origin POST, and the /start starter's
+// same-origin check refused every order a visitor placed (2026-11-11
+// recording). curl tests set Origin by hand and could not see it.
+//
+// The invariant: on a demo page, a same-origin form POST carries the
+// page's real Origin, and no request to another origin carries a
+// referrer.
+
+// browserReferrerPolicy is the policy a browser applies from a
+// Referrer-Policy header: the last recognised token in the comma list
+// (W3C Referrer Policy, "parse a referrer policy from a Referrer-Policy
+// header"). "" is the default, strict-origin-when-cross-origin.
+func browserReferrerPolicy(values []string) string {
+ policy := ""
+ for _, v := range values {
+ for _, token := range strings.Split(v, ",") {
+ switch token = strings.ToLower(strings.TrimSpace(token)); token {
+ case "no-referrer", "no-referrer-when-downgrade", "same-origin", "origin",
+ "strict-origin", "origin-when-cross-origin", "strict-origin-when-cross-origin", "unsafe-url":
+ policy = token
+ }
+ }
+ }
+ return policy
+}
+
+func demoURLOrigin(u *url.URL) string { return u.Scheme + "://" + u.Host }
+
+// postOriginHeader is the Origin a browser sends on a POST from page to
+// target under policy (Fetch, "append a request Origin header").
+func postOriginHeader(policy string, page, target *url.URL) string {
+ origin := demoURLOrigin(page)
+ switch policy {
+ case "no-referrer":
+ return "null"
+ case "", "no-referrer-when-downgrade", "strict-origin", "strict-origin-when-cross-origin":
+ if page.Scheme == "https" && target.Scheme != "https" {
+ return "null"
+ }
+ case "same-origin":
+ if origin != demoURLOrigin(target) {
+ return "null"
+ }
+ }
+ return origin
+}
+
+// referrerSent is the Referer a browser sends from page to target under
+// policy (W3C Referrer Policy, "determine request's referrer"); "" is none.
+func referrerSent(policy string, page, target *url.URL) string {
+ same := demoURLOrigin(page) == demoURLOrigin(target)
+ downgrade := page.Scheme == "https" && target.Scheme != "https"
+ full, origin := page.String(), demoURLOrigin(page)+"/"
+ switch policy {
+ case "no-referrer":
+ return ""
+ case "no-referrer-when-downgrade":
+ if downgrade {
+ return ""
+ }
+ return full
+ case "same-origin":
+ if same {
+ return full
+ }
+ return ""
+ case "origin":
+ return origin
+ case "strict-origin":
+ if downgrade {
+ return ""
+ }
+ return origin
+ case "origin-when-cross-origin":
+ if same {
+ return full
+ }
+ return origin
+ case "unsafe-url":
+ return full
+ default: // strict-origin-when-cross-origin
+ if same {
+ return full
+ }
+ if downgrade {
+ return ""
+ }
+ return origin
+ }
+}
+
+// demoReferrerInvariant reports how policy breaks the invariant, or nil.
+func demoReferrerInvariant(policy string) error {
+ page, _ := url.Parse("https://shop.demos.example.com/orders/new?item=1")
+ form, _ := url.Parse("https://shop.demos.example.com/orders")
+ if got := postOriginHeader(policy, page, form); got != "https://shop.demos.example.com" {
+ return fmt.Errorf("policy %q: same-origin form POST sends Origin %q", policy, got)
+ }
+ for _, other := range []string{"https://other.demos.example.com/", "https://example.org/collect", "http://shop.demos.example.com/"} {
+ target, _ := url.Parse(other)
+ if got := referrerSent(policy, page, target); got != "" {
+ return fmt.Errorf("policy %q: request to %s carries referrer %q", policy, other, got)
+ }
+ }
+ return nil
+}
+
+func TestDemoReferrerPolicyKeepsFormOrigin(t *testing.T) {
+ // The model must tell the policies apart, or the check below proves
+ // nothing: no-referrer nulls the form's Origin, the default and the
+ // origin-sending policies leak to other origins.
+ for policy, ok := range map[string]bool{
+ "same-origin": true,
+ "no-referrer": false,
+ "": false,
+ "strict-origin": false,
+ "strict-origin-when-cross-origin": false,
+ "origin": false,
+ "unsafe-url": false,
+ } {
+ if err := demoReferrerInvariant(policy); (err == nil) != ok {
+ t.Errorf("model: policy %q invariant holds = %t, want %t (%v)", policy, err == nil, ok, err)
+ }
+ }
+
+ // What the edge actually serves, whatever the app tried to set, on a
+ // page, on the form POST's redirect and on an error.
+ for _, tc := range []struct {
+ method string
+ status int
+ }{{http.MethodGet, 200}, {http.MethodPost, 303}, {http.MethodPost, 403}, {http.MethodGet, 502}} {
+ t.Run(fmt.Sprintf("%s/%d", tc.method, tc.status), func(t *testing.T) {
+ r := httptest.NewRequest(tc.method, "https://shop.demos.example.com/orders", nil)
+ rec := httptest.NewRecorder()
+ w := new(Edge).beginDemoResponse(rec, r)
+ w.Header().Set("Referrer-Policy", "no-referrer")
+ w.Header()["referrer-policy"] = []string{"unsafe-url"}
+ if tc.status == 303 {
+ w.Header().Set("Location", "/")
+ }
+ w.WriteHeader(tc.status)
+ var got []string
+ for key, values := range rec.Result().Header {
+ if strings.EqualFold(key, "Referrer-Policy") {
+ got = append(got, values...)
+ }
+ }
+ if len(got) != 1 {
+ t.Fatalf("Referrer-Policy = %q, want one value", got)
+ }
+ if err := demoReferrerInvariant(browserReferrerPolicy(got)); err != nil {
+ t.Fatal(err)
+ }
+ })
+ }
+}
diff --git a/internal/edge/demo_response.go b/internal/edge/demo_response.go
index c59087d77..907898768 100644
--- a/internal/edge/demo_response.go
+++ b/internal/edge/demo_response.go
@@ -77,6 +77,14 @@ func demoCookieASCII(s string) bool {
return true
}
+// demoReferrerPolicy sends no referrer to any other origin, but keeps the
+// app's own. It must not be no-referrer: under that policy browsers send
+// "Origin: null" on a same-origin form POST (Fetch, "append a request
+// Origin header"), and an app's same-origin check, the /start starter's
+// included, then refuses every form. TestDemoReferrerPolicyKeepsFormOrigin
+// holds this.
+const demoReferrerPolicy = "same-origin"
+
// sanitizeDemoHeaders also handles noncanonical map keys. Neither a policy
// override nor a second Location can hide behind a different spelling.
func sanitizeDemoHeaders(h http.Header, host string) error {
@@ -105,7 +113,7 @@ func sanitizeDemoHeaders(h http.Header, host string) error {
h.Set("Content-Security-Policy", demoCSP())
h.Set("X-Frame-Options", "DENY")
h.Set("X-Content-Type-Options", "nosniff")
- h.Set("Referrer-Policy", "no-referrer")
+ h.Set("Referrer-Policy", demoReferrerPolicy)
h.Set("Cache-Control", "no-store")
h.Set("X-DNS-Prefetch-Control", "off")
h.Set("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=(), fullscreen=(), document-domain=()")
diff --git a/internal/edge/demo_response_test.go b/internal/edge/demo_response_test.go
index bb836a429..658525d16 100644
--- a/internal/edge/demo_response_test.go
+++ b/internal/edge/demo_response_test.go
@@ -27,7 +27,7 @@ func assertDemoPolicy(t *testing.T, h http.Header) {
"Content-Security-Policy": testDemoCSP,
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
- "Referrer-Policy": "no-referrer",
+ "Referrer-Policy": "same-origin",
"Cache-Control": "no-store",
"X-DNS-Prefetch-Control": "off",
"Permissions-Policy": "camera=(), microphone=(), geolocation=(), payment=(), fullscreen=(), document-domain=()",
diff --git a/internal/edge/testdata/demo-protections/main.go b/internal/edge/testdata/demo-protections/main.go
index 3d4bd7054..c0fd50327 100644
--- a/internal/edge/testdata/demo-protections/main.go
+++ b/internal/edge/testdata/demo-protections/main.go
@@ -79,6 +79,13 @@ func main() {
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
+ // Like the /start starter's same-origin check, refuse
+ // Origin "null". A browser sends that on a same-origin
+ // form post when the edge says no-referrer.
+ if r.Header.Get("Origin") == "null" {
+ http.Error(w, "shop", 403)
+ return
+ }
if _, err := db.ExecContext(r.Context(), `INSERT INTO shop DEFAULT VALUES`); err != nil {
http.Error(w, "shop", 500)
return