check -csp: let forms send where they say #35

Merged
jcoffey-dev merged 1 commits from check/form-action into main 2026-10-17 01:19:48 +00:00
3 changed files with 30 additions and 2 deletions

No files matched your search

+10 -2
View File
@@ -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, "; ")
}
+11
View File
@@ -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)
}
}
+9
View File
@@ -33,6 +33,7 @@ type page struct {
scriptLinks []string // javascript: (and vbscript:, data:text/html) links
frames []string // iframe sources
objects []string // <object data> and <embed src>
forms []string // where forms send: <form action>, <button formaction>
styles []string // external stylesheets
images []string // img/source/video sources
imgNoAlt int
@@ -209,6 +210,14 @@ func parsePage(data []byte, file, u string) *page {
pg.insecure = append(pg.insecure, src)
}
}
case "form", "button", "input":
k := "formaction"
if t.Data == "form" {
k = "action"
}
if a, ok := attr(t, k); ok && a != "" {
pg.forms = append(pg.forms, a)
}
case "object", "embed":
k := "data"
if t.Data == "embed" {