Files
summercms/modules/cabana/mail_brand_layout_postgres_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

204 lines
8.8 KiB
Go

package cabana_test
import (
"fmt"
"net/http"
"regexp"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/postcard"
)
const mailBrandPath = "/settings/mail_brand"
// headerSpan is the site name in the built-in header partial's cell.
var headerSpan = regexp.MustCompile(`<td class="header"[^>]*>\s*<span[^>]*>([^<]*)</span>`)
// brandPreview renders the Branding preview of values and returns its HTML.
func brandPreview(t *testing.T, env *mailEnv, token string, values map[string]any) string {
t.Helper()
got := env.expect(t, http.StatusOK, http.MethodPost, "/mail/preview", token, map[string]any{"mode": "branding", "id": 0, "values": values})
return fmt.Sprint(dataOf(got)["html"])
}
// TestMailBrandSiteName covers the Branding site name: saved trimmed,
// limited to 255 characters, shown by the Branding preview and by sends in
// the built-in layouts; no header row without a name or a logo.
func TestMailBrandSiteName(t *testing.T) {
env := newMailScreensEnv(t, nil)
_, root := env.admin(t, "mail-site-root", true, "")
t.Run("saved_trimmed", func(t *testing.T) {
saved := env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": " Acme Mail "})
if name := dataOf(saved)["data"].(map[string]any)["site_name"]; name != "Acme Mail" {
t.Fatalf("response site_name = %#v", name)
}
var stored cabana.MailBrandSetting
if err := env.gdb.First(&stored, 1).Error; err != nil || stored.SiteName != "Acme Mail" {
t.Fatalf("brand row = %+v err=%v", stored, err)
}
got := env.expect(t, http.StatusOK, http.MethodGet, mailBrandPath, root, nil)
if name := dataOf(got)["data"].(map[string]any)["site_name"]; name != "Acme Mail" {
t.Errorf("read back site_name = %#v", name)
}
})
t.Run("max_length", func(t *testing.T) {
env.expectFieldRefusal(t, http.StatusUnprocessableEntity, http.MethodPut, mailBrandPath, root,
map[string]any{"site_name": strings.Repeat("x", 256)}, "site_name", "255", false)
var stored cabana.MailBrandSetting
if err := env.gdb.First(&stored, 1).Error; err != nil || stored.SiteName != "Acme Mail" {
t.Fatalf("refused save changed the row: %+v err=%v", stored, err)
}
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": strings.Repeat("y", 255)})
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": "Acme Mail"})
})
t.Run("preview", func(t *testing.T) {
html := brandPreview(t, env, root, map[string]any{"site_name": "Preview Name"})
if m := headerSpan.FindStringSubmatch(html); m == nil || m[1] != "Preview Name" {
t.Errorf("preview header does not show the site name:\n%s", html)
}
if !strings.Contains(html, "Preview Name</p>") {
t.Errorf("preview footer does not show the site name:\n%s", html)
}
// No logo and no name (mail.from has no display name): no header row.
html = brandPreview(t, env, root, map[string]any{"site_name": ""})
if strings.Contains(html, `class="header"`) {
t.Errorf("preview without a logo or a name has a header row:\n%s", html)
}
})
t.Run("send", func(t *testing.T) {
// The welcome template moved to the built-in default layout shows the
// saved name in its header and footer.
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
defaultID := env.mailRowID(t, postcard.LayoutsTable, "postcard::mail.layouts.default")
env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID), root, templateBody("Witaj, {{ .name }}", "Cześć **{{ .name }}**.", defaultID))
got := sendGrace(t, env, blogWelcome)
if m := headerSpan.FindStringSubmatch(got.HTML); m == nil || m[1] != "Acme Mail" {
t.Errorf("sent header does not show the saved site name:\n%s", got.HTML)
}
if !strings.Contains(got.Text, "Acme Mail") {
t.Errorf("sent text part lacks the footer's site name:\n%s", got.Text)
}
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": ""})
if got = sendGrace(t, env, blogWelcome); strings.Contains(got.HTML, `class="header"`) {
t.Errorf("a send without a site name, a logo or a mail.from name has a header row:\n%s", got.HTML)
}
})
}
// inlineLayoutFiles is the blog fixture whose blog layout carries a style
// block and a button, so whether its CSS was inlined shows on the button.
func inlineLayoutFiles() map[string]string {
return map[string]string{
"views/mail/layouts/blog.htm": `name = "Blog"
==
{{ .Content }}
==
<html><head><style>{{ .brandCss }}{{ .css }}</style></head><body><div class="blog-mail">{{ .Content }}{{ partial "button" "url" "https://example.com" "body" "Go" }}</div></body></html>
`,
}
}
var buttonTagRe = regexp.MustCompile(`<a [^>]*button-primary[^>]*>`)
// sentButtonInlined reports whether the last send's button carries the
// brand colour in a style attribute; it fails the test without a button.
func sentButtonInlined(t *testing.T, msg postcard.RenderedMessage, colour string) bool {
t.Helper()
tag := buttonTagRe.FindString(msg.HTML)
if tag == "" {
t.Fatalf("no button in %s", msg.HTML)
}
_, style, ok := strings.Cut(tag, ` style="`)
return ok && strings.Contains(style, colour)
}
// TestMailLayoutInlineOptOut covers the layout form's inlining switch: on a
// file layout it keeps the CSS in the style block of the next send, reset
// restores the file's value, and a custom layout created with the switch
// off inlines.
func TestMailLayoutInlineOptOut(t *testing.T) {
files := mailBlogFiles()
for name, data := range inlineLayoutFiles() {
files[name].Data = []byte(data)
}
env := newMailScreensEnvWith(t, mailEnvOptions{files: files})
_, root := env.admin(t, "mail-inline-root", true, "")
const colour = "#123456"
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"button_primary_bg": colour})
blogID := env.mailRowID(t, postcard.LayoutsTable, blogLayout)
blogPath := fmt.Sprintf("%s/%d", mailLayoutsPath, blogID)
blogFile, _ := mailEditor(t, env).Catalog().Layout(blogLayout)
stored := func() cabana.MailLayout {
t.Helper()
var l cabana.MailLayout
if err := env.gdb.First(&l, blogID).Error; err != nil {
t.Fatal(err)
}
return l
}
// The file layout inlines (its header has no disable_auto_inline_css).
if blogFile.DisableAutoInlineCSS || stored().DisableAutoInlineCSS {
t.Fatal("the fixture layout opts out")
}
if !sentButtonInlined(t, sendGrace(t, env, blogWelcomeEn), colour) {
t.Fatal("the file layout did not inline")
}
if form := dataOf(env.expect(t, http.StatusOK, http.MethodGet, blogPath, root, nil)); form["disable_auto_inline_css"] != false {
t.Errorf("form value = %#v, want false", form["disable_auto_inline_css"])
}
t.Run("switch_on", func(t *testing.T) {
env.expect(t, http.StatusOK, http.MethodPut, blogPath, root, map[string]any{
"name": "Blog", "content_html": blogFile.HTML, "content_text": blogFile.Text, "content_css": "", "disable_auto_inline_css": true,
})
if l := stored(); !l.DisableAutoInlineCSS || !l.IsCustom {
t.Fatalf("saved layout = %+v", l)
}
msg := sendGrace(t, env, blogWelcomeEn)
if sentButtonInlined(t, msg, colour) {
t.Errorf("the switch did not keep the CSS in the style block:\n%s", msg.HTML)
}
if !strings.Contains(msg.HTML, colour) {
t.Errorf("the style block lost the brand CSS:\n%s", msg.HTML)
}
if form := dataOf(env.expect(t, http.StatusOK, http.MethodGet, blogPath, root, nil)); form["disable_auto_inline_css"] != true {
t.Errorf("form value = %#v, want true", form["disable_auto_inline_css"])
}
})
t.Run("reset_restores_the_file_value", func(t *testing.T) {
env.expect(t, http.StatusOK, http.MethodPost, blogPath+"/actions/reset", root, map[string]any{})
if l := stored(); l.DisableAutoInlineCSS || l.IsCustom {
t.Fatalf("reset layout = %+v", l)
}
if !sentButtonInlined(t, sendGrace(t, env, blogWelcomeEn), colour) {
t.Error("the reset layout does not inline")
}
})
t.Run("custom_layout_switch_off", func(t *testing.T) {
created := env.expect(t, http.StatusCreated, http.MethodPost, mailLayoutsPath, root, map[string]any{
"name": "Promo", "code": "promo-inline", "content_html": blogFile.HTML, "content_text": blogFile.Text,
"content_css": ".promo { margin: 0; }", "disable_auto_inline_css": false,
})
customID := uint(dataOf(created)["id"].(float64))
var custom cabana.MailLayout
if err := env.gdb.First(&custom, customID).Error; err != nil || custom.DisableAutoInlineCSS {
t.Fatalf("custom layout = %+v err=%v", custom, err)
}
enID := env.mailRowID(t, postcard.TemplatesTable, blogWelcomeEn)
env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/%d", mailTemplatesPath, enID), root, templateBody("Welcome, {{ .name }}", "Hi **{{ .name }}**.", customID))
if msg := sendGrace(t, env, blogWelcomeEn); !sentButtonInlined(t, msg, colour) {
t.Errorf("a custom layout with the switch off did not inline:\n%s", msg.HTML)
}
})
}