Files
summercms/modules/postcard/safety_smoke_test.go
Jakub Zych 13ab905f8d fix(postcard): check URL schemes in decoded attribute values, not in text
- validateHTML keeps the raw tag and handler regexes and reads the rest
  through unsafeMarkup: x/net/html tokenizer, decoded attribute values,
  prefix check on URL attributes, whole-value check on CSS, list
  attributes, meta content and comments; any on* attribute is refused
- text is not a URL: "Metadata: Weekly" or "Big Data: Summit" as a site
  name or body text no longer stops a send (CR-01)
- entity-encoded javascript: is refused whether or not the layout
  opts out of inlining (WR-05)
- draft layout CSS is checked as a style element
- fixtures that pinned prose as unsafe now carry markup-level URLs;
  the 35-07 subtest pinning an unsafe stored name is removed
- TestMailSafetySmoke prose_site_name and encoded_scheme
2026-11-14 15:13:35 +01:00

105 lines
4.1 KiB
Go

package postcard
import (
"bytes"
"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)
}
}
})
}