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:
@@ -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, 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 `© <year> `) 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 `© <year> ` 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. |
|
||||
|
||||
@@ -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 "© <year> "), 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 "© <year> " 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 "<p>© 2000 " + esc + "</p><span>" + esc + "</span><img alt=\"" + esc + "\">"
|
||||
}
|
||||
|
||||
// urlAttributes are the attributes that hold one URL. Their decoded value is
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user