carlos / platform Public

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

With no-referrer, Chromium sends Origin: null on a same-origin form
POST, and the demo starter's CSRF check refuses it, so every demo form
failed in a real browser (found by the /start bakery recording).
same-origin still sends no referrer to any other origin. Adds a
regression test for the invariant and makes the browser test's fixture
refuse Origin: null like the starter. Census: internal/edge re-pinned,
Astra approved on #280.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from demo-referrer-same-origin into main.
claude agent, operated by paul@keymail.dev e8a36b14c3eca29358c6be86927dbd0dbab7dbda parent 01110d2
7 files changed, +205 −2
  • docs/superpowers/notes/census-proposal-demo-referrer-same-origin.md +11 −0
  • internal/edge/demo_referrer_test.go +173 −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
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −0
  • internal/provision/testdata/plan14-root-reads-demo-referrer-same-origin.tsv +3 −0

CI passed — run details

diff --git a/docs/superpowers/notes/census-proposal-demo-referrer-same-origin.md b/docs/superpowers/notes/census-proposal-demo-referrer-same-origin.md
new file mode 100644
index 000000000..b3f776b5c
--- /dev/null
+++ b/docs/superpowers/notes/census-proposal-demo-referrer-same-origin.md
@@ -0,0 +1,11 @@
+# Census approval: demo-referrer-same-origin
+
+Ruling `census-ruling-demo-referrer-same-origin`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26).
+
+Envelopes re-pinned: `internal/edge`.
+
+Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner.
+
+Seal: `44b11f63c98595a70e62124034bdf5185c5a58561e99e3e269141557cf13880f` → `44b11f63c98595a70e62124034bdf5185c5a58561e99e3e269141557cf13880f`.
+
+Overlay: `internal/provision/testdata/plan14-root-reads-demo-referrer-same-origin.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/internal/edge/demo_referrer_test.go b/internal/edge/demo_referrer_test.go
new file mode 100644
index 000000000..478c8ab63
--- /dev/null
+++ b/internal/edge/demo_referrer_test.go
@@ -0,0 +1,173 @@
+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, while the edge-provided policy is the one in effect: on a
+// demo page, a same-origin form POST carries the page's real Origin, and
+// no request to another origin carries a referrer. The app's own HTML can
+// weaken the policy (a meta referrer tag, a referrerpolicy attribute), as
+// it could under no-referrer too; the edge does not rewrite documents for
+// that, and this test does not claim it.
+
+// 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
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index 3d7780b7c..8a326b72a 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -175,3 +175,4 @@ plan14-root-reads-activator-settle-wait.tsv b3d35a0bd7c0500c74efbe5e8f566adb6236
plan14-root-reads-lease-checkpoint-no-compaction.tsv b3d35a0bd7c0500c74efbe5e8f566adb623696d31bd9860164907e08a2e79369
plan14-root-reads-member-backups-1.tsv b3d35a0bd7c0500c74efbe5e8f566adb623696d31bd9860164907e08a2e79369
plan14-root-reads-gvisor-empty-execstart.tsv 44b11f63c98595a70e62124034bdf5185c5a58561e99e3e269141557cf13880f
+plan14-root-reads-demo-referrer-same-origin.tsv 44b11f63c98595a70e62124034bdf5185c5a58561e99e3e269141557cf13880f
diff --git a/internal/provision/testdata/plan14-root-reads-demo-referrer-same-origin.tsv b/internal/provision/testdata/plan14-root-reads-demo-referrer-same-origin.tsv
new file mode 100644
index 000000000..aa6cf7d38
--- /dev/null
+++ b/internal/provision/testdata/plan14-root-reads-demo-referrer-same-origin.tsv
@@ -0,0 +1,3 @@
+# Census overlay demo-referrer-same-origin, written by census-propose (#369). census-ruling-demo-referrer-same-origin, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-demo-referrer-same-origin.md.
+# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+@coverage internal/edge 90c08695bec6e2580594f137238312e3887a9253c6dc1e7c13e77a564e97abf2