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
This commit is contained in:
Jakub Zych
2026-11-14 15:47:35 +01:00
parent e0016e0c0d
commit 446a77efbf
3 changed files with 288 additions and 0 deletions

View File

@@ -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", `<p>See the metadata: field</p>`},
{"prose_big_data", `<p>Big Data: Summit</p>`},
{"img_alt", `<img alt="Data: Summit" src="https://cdn.example.com/a.png">`},
{"title_text", `<td title="javascript: tips">Tips</td>`},
{"scheme_in_query", `<a href="https://example.com/?q=javascript:x">x</a>`},
}
for _, tc := range accepts {
t.Run("accept/"+tc.name, func(t *testing.T) {
t.Parallel()
if err := validateHTML("<div>" + schemeFiller + tc.html + schemeFiller + "</div>"); 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", `<a href="javascript:alert(1)">x</a>`, `href="javascript:alert(1)"`, schemeMsg},
{"href_space_mixed_case", `<a href=" JavaScript:alert(1)">x</a>`, `JavaScript:alert(1)`, schemeMsg},
{"src_data", `<img src="data:image/png;base64,AAAA">`, `data:image/png`, schemeMsg},
{"href_vbscript", `<a href="vbscript:msgbox(1)">x</a>`, `vbscript:msgbox(1)`, schemeMsg},
{"form_action", `<form action="javascript:x"></form>`, `action="javascript:x"`, schemeMsg},
{"button_formaction", `<button formaction="javascript:x">Go</button>`, `formaction="javascript:x"`, schemeMsg},
{"table_background", `<table background="javascript:x"><tr><td>x</td></tr></table>`, `background="javascript:x"`, schemeMsg},
{"video_poster", `<video poster="javascript:x"></video>`, `poster="javascript:x"`, schemeMsg},
{"svg_xlink_href", `<svg><a xlink:href="javascript:x"><text>x</text></a></svg>`, `xlink:href="javascript:x"`, schemeMsg},
{"img_srcset", `<img src="a.png" srcset="a.png 1x, javascript:x 2x">`, `javascript:x 2x`, schemeMsg},
{"style_url", `<div style="background:url(javascript:x)">x</div>`, `url(javascript:x)`, schemeMsg},
{"style_url_quoted_entity", `<div style="background:url(&#34;javascript:x&#34;)">x</div>`, `url(&#34;javascript:x&#34;)`, schemeMsg},
{"style_element_data", `<style>td { background: url(data:image/png;base64,AA); }</style>`, `url(data:image/png;base64,AA)`, schemeMsg},
{"meta_refresh", `<meta http-equiv="refresh" content="0;url=javascript:x">`, `url=javascript:x`, schemeMsg},
{"entity_decimal_colon", `<a href="javascript&#58;alert(1)">x</a>`, `javascript&#58;alert(1)`, schemeMsg},
{"entity_hex_colon", `<a href="javascript&#x3A;alert(1)">x</a>`, `javascript&#x3A;alert(1)`, schemeMsg},
{"entity_named_colon", `<a href="javascript&colon;alert(1)">x</a>`, `javascript&colon;alert(1)`, schemeMsg},
{"entity_first_letter", `<a href="&#106;avascript:alert(1)">x</a>`, `&#106;avascript:alert(1)`, schemeMsg},
{"tab_split", `<a href="java&#9;script:alert(1)">x</a>`, `java&#9;script:alert(1)`, schemeMsg},
{"newline_split", `<a href="java&#x0A;script:alert(1)">x</a>`, `java&#x0A;script:alert(1)`, schemeMsg},
{"handler_without_space", `<img src="x.png"onerror="alert(1)">`, `onerror="alert(1)"`, "event handlers"},
}
for _, tc := range refuses {
t.Run("refuse/"+tc.name, func(t *testing.T) {
t.Parallel()
err := validateHTML("<div>" + schemeFiller + tc.html + schemeFiller + "</div>")
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, "<div>") {
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 = `<a href="javascript&#58;alert(1)">x</a>`
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: `<html><head><style>{{ .css }}</style></head><body><div>{{ .Content }}` + encodedLink + `</div></body></html>`,
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: `<div>{{ .Content }}` + encodedLink + `</div>`, 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&#58;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", `<a href="javascript:alert(1)">x</a>`},
{"encoded", `<a href="javascript&#58;alert(1)">x</a>`},
}
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, "</body>", l.link+"</body>", 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)
}
})
}
}

View File

@@ -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 <Studio>", "Acme &lt;Studio&gt;"},
{"[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{"<em", "<strong", "<a ", "<a>", "raw HTML omitted", "<!--", "<Studio"} {
if strings.Contains(footer, bad) {
t.Errorf("the footer holds %q:\n%s", bad, footer)
}
}
cell, ok := headerCell(msg.HTML)
if m := logoSpan.FindStringSubmatch(cell); !ok || m == nil || m[1] != n.escaped {
t.Errorf("header = %q, want %q", cell, n.escaped)
}
if !strings.Contains(msg.Text, "\n"+copyright()+" "+n.name+"\n") && !strings.HasSuffix(msg.Text, "\n"+copyright()+" "+n.name) {
t.Errorf("the text part's footer line is not %q:\n%s", copyright()+" "+n.name, msg.Text)
}
if siteNameWarnings(logs.String()) != 0 {
t.Errorf("a safe name was warned about:\n%s", logs)
}
})
}
}
// Called from a template: body is Markdown, text is printed as it is.
cases := []struct {
name, call, want, bad string
}{
{"body_markdown", `{{ partial "footer" "body" "*b*" }}`, "<em>b</em>", "*b*"},
{"text_literal", `{{ partial "footer" "text" "*b*" }}`, "<p>*b*</p>", "<em>"},
}
for _, tc := range cases {
t.Run("partial/"+tc.name, func(t *testing.T) {
t.Parallel()
m, drv := siteNameMailer(t, Options{}, &Overrides{Template: &TemplateOverride{Subject: "S", Body: tc.call}})
msg := sendTemplate(t, m, drv, "acme.site::mail.hello", inlineVars)
tables := withClass(msg.HTML, "table", "footer")
if len(tables) != 2 {
t.Fatalf("%d footer tables, want the body's and the layout's:\n%s", len(tables), msg.HTML)
}
body, _, _ := strings.Cut(msg.HTML[tables[0].start:], "</table>")
if !strings.Contains(body, tc.want) || strings.Contains(body, tc.bad) {
t.Errorf("footer partial %s = %q, want %q and no %q", tc.call, body, tc.want, tc.bad)
}
})
}
}

View File

@@ -104,6 +104,9 @@ GO_TESTS=(
./modules/postcard:TestSiteNameSend
./modules/postcard:TestUnsafeSiteNameDropped
./modules/postcard:TestUnsafeText
./modules/postcard:TestURLSchemeCheck
./modules/postcard:TestInlineUnsafeOutput
./modules/postcard:TestFooterSiteNameLiteral
)
# docker stage: the non-short framework tests (Postgres).