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