fix(35): WR-03 treat data: in a site name as a data URL only with ; or ,

scriptURLText took data: followed by any non-space character as a data
URL, so names such as Data:Lab, Big Data:Summit and data:Science were
refused on the Branding form and dropped from mail, contrary to the docs.
A data URL now needs its optional MIME type and the ; or , every data URL
has. javascript: and vbscript: keep matching with whitespace after the
colon, since such a URL still runs.
This commit is contained in:
Jakub Zych
2026-11-14 17:40:13 +01:00
parent 31de2ff90f
commit 8deb56ff0b
6 changed files with 24 additions and 12 deletions

View File

@@ -87,7 +87,7 @@ func TestMailSiteNameSafety(t *testing.T) {
t.Run("prose_names_saved", func(t *testing.T) {
// Text shaped like an event handler is text, also after the footer's
// "© <year> " (CR-01 residual, WR-02).
for _, name := range []string{"Big Data: Summit", "Metadata: Weekly", "Online=Shop", "onboarding=team"} {
for _, name := range []string{"Big Data: Summit", "Metadata: Weekly", "Online=Shop", "onboarding=team", "Data:Lab", "Big Data:Summit"} {
saved := env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": name})
if got := dataOf(saved)["data"].(map[string]any)["site_name"]; got != name {
t.Errorf("response site_name = %#v, want %q", got, name)

View File

@@ -289,7 +289,7 @@ The row's `logo_file_id` names a public `system_files` row. Its URL is not store
The row's `site_name` column is `postcard.Brand.SiteName`. The built-in layouts show it in their header (as the logo's alternative text when there is a logo) and footer. While it is empty, and while no Branding row exists, `{{ .siteName }}` is the display name of `mail.from` parsed with `net/mail` (`Acme` for `Acme <no-reply@acme.test>`), or empty when the address has none. The name is HTML-escaped by `html/template` wherever a layout prints it, and printed literally in the header and the footer (`*Acme*` and `Acme <Studio>` appear as typed).
A name `postcard.UnsafeText` refuses (a `javascript:`, `vbscript:` or `data:` URL) never reaches a layout and never stops a send: an unsafe Branding name is treated as empty, so the `mail.from` display name is used if that one is safe, and an unsafe `mail.from` display name gives no name. A warning naming the source (`branding` or `mail.from`), never the value, is logged at most once a minute per source. Prose such as `Big Data: Summit`, and text shaped like an event handler such as `Online=Shop` or `Shop onclick=x`, is a name like any other: it is printed as escaped text, which is never markup. The Branding preview of an unsaved draft leaves such a name out the same way; the admin's Branding form refuses to save it.
A name `postcard.UnsafeText` refuses (a `javascript:` or `vbscript:` URL, or a data URL such as `data:text/html,x`; `Data:Lab` is no data URL) never reaches a layout and never stops a send: an unsafe Branding name is treated as empty, so the `mail.from` display name is used if that one is safe, and an unsafe `mail.from` display name gives no name. A warning naming the source (`branding` or `mail.from`), never the value, is logged at most once a minute per source. Prose such as `Big Data: Summit`, and text shaped like an event handler such as `Online=Shop` or `Shop onclick=x`, is a name like any other: it is printed as escaped text, which is never markup. The Branding preview of an unsaved draft leaves such a name out the same way; the admin's Branding form refuses to save it.
A mail that falls back to its file version because an override failed still gets the Branding.

View File

@@ -9,19 +9,23 @@ import (
)
// scriptURLText finds a script URL in text: javascript: or vbscript: at a
// scheme boundary, or data: at a scheme boundary directly followed by a
// non-space character (a data URL). A boundary is the start of the text or a
// character that cannot belong to a URL scheme, so "Metadata: Weekly" and
// "Big Data: Summit" do not match.
var scriptURLText = regexp.MustCompile(`(?i)(?:^|[^a-z0-9+.\-])(?:(?:javascript|vbscript):|data:\S)`)
// scheme boundary (whitespace may follow the colon of a script URL, so
// "javascript: Acme" matches), or a data URL: data: at a scheme boundary,
// then an optional MIME type and the ";" or "," every data URL has
// ("data:text/html,x", "data:,x"). A boundary is the start of the text or a
// character that cannot belong to a URL scheme, so "Metadata: Weekly" does
// not match; nor do "Big Data: Summit" and "Data:Lab", which are no data
// URLs.
var scriptURLText = regexp.MustCompile(`(?i)(?:^|[^a-z0-9+.\-])(?:(?:javascript|vbscript):|data:\s*(?:[a-z]+/[a-z0-9.+\-]+\s*)?[;,])`)
// UnsafeText reports whether a text that a layout prints, such as the site
// name of the built-in layouts, must be kept out of mail. A text is unsafe
// when, HTML-escaped as html/template prints text, it fails the rendered-HTML
// safety checks in the places the built-in layouts print it, or when it holds
// a script URL: javascript: or vbscript: at a scheme boundary, or a data:
// URL. Escaped text is never markup, so "Big Data: Summit", "Online=Shop" and
// "Shop onclick=x" are safe; "javascript: Acme" is not.
// URL. Escaped text is never markup, so "Big Data: Summit", "Data:Lab",
// "Online=Shop" and "Shop onclick=x" are safe; "javascript: Acme" and
// "data:text/html,x" are not.
func UnsafeText(s string) bool {
return validateHTML(printContext(html.EscapeString(s))) != nil || scriptURLText.MatchString(s)
}

View File

@@ -116,7 +116,7 @@ func TestMailSafetySmoke(t *testing.T) {
})
t.Run("unsafe_site_name", func(t *testing.T) {
ctx := context.Background()
for _, s := range []string{"", "Acme", "Big Data: Summit", "Metadata: Weekly", "Metadata:Weekly", "Acme <Studio>", "*Acme*", "[x](https://example.com)", "Shop onclick=x", "Online=Shop"} {
for _, s := range []string{"", "Acme", "Big Data: Summit", "Metadata: Weekly", "Metadata:Weekly", "Acme <Studio>", "*Acme*", "[x](https://example.com)", "Shop onclick=x", "Online=Shop", "Data:Lab", "Big Data:Summit"} {
if UnsafeText(s) {
t.Errorf("UnsafeText(%q) = true, want false", s)
}

View File

@@ -292,6 +292,11 @@ func TestUnsafeText(t *testing.T) {
"Big Data: Summit",
"Metadata: Weekly",
"Metadata:Weekly",
// data: without a MIME type and ";" or "," is no data URL (WR-03).
"Data:Lab",
"Big Data:Summit",
"data:Science",
"Shop data:x",
"Data: Summit",
"Acme <Studio>",
"*Acme*",
@@ -313,7 +318,10 @@ func TestUnsafeText(t *testing.T) {
"JAVASCRIPT:x",
"Go vbscript:msgbox(1)",
"data:text/html,x",
"Shop data:x",
"data:,x",
"data:;base64,AAAA",
"Shop data:image/png;base64,AAAA",
"data: text/html,x",
} {
if !UnsafeText(s) {
t.Errorf("UnsafeText(%q) = false, want true", s)