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

@@ -92,6 +92,25 @@ func hasDangerousScheme(val string) bool {
return strings.HasPrefix(v, "javascript:") || strings.HasPrefix(v, "vbscript:") || strings.HasPrefix(v, "data:")
}
// rawTextElements are the elements whose content x/net/html's tokenizer
// reads as one text token (raw text or RCDATA). A mail client may parse that
// content as markup: it renders with scripting off, so noscript content is
// markup, and title, textarea, style, xmp and the like are foreign elements
// with markup children inside svg or math. unsafeMarkup checks their content
// as markup too.
var rawTextElements = map[string]bool{
"iframe": true,
"noembed": true,
"noframes": true,
"noscript": true,
"plaintext": true,
"script": true,
"style": true,
"textarea": true,
"title": true,
"xmp": true,
}
// unsafeMarkup checks the markup of rendered HTML: event-handler attributes
// and javascript:, vbscript: or data: URLs. It reads doc with x/net/html's
// tokenizer, which lower-cases attribute names and decodes character
@@ -105,19 +124,43 @@ func hasDangerousScheme(val string) bool {
// so prose such as "metadata:" or "Big Data: Summit" is not checked. Any
// attribute whose name starts with "on" is an event handler, with or
// without whitespace before it.
//
// The content of a raw-text element (rawTextElements) and of a comment is
// checked again as markup, because a mail client may read it so (noscript
// with scripting off, title or textarea in svg, Outlook's conditional
// comments). Each nested check reads a strictly shorter text, so the
// recursion ends; markup nested deeper than maxMarkupDepth is refused.
func unsafeMarkup(doc string) error {
return unsafeMarkupAt(doc, 0)
}
// maxMarkupDepth is how deep raw-text elements and comments may nest inside
// each other. Mail markup needs one or two levels (a style element, an
// Outlook conditional comment); a deeper document is refused rather than
// read again and again.
const maxMarkupDepth = 8
func unsafeMarkupAt(doc string, depth int) error {
if depth > maxMarkupDepth {
return &unsafeError{msg: "rendered HTML nests raw-text elements or comments too deeply", snippet: snippetAt(doc, []int{0, min(len(doc), 48)})}
}
z := xhtml.NewTokenizer(strings.NewReader(doc))
offset := 0
inStyle := false
// rawTag is the raw-text element whose content the next text token is.
rawTag := ""
for {
tt := z.Next()
if tt == xhtml.ErrorToken {
return nil
}
raw := string(z.Raw())
start := offset
offset += len(z.Raw())
offset += len(raw)
loc := []int{start, offset}
t := z.Token()
inRaw := rawTag != ""
rawTag = ""
switch tt {
case xhtml.StartTagToken, xhtml.SelfClosingTagToken:
for _, a := range t.Attr {
@@ -135,7 +178,12 @@ func unsafeMarkup(doc string) error {
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
}
}
if tt == xhtml.StartTagToken && t.Data == "style" {
// The tokenizer reads the content of a raw-text element as text
// even after a self-closing start tag (<style/>), as browsers do.
if rawTextElements[t.Data] {
rawTag = t.Data
}
if t.Data == "style" {
inStyle = true
}
case xhtml.EndTagToken:
@@ -146,10 +194,21 @@ func unsafeMarkup(doc string) error {
if inStyle && dangerousScheme.MatchString(t.Data) {
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
}
// The raw source, not the decoded text: RCDATA (title,
// textarea) is decoded by the tokenizer, and &lt;a&gt; in it is
// text to every reader.
if inRaw {
if err := unsafeMarkupAt(raw, depth+1); err != nil {
return err
}
}
case xhtml.CommentToken:
if dangerousScheme.MatchString(t.Data) {
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
}
if err := unsafeMarkupAt(t.Data, depth+1); err != nil {
return err
}
}
}
}