From 446a77efbf271790f79ce8bc4789aac81e45da47 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Sat, 14 Nov 2026 15:47:35 +0100 Subject: [PATCH] test(postcard): decoded URL scheme check, unsafe inlined output and the literal footer - TestURLSchemeCheck: prose and scheme words in titles or queries pass; every URL-bearing attribute, CSS, meta refresh and each entity-encoded or whitespace-split script URL is refused with a snippet at the markup; a layout override with javascript: falls back to the file version with inlining on and off; the editor refuses it on content_html and CSS URLs on content_css - TestInlineUnsafeOutput: a failing inlined result sends the style-block version with one warning per layout - TestFooterSiteNameLiteral: the footer prints the site name escaped and literally on the default and system layouts; body stays Markdown - check-phase35.sh GO_TESTS requires the three tests --- modules/postcard/safety_test.go | 220 ++++++++++++++++++++++++++++++ modules/postcard/sitename_test.go | 65 +++++++++ scripts/check-phase35.sh | 3 + 3 files changed, 288 insertions(+) create mode 100644 modules/postcard/safety_test.go diff --git a/modules/postcard/safety_test.go b/modules/postcard/safety_test.go new file mode 100644 index 00000000..ed9e57be --- /dev/null +++ b/modules/postcard/safety_test.go @@ -0,0 +1,220 @@ +package postcard + +import ( + "context" + "errors" + "strings" + "testing" +) + +// schemeFiller surrounds a case's markup, so a snippet that points at the +// offending markup is told apart from one that starts the document. +var schemeFiller = strings.Repeat("plain words here ", 6) + +// TestURLSchemeCheck covers the anchored, decoded URL-scheme check of +// rendered HTML (WR-05): prose and URLs that merely contain a scheme word +// pass; every URL-bearing attribute, CSS, meta refresh and every +// character-reference or whitespace-split spelling of a script or data URL +// is refused, on a send with inlining on and off and in the editor. +func TestURLSchemeCheck(t *testing.T) { + t.Parallel() + + accepts := []struct{ name, html string }{ + {"prose_metadata", `

See the metadata: field

`}, + {"prose_big_data", `

Big Data: Summit

