diff --git a/internal/check/csp.go b/internal/check/csp.go index 7e74c43..13fa108 100644 --- a/internal/check/csp.go +++ b/internal/check/csp.go @@ -8,7 +8,8 @@ import ( ) // policy works out the Content-Security-Policy the built pages need: their -// own origin, the hosts they load scripts, styles, images and frames from, +// own origin, the hosts they load scripts, styles, images and frames from +// and send forms to, // 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. @@ -75,6 +76,13 @@ func policy(pages []*page) string { add("frame-src", "'self'") } } + // Forms that send to another site: a payment provider's checkout, + // say. Without its host in form-action the browser won't send them. + for _, s := range pg.forms { + if o := origin(s); o != "" { + add("form-action", o) + } + } // A counter the consent script loads (only after consent): its own // script, where it reports, and its tracking pixel. for _, h := range pg.consentHosts { @@ -115,7 +123,7 @@ func policy(pages []*page) string { } else { parts = append(parts, "object-src 'none'") } - parts = append(parts, "base-uri 'self'", "form-action 'self'", "frame-ancestors 'self'") + parts = append(parts, "base-uri 'self'", list("form-action", "'self'"), "frame-ancestors 'self'") return strings.Join(parts, "; ") } diff --git a/internal/check/csp_test.go b/internal/check/csp_test.go index 733df71..88d9179 100644 --- a/internal/check/csp_test.go +++ b/internal/check/csp_test.go @@ -28,3 +28,14 @@ func TestPolicyFramesAndObjects(t *testing.T) { t.Errorf("third-party object: %s", p) } } + +func TestPolicyFormAction(t *testing.T) { + p := policy([]*page{{forms: []string{"/_hotdog/form/contact"}}}) + if !strings.Contains(p, "form-action 'self';") { + t.Errorf("own form: %s", p) + } + p = policy([]*page{{forms: []string{"https://shop.example.com/cart/checkout", "https://pay.example.org/x"}}, {forms: []string{"https://shop.example.com/cart/checkout?x=1"}}}) + if !strings.Contains(p, "form-action 'self' https://pay.example.org https://shop.example.com;") { + t.Errorf("checkout forms: %s", p) + } +} diff --git a/internal/check/pages.go b/internal/check/pages.go index 1b9bb5b..d72bb93 100644 --- a/internal/check/pages.go +++ b/internal/check/pages.go @@ -33,6 +33,7 @@ type page struct { scriptLinks []string // javascript: (and vbscript:, data:text/html) links frames []string // iframe sources objects []string // and + forms []string // where forms send:
,