Files
summercms/modules/postcard/safety_smoke_test.go
Jakub Zych 8deb56ff0b fix(35): WR-03 treat data: in a site name as a data URL only with ; or ,
scriptURLText took data: followed by any non-space character as a data
URL, so names such as Data:Lab, Big Data:Summit and data:Science were
refused on the Branding form and dropped from mail, contrary to the docs.
A data URL now needs its optional MIME type and the ; or , every data URL
has. javascript: and vbscript: keep matching with whitespace after the
colon, since such a URL still runs.
2026-11-14 17:40:13 +01:00

254 lines
11 KiB
Go

package postcard
import (
"bytes"
"context"
"html"
"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)
}
}
// footerTable returns the footer partial's table of html.
func footerTable(t *testing.T, html string) string {
t.Helper()
tables := withClass(html, "table", "footer")
if len(tables) != 1 {
t.Fatalf("%d footer tables, want 1:\n%s", len(tables), html)
}
table, _, _ := strings.Cut(html[tables[0].start:], "</table>")
return table
}
// 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)", "Shop onclick=x", "Online=Shop", "Data:Lab", "Big Data:Summit"} {
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"} {
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: "vbscript:msgbox(1)"}})
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, "msgbox") {
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: "vbscript:msgbox(1)"}})
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)
}
})
t.Run("inline_unsafe_output", func(t *testing.T) {
// An inliner whose output fails the safety check: the checked,
// un-inlined HTML is sent with its style block.
m, drv := partialsMailer(t, Options{}, nil)
m.(*mailer).inline = func(html string) (string, error) {
return strings.Replace(html, "</body>", `<a href="javascript:alert(1)">x</a></body>`, 1), nil
}
logs := captureLog(m)
plainMailer, plainDrv := partialsMailer(t, Options{}, nil)
plainMailer.(*mailer).inline = noInline
plain := sendTemplate(t, plainMailer, plainDrv, "acme.blog::mail.welcome", inlineVars)
for range 2 {
got := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
if strings.Contains(got.HTML, "javascript:") {
t.Errorf("the failing inlined output was sent:\n%s", got.HTML)
}
if !styleElement.MatchString(got.HTML) {
t.Errorf("no style block in the fallback:\n%s", got.HTML)
}
if style := buttonStyle(t, got.HTML); style != "" {
t.Errorf("the button has an inlined style %q", style)
}
if got.HTML != plain.HTML || got.Text != plain.Text {
t.Errorf("the fallback differs from the un-inlined message:\n got %s\nwant %s", got.HTML, plain.HTML)
}
}
out := logs.String()
if n := strings.Count(out, "inlined CSS failed the safety check"); n != 1 {
t.Errorf("%d inline warnings in two sends, want 1:\n%s", n, out)
}
if !strings.Contains(out, "layout="+defaultLayoutName) {
t.Errorf("log = %q, want the layout", out)
}
})
t.Run("literal_footer", func(t *testing.T) {
for _, name := range []string{"*Acme*", "Acme <Studio>", "[x](https://example.com)"} {
for _, l := range safetyLayouts {
msg := brandedSend(t, &Brand{SiteName: name}, "", l.template)
escaped := html.EscapeString(name)
if got, want := footerText(t, msg.HTML), copyright()+" "+escaped; got != want {
t.Errorf("%s %q: footer = %q, want %q", l.layout, name, got, want)
}
footer := footerTable(t, msg.HTML)
for _, bad := range []string{"<em", "<a ", "raw HTML omitted", "<!--"} {
if strings.Contains(footer, bad) {
t.Errorf("%s %q: the footer holds %q:\n%s", l.layout, name, bad, footer)
}
}
cell, _ := headerCell(msg.HTML)
if m := logoSpan.FindStringSubmatch(cell); m == nil || m[1] != escaped {
t.Errorf("%s %q: header = %q, want %q", l.layout, name, cell, escaped)
}
if want := "-------------------\n" + copyright() + " " + name; !strings.HasSuffix(strings.TrimRight(msg.Text, "\n"), want) {
t.Errorf("%s %q: text part does not end with %q:\n%s", l.layout, name, want, msg.Text)
}
}
}
// The footer partial called with body only still renders Markdown.
m, drv := partialsMailer(t, Options{}, &Overrides{Template: &TemplateOverride{Subject: "S", Body: `{{ partial "footer" "body" "**Bold** text" }}`}})
msg := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
tables := withClass(msg.HTML, "table", "footer")
if len(tables) != 2 {
t.Fatalf("%d footer tables, want the body's and the layout's:\n%s", len(tables), msg.HTML)
}
if body, _, _ := strings.Cut(msg.HTML[tables[0].start:], "</table>"); !strings.Contains(body, "<strong>Bold</strong> text") {
t.Errorf("a footer body is not rendered as Markdown:\n%s", msg.HTML)
}
})
}