fix(35): CR-02 check raw-text element and comment content as markup

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.
This commit is contained in:
Jakub Zych
2026-11-14 17:30:07 +01:00
parent 2dbee30530
commit 1d481bb02d
4 changed files with 111 additions and 20 deletions

View File

@@ -25,6 +25,11 @@ func TestURLSchemeCheck(t *testing.T) {
{"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>`},
{"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) {
@@ -63,6 +68,17 @@ func TestURLSchemeCheck(t *testing.T) {
{"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"},
// 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"},
{"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) {
@@ -88,6 +104,14 @@ func TestURLSchemeCheck(t *testing.T) {
// 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)
@@ -97,23 +121,25 @@ func TestURLSchemeCheck(t *testing.T) {
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"
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)
})
}
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
@@ -136,6 +162,12 @@ func TestURLSchemeCheck(t *testing.T) {
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 {