fix(35): CR-01 check the site name where the layouts print it

UnsafeText checked the escaped name on its own, so a name starting with
on<letters>= (Online=Shop, onboarding=team) passed, was saved, and then
failed every default/system-layout send once the footer printed it after
"© <year> ". The name is now checked in the footer, header span and alt
contexts, so such a name is dropped at render and refused on the Branding
form instead of stopping mail.
This commit is contained in:
Jakub Zych
2026-11-14 17:25:53 +01:00
parent 980ba48764
commit 2dbee30530
5 changed files with 67 additions and 20 deletions

View File

@@ -145,6 +145,10 @@ func TestUnsafeSiteNameDropped(t *testing.T) {
{"script_branding", "javascript:alert(1)", "Acme <x@example.com>", "Acme", []string{"javascript:", "alert"}},
{"handler_branding", "Shop onclick=x", "Acme <x@example.com>", "Acme", []string{"onclick", "Shop"}},
{"both_unsafe", "javascript: Acme", `"vbscript:x" <x@example.com>`, "", []string{"javascript:", "vbscript:", "Acme"}},
// Unsafe only where the layouts print it: the footer's "© <year> "
// puts whitespace before the on<letters>= (CR-01 residual).
{"context_handler_branding", "Online=Shop", "Acme <x@example.com>", "Acme", []string{"Online"}},
{"context_handler_both", "onboarding=team", `"Online=Shop" <x@example.com>`, "", []string{"onboarding", "Online"}},
}
for _, l := range siteNameLayouts {
for _, tc := range cases {
@@ -189,20 +193,31 @@ func TestUnsafeSiteNameDropped(t *testing.T) {
}
}
t.Run("mail_from_source", func(t *testing.T) {
t.Parallel()
// No Branding, an unsafe mail.from display name: no name, and the
// warning names mail.from.
m, drv := siteNameMailer(t, Options{From: `"javascript: Acme" <x@example.com>`}, nil)
logs := captureLog(m)
msg := sendTemplate(t, m, drv, "acme.site::mail.hello", inlineVars)
if got := footerText(t, msg.HTML); got != copyright() {
t.Errorf("footer = %q, want %q", got, copyright())
// No Branding, an unsafe mail.from display name: no name, and the
// warning names mail.from. Configuration alone never stops a send.
for _, l := range siteNameLayouts {
for _, from := range []struct{ name, value string }{
{"script", "javascript: Acme"},
{"context_handler", "Online=Shop"},
{"context_handler_lower", "onboarding=team"},
} {
t.Run(l.layout+"/mail_from_source/"+from.name, func(t *testing.T) {
t.Parallel()
m, drv := siteNameMailer(t, Options{From: `"` + from.value + `" <x@example.com>`}, nil)
logs := captureLog(m)
msg := sendTemplate(t, m, drv, l.template, inlineVars)
if got := footerText(t, msg.HTML); got != copyright() {
t.Errorf("footer = %q, want %q", got, copyright())
}
if strings.Contains(msg.HTML, from.value) || strings.Contains(msg.Text, from.value) {
t.Errorf("%q reached the message:\n%s", from.value, msg.HTML)
}
if out := logs.String(); siteNameWarnings(out) != 1 || !strings.Contains(out, "source=mail.from") || strings.Contains(out, from.value) {
t.Errorf("log = %q, want one warning naming mail.from and not the value", out)
}
})
}
if out := logs.String(); siteNameWarnings(out) != 1 || !strings.Contains(out, "source=mail.from") || strings.Contains(out, "javascript") {
t.Errorf("log = %q, want one warning naming mail.from and not the value", out)
}
})
}
t.Run("editor", func(t *testing.T) {
t.Parallel()
@@ -230,6 +245,16 @@ func TestUnsafeSiteNameDropped(t *testing.T) {
}
}
// A name unsafe only in the footer's context does not refuse a save
// either (the template's layout renders the footer).
edCtx, _ := newEditor(t, &Overrides{Brand: &Brand{SiteName: "Online=Shop"}})
if err := edCtx.Validate(ctx, tmpl); err != nil {
t.Errorf("a template draft under a stored Online=Shop was refused: %+v", err)
}
if err := edCtx.Validate(ctx, layout); err != nil {
t.Errorf("a layout draft under a stored Online=Shop was refused: %+v", err)
}
// A Branding draft with an unsafe name previews without it.
prev, err := ed.Preview(ctx, Draft{Kind: KindBrand, Brand: &Brand{SiteName: "javascript: Acme"}})
if err != nil {
@@ -273,6 +298,9 @@ func TestUnsafeText(t *testing.T) {
"Shop data:x",
"Shop onclick=x",
"x onload = 1",
// Unsafe where the layouts print it, after "© <year> ".
"Online=Shop",
"onboarding=team",
} {
if !UnsafeText(s) {
t.Errorf("UnsafeText(%q) = false, want true", s)