UnsafeText checked the escaped name on its own, so a name starting with on<letters>= (Online=Shop, onboarding=team) passed, was saved, and then failed every default/system-layout send once the footer printed it after "© <year> ". The name is now checked in the footer, header span and alt contexts, so such a name is dropped at render and refused on the Branding form instead of stopping mail.
190 lines
7.6 KiB
Go
190 lines
7.6 KiB
Go
package cabana_test
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
"git.golem15.com/golem15/summercms/modules/postcard"
|
|
)
|
|
|
|
const (
|
|
mailDefaultLayout = "postcard::mail.layouts.default"
|
|
mailSystemLayout = "postcard::mail.layouts.system"
|
|
)
|
|
|
|
// storedSiteName is the site_name of the Branding row.
|
|
func storedSiteName(t *testing.T, env *mailEnv) string {
|
|
t.Helper()
|
|
var stored cabana.MailBrandSetting
|
|
if err := env.gdb.First(&stored, 1).Error; err != nil {
|
|
t.Fatalf("brand row: %v", err)
|
|
}
|
|
return stored.SiteName
|
|
}
|
|
|
|
// putInLanguage is a PUT with Accept-Language lang instead of en.
|
|
func putInLanguage(t *testing.T, env *mailEnv, rel, token, lang string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req, err := env.request(http.MethodPut, rel, token, body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Accept-Language", lang)
|
|
rec := httptest.NewRecorder()
|
|
env.h.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
// moveWelcome saves the welcome template on the layout code and returns the
|
|
// next send of it.
|
|
func moveWelcome(t *testing.T, env *mailEnv, token, layout string) postcard.RenderedMessage {
|
|
t.Helper()
|
|
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
|
|
layoutID := env.mailRowID(t, postcard.LayoutsTable, layout)
|
|
env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID), token, templateBody("Witaj, {{ .name }}", "Cześć **{{ .name }}**.", layoutID))
|
|
return sendGrace(t, env, blogWelcome)
|
|
}
|
|
|
|
// TestMailSiteNameSafety covers the admin side of CR-01: a Branding save of
|
|
// a name mail could not show is refused on site_name in the admin's
|
|
// language and stores nothing, prose names with a colon save, and a name
|
|
// stored before the check existed never blocks a template save, a layout
|
|
// save or a send; the sender's display name of mail.from shows like any
|
|
// other name.
|
|
func TestMailSiteNameSafety(t *testing.T) {
|
|
env := newMailScreensEnv(t, nil)
|
|
_, root := env.admin(t, "mail-safety-root", true, "")
|
|
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": "Acme Mail"})
|
|
|
|
t.Run("refused_on_save", func(t *testing.T) {
|
|
body := map[string]any{"site_name": "javascript: Acme"}
|
|
env.expectFieldRefusal(t, http.StatusUnprocessableEntity, http.MethodPut, mailBrandPath, root, body, "site_name", "cannot be shown in mail", false)
|
|
if got := storedSiteName(t, env); got != "Acme Mail" {
|
|
t.Fatalf("the refused save stored site_name %q", got)
|
|
}
|
|
|
|
rec := putInLanguage(t, env, mailBrandPath, root, "pl", body)
|
|
if rec.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("pl PUT status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
_, message, details := errorEnvelope(t, rec)
|
|
if !detailHas(details, "site_name", "nie można pokazać") {
|
|
t.Fatalf("pl details[site_name]=%v (message %q), want the Polish text", details["site_name"], message)
|
|
}
|
|
if detailHas(details, "site_name", "backend::") {
|
|
t.Errorf("pl details[site_name]=%v holds a phrase key", details["site_name"])
|
|
}
|
|
if got := storedSiteName(t, env); got != "Acme Mail" {
|
|
t.Fatalf("the refused pl save stored site_name %q", got)
|
|
}
|
|
|
|
// A name unsafe only where the layouts print it, after the footer's
|
|
// "© <year> ", is refused as well (CR-01 residual).
|
|
for _, name := range []string{"Online=Shop", "onboarding=team"} {
|
|
env.expectFieldRefusal(t, http.StatusUnprocessableEntity, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": name}, "site_name", "cannot be shown in mail", false)
|
|
if got := storedSiteName(t, env); got != "Acme Mail" {
|
|
t.Fatalf("the refused save of %q stored site_name %q", name, got)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("prose_names_saved", func(t *testing.T) {
|
|
for _, name := range []string{"Big Data: Summit", "Metadata: Weekly"} {
|
|
saved := env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": name})
|
|
if got := dataOf(saved)["data"].(map[string]any)["site_name"]; got != name {
|
|
t.Errorf("response site_name = %#v, want %q", got, name)
|
|
}
|
|
if got := storedSiteName(t, env); got != name {
|
|
t.Errorf("stored site_name = %q, want %q", got, name)
|
|
}
|
|
}
|
|
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": "Acme Mail"})
|
|
})
|
|
|
|
t.Run("preview_drops_unsafe_name", func(t *testing.T) {
|
|
html := brandPreview(t, env, root, map[string]any{"site_name": "javascript: Acme"})
|
|
if strings.Contains(html, "javascript:") {
|
|
t.Errorf("the preview shows the unsafe name:\n%s", html)
|
|
}
|
|
if m := headerSpan.FindStringSubmatch(html); m != nil {
|
|
t.Errorf("the preview has a header span %q for an unsafe name", m[1])
|
|
}
|
|
})
|
|
|
|
// A name saved before the check existed: written straight into the row.
|
|
storeUnsafe := func(t *testing.T) {
|
|
t.Helper()
|
|
if err := env.gdb.Exec("UPDATE "+postcard.BrandTable+" SET site_name = ?", "javascript: Acme").Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := storedSiteName(t, env); got != "javascript: Acme" {
|
|
t.Fatalf("stored site_name = %q", got)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := env.gdb.Exec("UPDATE " + postcard.BrandTable + " SET site_name = ''").Error; err != nil {
|
|
t.Error(err)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("stored_unsafe_name_saves", func(t *testing.T) {
|
|
storeUnsafe(t)
|
|
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
|
|
defaultID := env.mailRowID(t, postcard.LayoutsTable, mailDefaultLayout)
|
|
env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID), root, templateBody("Witaj, {{ .name }}", "Cześć **{{ .name }}**.", defaultID))
|
|
|
|
def, ok := mailEditor(t, env).Catalog().Layout(mailDefaultLayout)
|
|
if !ok {
|
|
t.Fatal("no built-in default layout")
|
|
}
|
|
env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/%d", mailLayoutsPath, defaultID), root, map[string]any{
|
|
"name": "Default", "content_html": def.HTML, "content_text": def.Text, "content_css": "", "disable_auto_inline_css": false,
|
|
})
|
|
var saved cabana.MailLayout
|
|
if err := env.gdb.First(&saved, defaultID).Error; err != nil || !saved.IsCustom {
|
|
t.Fatalf("the layout override was not stored: %+v err=%v", saved, err)
|
|
}
|
|
})
|
|
|
|
t.Run("stored_unsafe_name_send", func(t *testing.T) {
|
|
storeUnsafe(t)
|
|
got := moveWelcome(t, env, root, mailDefaultLayout)
|
|
if strings.Contains(got.HTML, "javascript:") || strings.Contains(got.Text, "javascript:") {
|
|
t.Errorf("the stored unsafe name reached the message:\n%s\n%s", got.HTML, got.Text)
|
|
}
|
|
if !strings.Contains(got.HTML, "Cześć <strong>Grace</strong>") {
|
|
t.Errorf("the saved template was not sent:\n%s", got.HTML)
|
|
}
|
|
if m := headerSpan.FindStringSubmatch(got.HTML); m != nil {
|
|
t.Errorf("a header span %q without a safe name or a logo", m[1])
|
|
}
|
|
})
|
|
|
|
t.Run("mail_from_prose_name", func(t *testing.T) {
|
|
const from = `"Metadata: Weekly" <x@example.com>`
|
|
fromEnv := newMailScreensEnv(t, func(cfg *compass.Config) {
|
|
if err := cfg.Set("mail.from", from); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
_, tok := fromEnv.admin(t, "mail-safety-from", true, "")
|
|
for _, layout := range []string{mailDefaultLayout, mailSystemLayout} {
|
|
got := moveWelcome(t, fromEnv, tok, layout)
|
|
if got.From != from {
|
|
t.Fatalf("%s: From = %q, want %q", layout, got.From, from)
|
|
}
|
|
if m := headerSpan.FindStringSubmatch(got.HTML); m == nil || m[1] != "Metadata: Weekly" {
|
|
t.Errorf("%s: the header does not show the mail.from name:\n%s", layout, got.HTML)
|
|
}
|
|
if !strings.Contains(got.HTML, "Metadata: Weekly</p>") || !strings.Contains(got.Text, "Metadata: Weekly") {
|
|
t.Errorf("%s: the footer does not show the mail.from name:\n%s\n%s", layout, got.HTML, got.Text)
|
|
}
|
|
}
|
|
})
|
|
}
|