fix(postcard,cabana): unsafe site names never stop a send and are refused at save

- postcard.UnsafeText: a text a layout prints is unsafe when, escaped,
  it fails the rendered-HTML checks or holds a script or data URL at a
  scheme boundary ("Big Data: Summit" is safe)
- siteNameFrom/layoutSiteName: an unsafe Branding name falls back to the
  mail.from display name, an unsafe one of those gives no name; one
  rate-limited warning per source (site_name:branding, site_name:mail.from),
  never the value; file version, override, fallback and editor alike
- the Branding preview of an unsafe draft name renders without it
- MailBrandSetting.BeforeValidate: 422 on site_name with
  backend::lang.mail.brand.site_name_unsafe (en/pl)
- settings Put localizes a BeforeValidate refusal's phrase keys, as a
  controller save does
- TestMailSafetySmoke/unsafe_site_name, TestMailBrandSiteName/unsafe_name_refused
This commit is contained in:
Jakub Zych
2026-11-14 15:20:29 +01:00
parent 13ab905f8d
commit 3a725469c6
12 changed files with 180 additions and 16 deletions

View File

@@ -149,11 +149,13 @@ func (m *mailer) render(ctx context.Context, msg Message) (RenderedMessage, erro
// options returns the render options of the file version: mail.css, and
// either the saved Branding (its compiled CSS and its logo) or, while none
// has been saved, mail.brandCss. A Branding whose colours do not compile
// keeps mail.brandCss. The site name is the Branding's, else the display
// name of mail.from.
// keeps mail.brandCss. The site name is layoutSiteName's: the Branding's,
// else the display name of mail.from, leaving out a name that fails the
// safety check. The override render and the override-failure fallback are
// built from these options, so all of them carry the same checked name.
func (m *mailer) options(brand *Brand) renderOptions {
opts := renderOptions{
css: m.opts.CSS, brandCSS: m.opts.BrandCSS, siteName: siteName(brand, m.opts.From), year: time.Now().Year(),
css: m.opts.CSS, brandCSS: m.opts.BrandCSS, siteName: m.layoutSiteName(brand), year: time.Now().Year(),
inline: m.inline, warn: m.warn,
}
if brand == nil {
@@ -172,18 +174,51 @@ func (m *mailer) options(brand *Brand) renderOptions {
// siteName returns the name the built-in layouts show: the Branding's site
// name, trimmed, else the display name of the sender address from, else
// empty.
// empty. A name UnsafeText refuses is skipped.
func siteName(brand *Brand, from string) string {
name, _ := siteNameFrom(brand, from)
return name
}
// siteNameFrom returns the first non-empty candidate UnsafeText accepts, of
// the trimmed Branding site name and the trimmed display name of from. In
// skipped it returns the source ("branding" or "mail.from") of the first
// non-empty candidate it refused.
func siteNameFrom(brand *Brand, from string) (name, skipped string) {
type candidate struct{ source, name string }
var candidates []candidate
if brand != nil {
if name := strings.TrimSpace(brand.SiteName); name != "" {
return name
candidates = append(candidates, candidate{"branding", strings.TrimSpace(brand.SiteName)})
}
if addr, err := netmail.ParseAddress(from); err == nil {
candidates = append(candidates, candidate{"mail.from", strings.TrimSpace(addr.Name)})
}
for _, c := range candidates {
if c.name == "" {
continue
}
if UnsafeText(c.name) {
if skipped == "" {
skipped = c.source
}
continue
}
return c.name, skipped
}
addr, err := netmail.ParseAddress(from)
if err != nil {
return ""
return "", skipped
}
// layoutSiteName is the site name the layouts get for brand and mail.from.
// A name that fails the safety check is left out, so it can never stop a
// send; a warning names its source, never its value, at most once per
// warnInterval.
func (m *mailer) layoutSiteName(brand *Brand) string {
name, skipped := siteNameFrom(brand, m.opts.From)
if skipped != "" {
m.warn("site_name:"+skipped, "postcard: site name left out of the layouts, it fails the mail safety check",
slog.String("source", skipped))
}
return strings.TrimSpace(addr.Name)
return name
}
// overrides asks the source once. A failed lookup (no connection, tables not