test(quick-261114-p5m): pin end tags inside quoted attributes in the mail URL check

- refuse cases for all eight probe payloads (noscript href and handler,
  svg style, title and textarea, noembed, xmp, conditional comment)
- send fallback and editor refusal for the three that survive rendering
- escaped_send subtests check svg title/textarea sends with an x/net/html
  parser oracle, scripting on and off
- accept cases for conditional comments, quoted CSS, a safe noscript
  link and a literal comment opener in an attribute
This commit is contained in:
Jakub Zych
2026-11-14 18:37:08 +01:00
parent e47d4ae6cf
commit f1f284a0f9

View File

@@ -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", `<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]-->`},
// The tag reading, where quoting decides where a tag ends, refuses
// nothing mail uses (CR-02 residual).
{"conditional_comment_quoted_attrs", `<!--[if mso]><table role="presentation" width="600"><tr><td style="padding:0"><![endif]-->`},
{"conditional_comment_not_mso_safe_link", `<!--[if !mso]><!--><a href="https://example.com/">x</a><!--<![endif]-->`},
{"style_css_quoted_strings", `<style>td[title="a > b"] { font-family: "Helvetica Neue", Arial; }</style>`},
{"title_escaped_end_in_attr", `<svg><title>&lt;a title="&lt;/title&gt;" href="javascript:x"&gt;</title></svg>`},
{"noscript_safe_link_end_in_attr", `<noscript><a title="</noscript>" href="https://example.com/">Open</a></noscript>`},
// Only the "<" of a comment opener is blanked, so a literal "<!--"
// in an attribute value does not become a scheme prefix.
{"href_literal_comment_opener", `<a href="<!--javascript:x">x</a>`},
}
for _, tc := range accepts {
t.Run("accept/"+tc.name, func(t *testing.T) {
@@ -87,6 +101,13 @@ func TestURLSchemeCheck(t *testing.T) {
// the tokenizer but not for a conforming parser, which keeps the
// rest of the tag together (CR-02 residual).
{"svg_style_end_in_attr", `<svg><style><a title="</style>" href="javascript:alert(1)">x</a></style></svg>`, `href="javascript:alert(1)"`, schemeMsg},
{"noscript_href_end_in_attr", `<noscript><a title="</noscript>" href="javascript:alert(1)">x</a></noscript>`, `href="javascript:alert(1)"`, schemeMsg},
{"noscript_handler_end_in_attr", `<noscript><img title="</noscript>" src=x onerror=alert(1)></noscript>`, `onerror=alert(1)`, "event handlers"},
{"svg_title_end_in_attr", `<svg><title><a title="</title>" href="javascript:alert(1)">x</a></title></svg>`, `href="javascript:alert(1)"`, schemeMsg},
{"svg_textarea_end_in_attr", `<svg><textarea><a title="</textarea>" href="javascript:alert(1)">x</a></textarea></svg>`, `href="javascript:alert(1)"`, schemeMsg},
{"noembed_end_in_attr", `<noembed><a title="</noembed>" href="javascript:alert(1)">x</a></noembed>`, `href="javascript:alert(1)"`, schemeMsg},
{"xmp_end_in_attr", `<xmp><a title="</xmp>" href="javascript:alert(1)">x</a></xmp>`, `href="javascript:alert(1)"`, schemeMsg},
{"conditional_comment_end_in_attr", `<!--[if mso]><a title="-->" href="javascript:alert(1)">x</a><![endif]-->`, `href="javascript:alert(1)"`, schemeMsg},
// 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},
@@ -128,8 +149,12 @@ func TestURLSchemeCheck(t *testing.T) {
{"noscript", `<noscript><a href="javascript:alert(1)">x</a></noscript>`},
{"xmp", `<xmp><a href="javascript:alert(1)">x</a></xmp>`},
// 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", `<svg><style><a title="</style>" href="javascript:alert(1)">x</a></style></svg>`},
{"noscript_href_end_in_attr", `<noscript><a title="</noscript>" href="javascript:alert(1)">x</a></noscript>`},
{"noscript_handler_end_in_attr", `<noscript><img title="</noscript>" src=x onerror=alert(1)></noscript>`},
}
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", `<svg><title><a title="</title>" href="javascript:alert(1)">x</a></title></svg>`},
{"svg_textarea_end_in_attr", `<svg><textarea><a title="</textarea>" href="javascript:alert(1)">x</a></textarea></svg>`},
}
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: `<html><head><style>{{ .css }}</style></head><body><div>{{ .Content }}` + l.link + `</div></body></html>`,
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, "&lt;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.