test(cabana): unsafe Branding site names refused at save, stored ones never block saves or sends
- TestMailSiteNameSafety: a javascript: site name is a 422 on site_name in en and pl and stores nothing; Big Data: Summit and Metadata: Weekly save; the Branding preview drops an unsafe draft name; a name stored before the check never refuses a template or layout save or stops a send; a mail.from display name with a colon shows on the default and system layouts - the mail screens env reads mail.from from its configuration (default unchanged), so a mutate hook can set the sender - check-phase35.sh DOCKER_TESTS requires TestMailSiteNameSafety
This commit is contained in:
@@ -198,7 +198,7 @@ func newMailScreensEnvWith(t *testing.T, opts mailEnvOptions) *mailEnv {
|
||||
for key, value := range map[string]any{
|
||||
"http.body_limits.default_bytes": 1048576, "http.body_limits.upload_bytes": 8388608,
|
||||
"admin.password.bcrypt_cost": 4, "admin.login.max_attempts": 100000,
|
||||
"app.url": "https://mail.example.test",
|
||||
"app.url": "https://mail.example.test", "mail.from": "noreply@example.test",
|
||||
} {
|
||||
if err := cfg.Set(key, value); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -217,7 +217,8 @@ func newMailScreensEnvWith(t *testing.T, opts mailEnvOptions) *mailEnv {
|
||||
t.Fatal(err)
|
||||
}
|
||||
driver := &mailSpy{}
|
||||
mailer := postcard.NewMailer(nil, driver, postcard.Options{From: "noreply@example.test", Overrides: postcard.NewDBSource(app)})
|
||||
// mail.from is read from the configuration, so mutate can set the sender.
|
||||
mailer := postcard.NewMailer(nil, driver, postcard.Options{From: cfg.String("mail.from"), Overrides: postcard.NewDBSource(app)})
|
||||
if err := app.Publish[postcard.Mailer](mailer); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
180
modules/cabana/mail_site_name_postgres_test.go
Normal file
180
modules/cabana/mail_site_name_postgres_test.go
Normal file
@@ -0,0 +1,180 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -129,6 +129,7 @@ DOCKER_TESTS=(
|
||||
./modules/cabana:TestMailLocaleGroups
|
||||
./modules/cabana:TestMailBrandSiteName
|
||||
./modules/cabana:TestMailLayoutInlineOptOut
|
||||
./modules/cabana:TestMailSiteNameSafety
|
||||
)
|
||||
|
||||
# admin stage: vitest files that must pass.
|
||||
|
||||
Reference in New Issue
Block a user