diff --git a/docs/services/mail.md b/docs/services/mail.md index b7857382..768efe5b 100644 --- a/docs/services/mail.md +++ b/docs/services/mail.md @@ -85,7 +85,7 @@ The settings also hold a logo. A layout shows it where it uses `{{ .logoUrl }}`: The settings also hold a site name, given to layouts as `{{ .siteName }}`. The built-in layouts show it in the header (as the logo's alternative text when there is a logo) and in the footer. Left empty, the display name of `mail.from` is used (`Acme` for `Acme `), so mail from an application that never saved Branding still carries a name. -A site name mail could not show safely is refused when the settings are saved: one holding a `javascript:`, `vbscript:` or `data:` address, or text that would read as markup, such as `onclick=`. Ordinary text with a colon, such as `Big Data: Summit`, is accepted. A name stored before this check, or a `mail.from` display name of that kind, is left out of mail at send time (the next name in line is used, or none), with a warning in the log; the message is still sent. +A site name mail could not show safely is refused when the settings are saved: one holding a `javascript:`, `vbscript:` or `data:` address, or text that would read as markup where the layouts print it, such as `onclick=`, or `Online=Shop`, which the footer prints after `© `. Ordinary text with a colon, such as `Big Data: Summit`, is accepted. A name stored before this check, or a `mail.from` display name of that kind, is left out of mail at send time (the next name in line is used, or none), with a warning in the log; the message is still sent. A mail client needs an absolute address, so the logo's URL is built at send time from the configuration: `storage.uploads.public_url` when it is set, otherwise `app.url` with `storage.uploads.public_path_prefix`; see [Absolute URLs for mail](storage.md#absolute-urls-for-mail). Set one of them, or the logo is left out and a warning is logged. diff --git a/modules/cabana/mail_site_name_postgres_test.go b/modules/cabana/mail_site_name_postgres_test.go index ce044329..a6699cbd 100644 --- a/modules/cabana/mail_site_name_postgres_test.go +++ b/modules/cabana/mail_site_name_postgres_test.go @@ -82,6 +82,15 @@ func TestMailSiteNameSafety(t *testing.T) { if got := storedSiteName(t, env); got != "Acme Mail" { t.Fatalf("the refused pl save stored site_name %q", got) } + + // A name unsafe only where the layouts print it, after the footer's + // "© ", is refused as well (CR-01 residual). + for _, name := range []string{"Online=Shop", "onboarding=team"} { + env.expectFieldRefusal(t, http.StatusUnprocessableEntity, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": name}, "site_name", "cannot be shown in mail", false) + if got := storedSiteName(t, env); got != "Acme Mail" { + t.Fatalf("the refused save of %q stored site_name %q", name, got) + } + } }) t.Run("prose_names_saved", func(t *testing.T) { diff --git a/modules/postcard/README.md b/modules/postcard/README.md index 15c2b863..a54ef3c4 100644 --- a/modules/postcard/README.md +++ b/modules/postcard/README.md @@ -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 `), 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 ` appear as typed). -A name `postcard.UnsafeText` refuses (a `javascript:`, `vbscript:` or `data:` URL, or text that would read as markup such as `Shop onclick=x`) 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` is a name like any other. 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:`, `vbscript:` or `data:` URL, or text that would read as markup where the layouts print it, such as `Shop onclick=x`, or `Online=Shop` after the footer's `© `) 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` is a name like any other. 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. @@ -414,7 +414,7 @@ A draft is rendered by the code that renders a send, with the mailer's CSS and t | `postcard.Brand` | The Branding: thirteen colours, `postcard.Brand.LogoURL`, the logo's absolute URL, and `postcard.Brand.SiteName`, the name the built-in layouts show. | | `postcard.DefaultBrand` | WinterCMS's default mail colours. | | `postcard.CompileBrandCSS` | Checks a brand's colours and returns the brand CSS compiled from them. | -| `postcard.UnsafeText` | Reports whether a text a layout prints, such as the site name, must be kept out of mail: it holds a `javascript:`, `vbscript:` or `data:` URL, or fails the rendered-HTML safety checks once HTML-escaped. | +| `postcard.UnsafeText` | Reports whether a text a layout prints, such as the site name, must be kept out of mail: it holds a `javascript:`, `vbscript:` or `data:` URL, or fails the rendered-HTML safety checks once HTML-escaped and placed where the built-in layouts print it (after `© ` in the footer, in the header). | | `postcard.Migrations` | The gormigrate set that creates the mail tables; lagoon runs it under the `summercms.postcard` history. | | `postcard.TemplatesTable`, `postcard.LayoutsTable`, `postcard.PartialsTable`, `postcard.BrandTable`, `postcard.ImageLibraryTable` | The names of the mail tables, for code that defines models over them. | | `postcard.SyncIndex` | Upserts one index row per file template, layout and partial of a catalog; mirrors the file into rows that are not customized and refreshes `file_hash` and the template's `base_code` on all. | diff --git a/modules/postcard/safety.go b/modules/postcard/safety.go index bfdecb33..f7b652b6 100644 --- a/modules/postcard/safety.go +++ b/modules/postcard/safety.go @@ -18,12 +18,22 @@ var scriptURLText = regexp.MustCompile(`(?i)(?:^|[^a-z0-9+.\-])(?:(?:javascript| // 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 (for example event-handler-shaped text such as -// "Shop onclick=x"), or when it holds a script URL: javascript: or vbscript: -// at a scheme boundary, or a data: URL. "Big Data: Summit" is safe; +// safety checks in the places the built-in layouts print it (for example +// event-handler-shaped text such as "Shop onclick=x", or "Online=Shop" after +// the footer's "© "), or when it holds a script URL: javascript: or +// vbscript: at a scheme boundary, or a data: URL. "Big Data: Summit" is safe; // "javascript: Acme" is not. func UnsafeText(s string) bool { - return validateHTML(html.EscapeString(s)) != nil || scriptURLText.MatchString(s) + return validateHTML(printContext(html.EscapeString(s))) != nil || scriptURLText.MatchString(s) +} + +// printContext is the markup the built-in layouts print an escaped site name +// in: after "© " in the footer paragraph, in the header's span and in +// the logo's alt attribute. A text is checked there, not on its own, so a +// check that depends on what precedes the text (a whitespace-prefixed +// handler) sees what a send renders. +func printContext(esc string) string { + return "

© 2000 " + esc + "

" + esc + "\""" } // urlAttributes are the attributes that hold one URL. Their decoded value is diff --git a/modules/postcard/sitename_test.go b/modules/postcard/sitename_test.go index 81510dfd..0c36fd8f 100644 --- a/modules/postcard/sitename_test.go +++ b/modules/postcard/sitename_test.go @@ -145,6 +145,10 @@ func TestUnsafeSiteNameDropped(t *testing.T) { {"script_branding", "javascript:alert(1)", "Acme ", "Acme", []string{"javascript:", "alert"}}, {"handler_branding", "Shop onclick=x", "Acme ", "Acme", []string{"onclick", "Shop"}}, {"both_unsafe", "javascript: Acme", `"vbscript:x" `, "", []string{"javascript:", "vbscript:", "Acme"}}, + // Unsafe only where the layouts print it: the footer's "© " + // puts whitespace before the on= (CR-01 residual). + {"context_handler_branding", "Online=Shop", "Acme ", "Acme", []string{"Online"}}, + {"context_handler_both", "onboarding=team", `"Online=Shop" `, "", []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" `}, 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 + `" `}, 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 "© ". + "Online=Shop", + "onboarding=team", } { if !UnsafeText(s) { t.Errorf("UnsafeText(%q) = false, want true", s)