The tokenizer-only walk read noscript, title, textarea, style, xmp and the other raw-text elements as one text token and never looked inside, so an unencoded javascript: link in <noscript> or <svg><title> passed the check and was sent. A mail client renders with scripting off and parses svg children as markup, so the raw content of those elements, and comment data (Outlook conditional comments), is now checked again as markup; nesting deeper than eight levels is refused.
253 lines
12 KiB
Go
253 lines
12 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><a href="javascript:x"></textarea>`},
|
|
{"title_escaped_link", `<svg><title><a href="javascript:x"></title></svg>`},
|
|
{"noscript_safe_link", `<noscript><a href="https://example.com/">Open</a></noscript>`},
|
|
{"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("javascript:x")">x</div>`, `url("javascript:x")`, 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:alert(1)">x</a>`, `javascript:alert(1)`, schemeMsg},
|
|
{"entity_hex_colon", `<a href="javascript:alert(1)">x</a>`, `javascript:alert(1)`, schemeMsg},
|
|
{"entity_named_colon", `<a href="javascript:alert(1)">x</a>`, `javascript:alert(1)`, schemeMsg},
|
|
{"entity_first_letter", `<a href="javascript:alert(1)">x</a>`, `javascript:alert(1)`, schemeMsg},
|
|
{"tab_split", `<a href="java	script:alert(1)">x</a>`, `java	script:alert(1)`, schemeMsg},
|
|
{"newline_split", `<a href="java
script:alert(1)">x</a>`, `java
script:alert(1)`, schemeMsg},
|
|
{"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:alert(1)">x</a></style></svg>`, `javascript: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:alert(1)">x</a>`, `javascript:alert(1)`, schemeMsg},
|
|
{"conditional_comment_handler", `<!--[if mso]><img src="x.png" onerror="alert(1)"><![endif]-->`, `onerror="alert(1)"`, "event handlers"},
|
|
{"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: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: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: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)
|
|
}
|
|
})
|
|
}
|
|
}
|