Files
summercms/modules/postcard/safety_smoke_test.go
Jakub Zych 3a725469c6 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
2026-11-14 15:20:29 +01:00

173 lines
7.4 KiB
Go

package postcard
import (
"bytes"
"context"
"log/slog"
"strings"
"testing"
)
// safetyLayouts are the blog fixture's templates on the default and the
// system layout.
var safetyLayouts = []struct{ layout, template string }{
{"default", "acme.blog::mail.welcome"},
{"system", "acme.blog::mail.auto"},
}
// captureLog points a mailer's log at a buffer and returns it.
func captureLog(m Mailer) *bytes.Buffer {
var logs bytes.Buffer
m.(*mailer).log = slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug}))
return &logs
}
// assertSiteName checks that msg shows name in its header, its footer and
// its text part.
func assertSiteName(t *testing.T, msg RenderedMessage, name string) {
t.Helper()
cell, ok := headerCell(msg.HTML)
if !ok {
t.Fatalf("no header row:\n%s", msg.HTML)
}
if m := logoSpan.FindStringSubmatch(cell); m == nil || m[1] != name {
t.Errorf("header = %q, want the site name %q", cell, name)
}
if got, want := footerText(t, msg.HTML), copyright()+" "+name; got != want {
t.Errorf("footer = %q, want %q", got, want)
}
if !strings.Contains(msg.Text, name) {
t.Errorf("text part lacks %q:\n%s", name, msg.Text)
}
}
// TestMailSafetySmoke covers the 35-08 gap closure: prose in a site name or
// a body is not a URL, encoded script URLs are refused, unsafe site names
// never stop a send, the inliner never stops mail and the footer prints the
// site name literally.
func TestMailSafetySmoke(t *testing.T) {
t.Parallel()
t.Run("prose_site_name", func(t *testing.T) {
const from = `"Metadata: Weekly" <x@example.com>`
brand := &Brand{SiteName: "Big Data: Summit"}
for _, l := range safetyLayouts {
// The display name of mail.from, no Branding: the file version.
m, drv := partialsMailer(t, Options{From: from}, nil)
assertSiteName(t, sendTemplate(t, m, drv, l.template, inlineVars), "Metadata: Weekly")
// The Branding name, still the file version.
m, drv = partialsMailer(t, Options{From: from}, &Overrides{Brand: brand})
assertSiteName(t, sendTemplate(t, m, drv, l.template, inlineVars), "Big Data: Summit")
// A template override as well: the override path.
m, drv = partialsMailer(t, Options{From: from}, &Overrides{Brand: brand, Template: &TemplateOverride{Subject: "S", Body: "Override **{{ .Name }}**."}})
msg := sendTemplate(t, m, drv, l.template, inlineVars)
if !strings.Contains(msg.HTML, "Override <strong>Ada</strong>") {
t.Fatalf("%s: the override was not sent:\n%s", l.layout, msg.HTML)
}
assertSiteName(t, msg, "Big Data: Summit")
// Prose with a scheme-like word in a body is not a URL.
m, drv = partialsMailer(t, Options{From: from}, &Overrides{Brand: brand, Template: &TemplateOverride{Subject: "S", Body: "See the metadata: field."}})
msg = sendTemplate(t, m, drv, l.template, inlineVars)
if !strings.Contains(msg.HTML, "See the metadata: field.") {
t.Fatalf("%s: the prose body was not sent:\n%s", l.layout, msg.HTML)
}
}
})
t.Run("encoded_scheme", func(t *testing.T) {
fileMailer, fileDrv := partialsMailer(t, Options{}, nil)
file := sendTemplate(t, fileMailer, fileDrv, "acme.blog::mail.welcome", inlineVars)
for _, optOut := range []bool{false, true} {
ov := &Overrides{Layouts: map[string]LayoutOverride{defaultLayoutName: {
HTML: `<div>{{ .Content }}<a href="javascript&#58;alert(1)">x</a></div>`,
Text: "{{ .Content }}",
DisableAutoInlineCSS: optOut,
}}}
m, drv := partialsMailer(t, Options{}, ov)
logs := captureLog(m)
got := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
if got.HTML != file.HTML || got.Text != file.Text {
t.Errorf("opt-out %v: the file version was not sent:\n%s", optOut, got.HTML)
}
for _, msg := range drv.Messages() {
if strings.Contains(msg.HTML, "javascript") || strings.Contains(msg.HTML, "&#58;") {
t.Errorf("opt-out %v: the encoded link reached the driver:\n%s", optOut, msg.HTML)
}
}
if !strings.Contains(logs.String(), "override failed") || !strings.Contains(logs.String(), "dangerous URL scheme") {
t.Errorf("opt-out %v: log = %q, want the override failure", optOut, logs)
}
}
})
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)"} {
if UnsafeText(s) {
t.Errorf("UnsafeText(%q) = true, want false", s)
}
}
for _, s := range []string{"javascript:alert(1)", "javascript: Acme", "JavaScript:x", "Go vbscript:msgbox(1)", "data:text/html,x", "Shop onclick=x"} {
if !UnsafeText(s) {
t.Errorf("UnsafeText(%q) = false, want true", s)
}
}
// An unsafe Branding name is left out; the mail.from name is shown,
// and one warning names the source, never the value.
m, drv := partialsMailer(t, Options{From: "Acme <x@example.com>"}, &Overrides{Brand: &Brand{SiteName: "javascript:alert(1)"}})
logs := captureLog(m)
for range 2 {
msg := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
assertSiteName(t, msg, "Acme")
if strings.Contains(msg.HTML, "javascript:") || strings.Contains(msg.Text, "javascript:") {
t.Errorf("the unsafe name reached the message:\n%s\n%s", msg.HTML, msg.Text)
}
}
out := logs.String()
if n := strings.Count(out, "site name left out of the layouts"); n != 1 {
t.Errorf("%d site name warnings in two sends, want 1:\n%s", n, out)
}
if !strings.Contains(out, "source=branding") || strings.Contains(out, "alert") {
t.Errorf("log = %q, want the source and not the value", out)
}
// Both names unsafe: no name at all, so no header row (no logo) and a
// footer with the year only.
m, drv = partialsMailer(t, Options{From: `"javascript: Acme" <x@example.com>`}, &Overrides{Brand: &Brand{SiteName: "Shop onclick=x"}})
logs = captureLog(m)
msg := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
if _, ok := headerCell(msg.HTML); ok {
t.Errorf("a header row without a name or a logo:\n%s", msg.HTML)
}
if got := footerText(t, msg.HTML); got != copyright() {
t.Errorf("footer = %q, want %q", got, copyright())
}
if out := logs.String(); !strings.Contains(out, "source=branding") || strings.Contains(out, "onclick") {
t.Errorf("log = %q, want the branding source and not the value", out)
}
// The editor: saving a template or a layout under a stored unsafe
// name is not refused; the Branding preview of an unsafe draft name
// renders without it.
ed, _ := newEditor(t, &Overrides{Brand: &Brand{SiteName: "Shop onclick=x"}})
tmpl := Draft{Kind: KindTemplate, Name: editorWelcome, Subject: "Hi {{ .Name }}", Body: "Hi **{{ .Name }}**.", Layout: defaultLayoutName}
if err := ed.Validate(ctx, tmpl); err != nil {
t.Errorf("a template draft under an unsafe stored name was refused: %+v", err)
}
layout := Draft{Kind: KindLayout, Name: defaultLayoutName, HTML: `<div>{{ .Content }}</div>{{ partial "footer" "body" .siteName }}`, Text: "{{ .Content }}"}
if err := ed.Validate(ctx, layout); err != nil {
t.Errorf("a layout draft under an unsafe stored name was refused: %+v", err)
}
preview, err := ed.Preview(ctx, Draft{Kind: KindBrand, Brand: &Brand{SiteName: "javascript:alert(1)"}})
if err != nil {
t.Fatalf("Branding preview of an unsafe name: %v", err)
}
if strings.Contains(preview.HTML, "javascript:") || strings.Contains(preview.Text, "javascript:") {
t.Errorf("the Branding preview shows the unsafe name:\n%s", preview.HTML)
}
})
}