diff --git a/modules/postcard/safety_test.go b/modules/postcard/safety_test.go index 0e284192..561cec24 100644 --- a/modules/postcard/safety_test.go +++ b/modules/postcard/safety_test.go @@ -5,6 +5,8 @@ import ( "errors" "strings" "testing" + + xhtml "golang.org/x/net/html" ) // schemeFiller surrounds a case's markup, so a snippet that points at the @@ -15,7 +17,9 @@ var schemeFiller = strings.Repeat("plain words here ", 6) // 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. +// is refused, on a send with inlining on and off and in the editor. A +// raw-text end tag or a comment's end inside a quoted attribute does not hide +// the rest of the tag (CR-02 residual). func TestURLSchemeCheck(t *testing.T) { t.Parallel() @@ -34,6 +38,16 @@ func TestURLSchemeCheck(t *testing.T) { {"footer_handler_shaped_name", `

© 2026 Online=Shop

`}, {"title_escaped_handler", `<img onerror=x>`}, {"outlook_conditional_vml", ``}, + // The tag reading, where quoting decides where a tag ends, refuses + // nothing mail uses (CR-02 residual). + {"conditional_comment_quoted_attrs", ``}, + {"conditional_comment_not_mso_safe_link", `x`}, + {"style_css_quoted_strings", ``}, + {"title_escaped_end_in_attr", `<a title="</title>" href="javascript:x">`}, + {"noscript_safe_link_end_in_attr", ``}, + // Only the "<" of a comment opener is blanked, so a literal "" href="javascript:alert(1)">x`, `href="javascript:alert(1)"`, schemeMsg}, // Whitespace-split schemes in list, meta, CSS and comment values // (WR-01). {"values_tab_split", `x`, `values="java script:alert(1)"`, schemeMsg}, @@ -128,8 +149,12 @@ func TestURLSchemeCheck(t *testing.T) { {"noscript", ``}, {"xmp", `<a href="javascript:alert(1)">x</a>`}, // An end tag inside a quoted attribute does not hide the rest of the - // tag (CR-02 residual). + // tag (CR-02 residual). html/template escapes literal title and + // textarea text, so the svg title and textarea variants are sent as + // escaped text and checked by escaped_send below, not as fallbacks. {"svg_style_end_in_attr", ``}, + {"noscript_href_end_in_attr", ``}, + {"noscript_handler_end_in_attr", ``}, } file := fileWelcome(t) if strings.Contains(file.HTML, "javascript") { @@ -161,6 +186,52 @@ func TestURLSchemeCheck(t *testing.T) { } } + // The svg title and textarea variants are escaped by html/template: the + // override is sent, and a conforming parser, with scripting on and off, + // builds no element with a handler or a script or data URL from it. + escaped := []struct{ name, link string }{ + {"svg_title_end_in_attr", `x`}, + {"svg_textarea_end_in_attr", ``}, + } + for _, l := range escaped { + for _, optOut := range []bool{false, true} { + name := "escaped_send/" + l.name + "/inline" + if optOut { + name = "escaped_send/" + l.name + "/opt_out" + } + t.Run(name, func(t *testing.T) { + t.Parallel() + drv := NewMemoryDriver() + m := NewMailer(cat, drv, Options{Overrides: fakeOverrides{ov: Overrides{ + Template: &TemplateOverride{Subject: "Hello {{ .name }}", Body: "Hi **{{ .name }}**.", Layout: "encoded"}, + Layouts: map[string]LayoutOverride{"encoded": { + HTML: `
{{ .Content }}` + l.link + `
`, + Text: "{{ .Content }}", + CSS: "p { color: #123456; }", + DisableAutoInlineCSS: optOut, + }}, + }}}) + sendWelcome(t, m, "") + got := lastMessage(t, drv) + if got.HTML == file.HTML { + t.Fatalf("the override was refused; the file version was sent") + } + if !strings.Contains(got.HTML, "<a title=") { + t.Errorf("the link was not sent as escaped text:\n%s", got.HTML) + } + for _, scripting := range []bool{true, false} { + doc, err := xhtml.ParseWithOptions(strings.NewReader(got.HTML), xhtml.ParseOptionEnableScripting(scripting)) + if err != nil { + t.Fatalf("parse (scripting %v): %v", scripting, err) + } + if live := liveUnsafeElements(doc); len(live) > 0 { + t.Errorf("scripting %v: live unsafe attributes %v in:\n%s", scripting, live, got.HTML) + } + } + }) + } + } + // 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. @@ -206,6 +277,29 @@ func TestURLSchemeCheck(t *testing.T) { } } +// liveUnsafeElements lists, as "element key=value", every attribute of an +// element in the parsed tree n that is an event handler or a single-URL +// attribute with a script or data URL, with the namespace prefix +// unsafeMarkup builds. +func liveUnsafeElements(n *xhtml.Node) []string { + var live []string + if n.Type == xhtml.ElementNode { + for _, a := range n.Attr { + key := a.Key + if a.Namespace != "" { + key = a.Namespace + ":" + a.Key + } + if strings.HasPrefix(key, "on") || urlAttributes[key] && hasDangerousScheme(a.Val) { + live = append(live, n.Data+" "+key+"="+a.Val) + } + } + } + for c := n.FirstChild; c != nil; c = c.NextSibling { + live = append(live, liveUnsafeElements(c)...) + } + return live +} + // 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.