From 0fcf2ce7d29f354792860385e47d9d288073fc48 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Sat, 21 Nov 2026 12:42:43 -0700 Subject: [PATCH] check -csp: allow Turnstile's frame A form with turnstile_sitekey loads challenges.cloudflare.com's script, which draws the check in a frame from the same host. The policy put the host in script-src only and left frame-src 'none', so the widget was blocked. Scripts known to frame their own host now add it to frame-src. --- internal/check/csp.go | 10 ++++++++++ internal/check/csp_test.go | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/internal/check/csp.go b/internal/check/csp.go index 13fa108..bf2b8f6 100644 --- a/internal/check/csp.go +++ b/internal/check/csp.go @@ -13,6 +13,13 @@ import ( // and 'unsafe-inline' only where a page still has inline code or styles. It // can't see what scripts fetch at run time (connect-src), so a site adds // those hosts itself. +// framingScripts are scripts that draw their widget in a frame from their +// own host, so a page that loads one needs that host in frame-src as well. +// Turnstile is what a form's turnstile_sitekey adds. +var framingScripts = map[string]bool{ + "https://challenges.cloudflare.com": true, +} + func policy(pages []*page) string { sets := map[string]map[string]bool{} add := func(dir, src string) { @@ -44,6 +51,9 @@ func policy(pages []*page) string { for _, s := range pg.scripts { if o := origin(s); o != "" { add("script-src", o) + if framingScripts[o] { + add("frame-src", o) + } } } for _, s := range pg.styles { diff --git a/internal/check/csp_test.go b/internal/check/csp_test.go index 88d9179..9b1ac0c 100644 --- a/internal/check/csp_test.go +++ b/internal/check/csp_test.go @@ -29,6 +29,19 @@ func TestPolicyFramesAndObjects(t *testing.T) { } } +func TestPolicyTurnstileFrame(t *testing.T) { + // Turnstile's script draws the check in a frame from its own host. + p := policy([]*page{{scripts: []string{"https://challenges.cloudflare.com/turnstile/v0/api.js"}}}) + if !strings.Contains(p, "script-src 'self' https://challenges.cloudflare.com;") || !strings.Contains(p, "frame-src https://challenges.cloudflare.com;") { + t.Errorf("turnstile: %s", p) + } + // Any other third-party script leaves frames shut. + p = policy([]*page{{scripts: []string{"https://cdn.example.net/x.js"}}}) + if !strings.Contains(p, "frame-src 'none'") { + t.Errorf("plain script: %s", p) + } +} + func TestPolicyFormAction(t *testing.T) { p := policy([]*page{{forms: []string{"/_hotdog/form/contact"}}}) if !strings.Contains(p, "form-action 'self';") { -- 2.54.0