fix(postcard,cabana): unsafe site names never stop a send and are refused at save
- postcard.UnsafeText: a text a layout prints is unsafe when, escaped,
it fails the rendered-HTML checks or holds a script or data URL at a
scheme boundary ("Big Data: Summit" is safe)
- siteNameFrom/layoutSiteName: an unsafe Branding name falls back to the
mail.from display name, an unsafe one of those gives no name; one
rate-limited warning per source (site_name:branding, site_name:mail.from),
never the value; file version, override, fallback and editor alike
- the Branding preview of an unsafe draft name renders without it
- MailBrandSetting.BeforeValidate: 422 on site_name with
backend::lang.mail.brand.site_name_unsafe (en/pl)
- settings Put localizes a BeforeValidate refusal's phrase keys, as a
controller save does
- TestMailSafetySmoke/unsafe_site_name, TestMailBrandSiteName/unsafe_name_refused
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1800,11 +1800,16 @@ func (MailBrandSetting) Rules() map[string]string {
|
||||
|
||||
var brandColourPattern = regexp.MustCompile(`^#[0-9a-f]{6}$`)
|
||||
|
||||
// BeforeValidate trims the site name, lower-cases colours, refuses anything
|
||||
// but #rrggbb or empty, and checks logo_file_id is null or a library image.
|
||||
// BeforeValidate trims the site name and refuses a name mail could not show
|
||||
// (postcard's UnsafeText), lower-cases colours, refuses anything but #rrggbb
|
||||
// or empty, and checks logo_file_id is null or a library image. One refusal
|
||||
// lists every field at fault.
|
||||
func (b *MailBrandSetting) BeforeValidate(tx *gorm.DB) error {
|
||||
b.SiteName = strings.TrimSpace(b.SiteName)
|
||||
details := map[string]any{}
|
||||
if postcard.UnsafeText(b.SiteName) {
|
||||
details["site_name"] = []string{"backend::lang.mail.brand.site_name_unsafe"}
|
||||
}
|
||||
for _, col := range []struct {
|
||||
name string
|
||||
value *string
|
||||
|
||||
@@ -89,6 +89,31 @@ func TestMailBrandSiteName(t *testing.T) {
|
||||
t.Errorf("a send without a site name, a logo or a mail.from name has a header row:\n%s", got.HTML)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unsafe_name_refused", func(t *testing.T) {
|
||||
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": "Acme Mail"})
|
||||
// A name mail could not show is refused on the field; nothing is stored.
|
||||
env.expectFieldRefusal(t, http.StatusUnprocessableEntity, http.MethodPut, mailBrandPath, root,
|
||||
map[string]any{"site_name": "javascript: Acme"}, "site_name", "cannot be shown in mail", 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)
|
||||
}
|
||||
// The preview of the unsaved draft renders without the name.
|
||||
if html := brandPreview(t, env, root, map[string]any{"site_name": "javascript: Acme"}); strings.Contains(html, "javascript:") {
|
||||
t.Errorf("the preview shows the unsafe name:\n%s", html)
|
||||
}
|
||||
// Prose with a colon is a name like any other.
|
||||
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": "Big Data: Summit"})
|
||||
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] != "Big Data: Summit" {
|
||||
t.Errorf("sent header does not show the saved site name:\n%s", got.HTML)
|
||||
}
|
||||
env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": ""})
|
||||
})
|
||||
}
|
||||
|
||||
// inlineLayoutFiles is the blog fixture whose blog layout carries a style
|
||||
|
||||
@@ -264,7 +264,9 @@ func (s SettingsService) Put(ctx context.Context, setting *CompiledSetting, body
|
||||
}
|
||||
if hook, ok := model.(lagoon.HasBeforeValidate); ok && hook != nil {
|
||||
if err := hook.BeforeValidate(tx); err != nil {
|
||||
return err
|
||||
// A refusal's phrase keys are localized, as on a
|
||||
// controller save.
|
||||
return localizeControllerError(ctx, s.Tr, err)
|
||||
}
|
||||
}
|
||||
rules := settingRules(setting, model)
|
||||
|
||||
Reference in New Issue
Block a user