fix(35): WR-02 drop the whole-document event-handler regex

validateHTML ran (?i)\son[a-z]+\s*= over the rendered document, text
included, so a variable such as "buy one = get one free" stopped its own
message, and it is what made Online=Shop in the footer a send failure.
The tokenizer walk refuses every on* attribute, and since CR-02 it also
reads raw-text element and comment content as markup, so the regex is
dropped. Text shaped like a handler is now sent; such site names are shown
and saved, and the Branding refusal copy no longer mentions onclick=.
This commit is contained in:
Jakub Zych
2026-11-14 17:37:52 +01:00
parent baba524caf
commit 31de2ff90f
11 changed files with 79 additions and 59 deletions

View File

@@ -80,26 +80,33 @@ func siteNameWarnings(logs string) int {
}
// TestSiteNameSend covers the CR-01 regression: a site name that reads as
// prose with a colon, from mail.from or from the Branding, is shown in the
// header and the footer of the built-in layouts and never stops a send, on
// every render path.
// prose with a colon, or text shaped like an event handler (Online=Shop,
// which the footer prints after "© <year> "), from mail.from or from the
// Branding, is shown in the header and the footer of the built-in layouts
// and never stops a send, on every render path.
func TestSiteNameSend(t *testing.T) {
t.Parallel()
const from = `"Metadata: Weekly" <x@example.com>`
sources := []struct {
name string
from string
brand *Brand
want string
}{
{"mail_from", nil, "Metadata: Weekly"},
{"branding", &Brand{SiteName: "Big Data: Summit"}, "Big Data: Summit"},
{"mail_from", from, nil, "Metadata: Weekly"},
{"branding", from, &Brand{SiteName: "Big Data: Summit"}, "Big Data: Summit"},
{"mail_from_handler_text", `"Online=Shop" <x@example.com>`, nil, "Online=Shop"},
{"mail_from_handler_text_lower", `"onboarding=team" <x@example.com>`, nil, "onboarding=team"},
{"branding_handler_text", from, &Brand{SiteName: "Online=Shop"}, "Online=Shop"},
{"branding_handler_text_lower", from, &Brand{SiteName: "onboarding=team"}, "onboarding=team"},
{"branding_handler_text_spaced", from, &Brand{SiteName: "Shop onclick=x"}, "Shop onclick=x"},
}
for _, l := range siteNameLayouts {
for _, src := range sources {
for _, p := range siteNamePaths {
t.Run(l.layout+"/"+src.name+"/"+p.name, func(t *testing.T) {
t.Parallel()
m, drv := siteNameMailer(t, Options{From: from}, siteNameOverrides(src.brand, p.template))
m, drv := siteNameMailer(t, Options{From: src.from}, siteNameOverrides(src.brand, p.template))
logs := captureLog(m)
msg := sendTemplate(t, m, drv, l.template, inlineVars)
assertPath(t, msg, l.fileText, p.want, p.fallback, logs.String())
@@ -122,6 +129,17 @@ func TestSiteNameSend(t *testing.T) {
}
assertSiteName(t, msg, "Big Data: Summit")
})
// Text shaped like an event handler is text (WR-02): a variable such
// as a user's name never stops its own message.
t.Run(l.layout+"/handler_shaped_variable", func(t *testing.T) {
t.Parallel()
m, drv := siteNameMailer(t, Options{From: from}, nil)
const name = "Ada, buy one = get one free online = yes"
msg := sendTemplate(t, m, drv, l.template, map[string]any{"Name": name})
if !strings.Contains(msg.HTML, name) || !strings.Contains(msg.Text, name) {
t.Errorf("the message lacks %q:\n%s\n%s", name, msg.HTML, msg.Text)
}
})
}
}
@@ -143,12 +161,7 @@ func TestUnsafeSiteNameDropped(t *testing.T) {
bad []string
}{
{"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 {
@@ -198,8 +211,7 @@ func TestUnsafeSiteNameDropped(t *testing.T) {
for _, l := range siteNameLayouts {
for _, from := range []struct{ name, value string }{
{"script", "javascript: Acme"},
{"context_handler", "Online=Shop"},
{"context_handler_lower", "onboarding=team"},
{"vbscript", "vbscript:x"},
} {
t.Run(l.layout+"/mail_from_source/"+from.name, func(t *testing.T) {
t.Parallel()
@@ -270,8 +282,8 @@ func TestUnsafeSiteNameDropped(t *testing.T) {
}
// TestUnsafeText pins the rule a layout's printed text is held to: prose
// with a colon is safe, a script URL at a scheme boundary, a data URL or
// event-handler-shaped text is not.
// with a colon and event-handler-shaped text are safe, a script URL at a
// scheme boundary or a data URL is not.
func TestUnsafeText(t *testing.T) {
t.Parallel()
for _, s := range []string{
@@ -284,6 +296,12 @@ func TestUnsafeText(t *testing.T) {
"Acme <Studio>",
"*Acme*",
"[x](https://example.com)",
// Text shaped like an event handler is text, also where the
// layouts print it, after "© <year> " (CR-01, WR-02).
"Shop onclick=x",
"x onload = 1",
"Online=Shop",
"onboarding=team",
} {
if UnsafeText(s) {
t.Errorf("UnsafeText(%q) = true, want false", s)
@@ -296,11 +314,6 @@ func TestUnsafeText(t *testing.T) {
"Go vbscript:msgbox(1)",
"data:text/html,x",
"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)