Files
summercms/modules/postcard/safety_test.go
Jakub Zych 31de2ff90f fix(35): WR-02 drop the whole-document event-handler regex
validateHTML ran (?i)\son[a-z]+\s*= over the rendered document, text
included, so a variable such as "buy one = get one free" stopped its own
message, and it is what made Online=Shop in the footer a send failure.
The tokenizer walk refuses every on* attribute, and since CR-02 it also
reads raw-text element and comment content as markup, so the regex is
dropped. Text shaped like a handler is now sent; such site names are shown
and saved, and the Branding refusal copy no longer mentions onclick=.
2026-11-14 17:37:52 +01:00

265 lines
13 KiB
Go

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>`},
// Escaped markup in a raw-text element is text to every reader.
{"textarea_escaped_link", `<textarea>&lt;a href="javascript:x"&gt;</textarea>`},
{"title_escaped_link", `<svg><title>&lt;a href="javascript:x"&gt;</title></svg>`},
{"noscript_safe_link", `<noscript><a href="https://example.com/">Open</a></noscript>`},
// Text shaped like an event handler is text (WR-02).
{"prose_handler_shaped", `<p>Ada, buy one = get one free, online = yes</p>`},
{"footer_handler_shaped_name", `<p>© 2026 Online=Shop</p>`},
{"title_escaped_handler", `<svg><title>&lt;img onerror=x&gt;</title></svg>`},
{"outlook_conditional_vml", `<!--[if mso]><v:roundrect href="https://example.com/" style="height:40px"><w:anchorlock/></v:roundrect><![endif]-->`},
}
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_with_space", `<p class="x" onclick="x()">x</p>`, `onclick="x()"`, "event handlers"},
{"handler_without_space", `<img src="x.png"onerror="alert(1)">`, `onerror="alert(1)"`, "event handlers"},
// Raw-text element content that a mail client parses as markup
// (scripting off, foreign content in svg) (CR-02).
{"noscript_href", `<noscript><a href="javascript:alert(1)">x</a></noscript>`, `href="javascript:alert(1)"`, schemeMsg},
{"noscript_handler", `<noscript><img src="x"/onerror=alert(1)></noscript>`, `onerror=alert(1)`, "event handlers"},
{"svg_title_href", `<svg><title><a href="javascript:alert(1)">x</a></title></svg>`, `href="javascript:alert(1)"`, schemeMsg},
{"svg_textarea_href", `<svg><textarea><a href="javascript:alert(1)">x</a></textarea></svg>`, `href="javascript:alert(1)"`, schemeMsg},
{"svg_style_entity", `<svg><style><a href="javascript&#58;alert(1)">x</a></style></svg>`, `javascript&#58;alert(1)`, schemeMsg},
{"xmp_href", `<xmp><a href="javascript:alert(1)">x</a></xmp>`, `href="javascript:alert(1)"`, schemeMsg},
{"self_closing_style", `<style/><a href="javascript&#58;alert(1)">x</a>`, `javascript&#58;alert(1)`, schemeMsg},
{"conditional_comment_handler", `<!--[if mso]><img src="x.png" onerror="alert(1)"><![endif]-->`, `onerror="alert(1)"`, "event handlers"},
// Whitespace-split schemes in list, meta, CSS and comment values
// (WR-01).
{"values_tab_split", `<svg><a><animate attributeName="href" values="java&#9;script:alert(1)"/><text>x</text></a></svg>`, `values="java&#9;script:alert(1)"`, schemeMsg},
{"meta_refresh_tab_split", `<meta http-equiv="refresh" content="0;url=java&#9;script:alert(1)">`, `url=java&#9;script:alert(1)`, schemeMsg},
{"srcset_newline_split", `<img src="a.png" srcset="a.png 1x, java&#10;script:x 2x">`, `java&#10;script:x 2x`, schemeMsg},
{"style_tab_split", `<div style="background:url(java&#9;script:x)">x</div>`, `url(java&#9;script:x)`, schemeMsg},
{"style_element_space_split", "<style>td { background: url(java\tscript:x); }</style>", "url(java script:x)", schemeMsg},
{"nested_too_deep", strings.Repeat("<noscript>", maxMarkupDepth+2) + "x", "<noscript>", "too deeply"},
}
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>`
// The same holds for an unencoded link hidden in a raw-text element that
// html/template leaves as written (CR-02; it escapes title and textarea
// text itself).
links := []struct{ name, link string }{
{"encoded", encodedLink},
{"noscript", `<noscript><a href="javascript:alert(1)">x</a></noscript>`},
{"xmp", `<xmp><a href="javascript:alert(1)">x</a></xmp>`},
}
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 _, l := range links {
for _, optOut := range []bool{false, true} {
name := "send/" + l.name + "/inline"
if optOut {
name = "send/" + l.name + "/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 }}` + l.link + `</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)
}
for _, l := range links[1:] {
hidden := Draft{Kind: KindLayout, Name: defaultLayoutName, HTML: `<div>{{ .Content }}` + l.link + `</div>`, Text: "{{ .Content }}", DisableAutoInlineCSS: optOut}
if de := ed.Validate(ctx, hidden); de == nil || de.Field != fieldContentHTML || de.Rule != ruleUnsafe {
t.Errorf("%s link in a layout draft: %+v, want content_html/unsafe", l.name, de)
}
}
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)
}
})
}
}