`}, + {"img_alt", `Data: Summit`}, + {"title_text", `Tips`}, + {"scheme_in_query", `x`}, + } + for _, tc := range accepts { + t.Run("accept/"+tc.name, func(t *testing.T) { + t.Parallel() + if err := validateHTML("
" + schemeFiller + tc.html + schemeFiller + "
"); err != nil { + t.Errorf("validateHTML(%s) = %v, want nil", tc.html, err) + } + }) + } + + const schemeMsg = "dangerous URL scheme" + refuses := []struct { + name, html string + // marker is a raw text of the offending markup the snippet holds. + marker string + msg string + }{ + {"href_javascript", `x`, `href="javascript:alert(1)"`, schemeMsg}, + {"href_space_mixed_case", `x`, `JavaScript:alert(1)`, schemeMsg}, + {"src_data", ``, `data:image/png`, schemeMsg}, + {"href_vbscript", `x`, `vbscript:msgbox(1)`, schemeMsg}, + {"form_action", `
`, `action="javascript:x"`, schemeMsg}, + {"button_formaction", ``, `formaction="javascript:x"`, schemeMsg}, + {"table_background", `
x
`, `background="javascript:x"`, schemeMsg}, + {"video_poster", ``, `poster="javascript:x"`, schemeMsg}, + {"svg_xlink_href", `x`, `xlink:href="javascript:x"`, schemeMsg}, + {"img_srcset", ``, `javascript:x 2x`, schemeMsg}, + {"style_url", `
x
`, `url(javascript:x)`, schemeMsg}, + {"style_url_quoted_entity", `
x
`, `url("javascript:x")`, schemeMsg}, + {"style_element_data", ``, `url(data:image/png;base64,AA)`, schemeMsg}, + {"meta_refresh", ``, `url=javascript:x`, schemeMsg}, + {"entity_decimal_colon", `x`, `javascript:alert(1)`, schemeMsg}, + {"entity_hex_colon", `x`, `javascript:alert(1)`, schemeMsg}, + {"entity_named_colon", `x`, `javascript:alert(1)`, schemeMsg}, + {"entity_first_letter", `x`, `javascript:alert(1)`, schemeMsg}, + {"tab_split", `x`, `java script:alert(1)`, schemeMsg}, + {"newline_split", `x`, `java script:alert(1)`, schemeMsg}, + {"handler_without_space", ``, `onerror="alert(1)"`, "event handlers"}, + } + for _, tc := range refuses { + t.Run("refuse/"+tc.name, func(t *testing.T) { + t.Parallel() + err := validateHTML("
" + schemeFiller + tc.html + schemeFiller + "
") + var ue *unsafeError + if !errors.As(err, &ue) { + t.Fatalf("validateHTML(%s) = %v, want an unsafe error", tc.html, err) + } + if !strings.Contains(ue.Error(), tc.msg) { + t.Errorf("error = %q, want %q", ue.Error(), tc.msg) + } + if !strings.Contains(ue.snippet, tc.marker) { + t.Errorf("snippet = %q, want it to point at %q", ue.snippet, tc.marker) + } + if strings.HasPrefix(ue.snippet, "
") { + t.Errorf("snippet = %q starts the document, not the offending markup", ue.snippet) + } + }) + } + + // An entity-encoded script link in a layout override's HTML makes the + // send fall back to the file version, whether or not the layout opts + // out of inlining; the encoded link never reaches the driver. + const encodedLink = `x` + file := fileWelcome(t) + if strings.Contains(file.HTML, "javascript") { + t.Fatalf("the file version holds a script link:\n%s", file.HTML) + } + plugin := acmeBlog{} + cat := NewCatalog() + if err := cat.Register(plugin.ID(), plugin.MailTemplatesFS(), plugin.MailTemplates(), plugin.MailLayouts()); err != nil { + t.Fatal(err) + } + for _, optOut := range []bool{false, true} { + name := "send/inline" + if optOut { + name = "send/opt_out" + } + t.Run(name, func(t *testing.T) { + t.Parallel() + assertFileFallback(t, cat, file, Options{Overrides: fakeOverrides{ov: Overrides{ + Template: &TemplateOverride{Subject: "Hello {{ .name }}", Body: "Hi **{{ .name }}**.", Layout: "encoded"}, + Layouts: map[string]LayoutOverride{"encoded": { + HTML: `
{{ .Content }}` + encodedLink + `
`, + Text: "{{ .Content }}", + CSS: "p { color: #123456; }", + DisableAutoInlineCSS: optOut, + }}, + }}}, true) + }) + } + + // The editor refuses the same layout draft on content_html, and a + // layout's CSS with a script URL on content_css; prose in a template + // body validates. + for _, optOut := range []bool{false, true} { + name := "editor/inline" + if optOut { + name = "editor/opt_out" + } + t.Run(name, func(t *testing.T) { + t.Parallel() + ctx := context.Background() + ed, _ := newEditor(t, nil) + layout := Draft{Kind: KindLayout, Name: defaultLayoutName, HTML: `
{{ .Content }}` + encodedLink + `
`, Text: "{{ .Content }}", DisableAutoInlineCSS: optOut} + de := ed.Validate(ctx, layout) + if de == nil || de.Field != fieldContentHTML || de.Rule != ruleUnsafe { + t.Fatalf("encoded link in a layout draft: %+v, want content_html/unsafe", de) + } + if !strings.Contains(de.Message, "javascript:alert(1)") { + t.Errorf("message = %q, want the encoded href", de.Message) + } + + def, ok := NewCatalog().Layout(defaultLayoutName) + if !ok { + t.Fatal("no built-in default layout") + } + css := Draft{Kind: KindLayout, Name: defaultLayoutName, HTML: def.HTML, Text: def.Text, CSS: "td { background: url(javascript:x); }", DisableAutoInlineCSS: optOut} + de = ed.Validate(ctx, css) + if de == nil || de.Field != fieldContentCSS || de.Rule != ruleUnsafe { + t.Fatalf("script URL in a layout's CSS: %+v, want content_css/unsafe", de) + } + + tmpl := Draft{Kind: KindTemplate, Name: editorWelcome, Subject: "Hi {{ .Name }}", Body: "See the metadata: field.", Layout: defaultLayoutName} + if de := ed.Validate(ctx, tmpl); de != nil { + t.Errorf("a prose body was refused: %+v", de) + } + }) + } +} + +// TestInlineUnsafeOutput covers an inlined result that fails the safety +// check (WR-05): the send succeeds with the checked, un-inlined HTML and its +// style block, and one warning per layout per minute names the layout. +func TestInlineUnsafeOutput(t *testing.T) { + t.Parallel() + brand := &Brand{ButtonPrimaryBg: inlineBrandColour} + links := []struct{ name, link string }{ + {"plain", `x`}, + {"encoded", `x`}, + } + for _, l := range links { + t.Run(l.name, func(t *testing.T) { + t.Parallel() + m, drv := partialsMailer(t, Options{CSS: inlineMedia}, &Overrides{Brand: brand}) + calls := 0 + m.(*mailer).inline = func(html string) (string, error) { + calls++ + return strings.Replace(html, "", l.link+"", 1), nil + } + logs := captureLog(m) + + ref, refDrv := partialsMailer(t, Options{CSS: inlineMedia}, &Overrides{Brand: brand}) + ref.(*mailer).inline = noInline + plain := sendTemplate(t, ref, refDrv, "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 css := styleBlock(t, got.HTML); !strings.Contains(css, inlineBrandColour) { + t.Errorf("the style block lost the Branding CSS:\n%s", css) + } + if style := buttonStyle(t, got.HTML); style != "" { + t.Errorf("the button has an inlined style %q", style) + } + if got.Text != plain.Text { + t.Errorf("text part = %q, want the un-inlined message's %q", got.Text, plain.Text) + } + if got.HTML != plain.HTML { + t.Errorf("the fallback differs from the un-inlined message:\n got %s\nwant %s", got.HTML, plain.HTML) + } + } + if calls != 2 { + t.Errorf("the inliner ran %d times, want 2", calls) + } + 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) || !strings.Contains(out, "dangerous URL scheme") { + t.Errorf("log = %q, want the layout and the error", out) + } + + // The system layout is another key: a second warning. + sendTemplate(t, m, drv, "acme.blog::mail.auto", nil) + out = logs.String() + if n := strings.Count(out, "inlined CSS failed the safety check"); n != 2 || !strings.Contains(out, "layout="+systemLayoutName) { + t.Errorf("log = %q, want a second warning for the system layout", out) + } + }) + } +} diff --git a/modules/postcard/sitename_test.go b/modules/postcard/sitename_test.go index b967df14..81510dfd 100644 --- a/modules/postcard/sitename_test.go +++ b/modules/postcard/sitename_test.go @@ -279,3 +279,68 @@ func TestUnsafeText(t *testing.T) { } } } + +// TestFooterSiteNameLiteral covers the literal footer (WR-01): the built-in +// layouts print the site name as text, escaped, in the footer as in the +// header, so Markdown or markup in a name is never rendered; the footer +// partial's body argument is still Markdown. +func TestFooterSiteNameLiteral(t *testing.T) { + t.Parallel() + names := []struct{ name, escaped string }{ + {"*Acme*", "*Acme*"}, + {"Acme ", "Acme <Studio>"}, + {"[x](https://example.com)", "[x](https://example.com)"}, + } + for _, l := range siteNameLayouts { + for _, n := range names { + t.Run(l.layout+"/"+n.name, func(t *testing.T) { + t.Parallel() + m, drv := siteNameMailer(t, Options{}, &Overrides{Brand: &Brand{SiteName: n.name}}) + logs := captureLog(m) + msg := sendTemplate(t, m, drv, l.template, inlineVars) + if got, want := footerText(t, msg.HTML), copyright()+" "+n.escaped; got != want { + t.Errorf("footer = %q, want %q", got, want) + } + footer := footerTable(t, msg.HTML) + for _, bad := range []string{"", "raw HTML omitted", "