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:], "") 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" ` 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 Ada") { 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: `
{{ .Content }}x
`, 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, ":") { 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 ", "*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 "}, &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" `}, &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: `
{{ .Content }}
{{ 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, "", `x`, 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 ", "[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{""); !strings.Contains(body, "Bold text") { t.Errorf("a footer body is not rendered as Markdown:\n%s", msg.HTML) } }) }