check -csp: allow Turnstile frame #43

Merged
jcoffey-dev merged 1 commits from fix/csp-turnstile-frame into main 2026-11-21 19:45:01 +00:00
2 changed files with 23 additions and 0 deletions

No files matched your search

+10
View File
@@ -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 {
+13
View File
@@ -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';") {