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';") {