- validateHTML keeps the raw tag and handler regexes and reads the rest through unsafeMarkup: x/net/html tokenizer, decoded attribute values, prefix check on URL attributes, whole-value check on CSS, list attributes, meta content and comments; any on* attribute is refused - text is not a URL: "Metadata: Weekly" or "Big Data: Summit" as a site name or body text no longer stops a send (CR-01) - entity-encoded javascript: is refused whether or not the layout opts out of inlining (WR-05) - draft layout CSS is checked as a style element - fixtures that pinned prose as unsafe now carry markup-level URLs; the 35-07 subtest pinning an unsafe stored name is removed - TestMailSafetySmoke prose_site_name and encoded_scheme
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package postcard
|
|
|
|
import (
|
|
"strings"
|
|
|
|
xhtml "golang.org/x/net/html"
|
|
)
|
|
|
|
// urlAttributes are the attributes that hold one URL. Their decoded value is
|
|
// refused when it starts with a script or data scheme.
|
|
var urlAttributes = map[string]bool{
|
|
"action": true,
|
|
"background": true,
|
|
"cite": true,
|
|
"codebase": true,
|
|
"data": true,
|
|
"dynsrc": true,
|
|
"formaction": true,
|
|
"href": true,
|
|
"icon": true,
|
|
"longdesc": true,
|
|
"lowsrc": true,
|
|
"manifest": true,
|
|
"poster": true,
|
|
"profile": true,
|
|
"src": true,
|
|
"usemap": true,
|
|
"xlink:href": true,
|
|
// SVG animation targets can set an href.
|
|
"from": true,
|
|
"to": true,
|
|
"by": true,
|
|
}
|
|
|
|
// urlListAttributes hold several URLs (or values that can be URLs). Their
|
|
// decoded value is refused when dangerousScheme matches anywhere in it.
|
|
var urlListAttributes = map[string]bool{
|
|
"srcset": true,
|
|
"ping": true,
|
|
"archive": true,
|
|
"values": true,
|
|
}
|
|
|
|
// hasDangerousScheme reports whether a decoded URL attribute value starts
|
|
// with javascript:, vbscript: or data:, after every ASCII whitespace and
|
|
// control character is removed and the value is lower-cased, the way
|
|
// browsers read it ("java\tscript:" is javascript:).
|
|
func hasDangerousScheme(val string) bool {
|
|
var b strings.Builder
|
|
b.Grow(len(val))
|
|
for i := 0; i < len(val); i++ {
|
|
c := val[i]
|
|
if c <= 0x20 || c == 0x7f {
|
|
continue
|
|
}
|
|
if 'A' <= c && c <= 'Z' {
|
|
c += 'a' - 'A'
|
|
}
|
|
b.WriteByte(c)
|
|
}
|
|
v := b.String()
|
|
return strings.HasPrefix(v, "javascript:") || strings.HasPrefix(v, "vbscript:") || strings.HasPrefix(v, "data:")
|
|
}
|
|
|
|
// 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
|
|
// references in attribute values, so href="javascript:x" is read as a
|
|
// mail client reads it.
|
|
//
|
|
// URL schemes are looked for only where a URL can be: single-URL attributes
|
|
// (urlAttributes, by prefix), list-valued attributes (urlListAttributes),
|
|
// style attributes, the content of meta elements, the text of style
|
|
// elements and comments (anywhere in the value). Text content is not a URL,
|
|
// 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.
|
|
func unsafeMarkup(doc string) error {
|
|
z := xhtml.NewTokenizer(strings.NewReader(doc))
|
|
offset := 0
|
|
inStyle := false
|
|
for {
|
|
tt := z.Next()
|
|
if tt == xhtml.ErrorToken {
|
|
return nil
|
|
}
|
|
start := offset
|
|
offset += len(z.Raw())
|
|
loc := []int{start, offset}
|
|
t := z.Token()
|
|
switch tt {
|
|
case xhtml.StartTagToken, xhtml.SelfClosingTagToken:
|
|
for _, a := range t.Attr {
|
|
key := a.Key
|
|
if a.Namespace != "" {
|
|
key = a.Namespace + ":" + a.Key
|
|
}
|
|
switch {
|
|
case strings.HasPrefix(key, "on"):
|
|
return &unsafeError{msg: "rendered HTML contains event handlers", snippet: snippetAt(doc, loc)}
|
|
case urlAttributes[key] && hasDangerousScheme(a.Val),
|
|
urlListAttributes[key] && dangerousScheme.MatchString(a.Val),
|
|
key == "style" && dangerousScheme.MatchString(a.Val),
|
|
t.Data == "meta" && key == "content" && dangerousScheme.MatchString(a.Val):
|
|
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
|
|
}
|
|
}
|
|
if tt == xhtml.StartTagToken && t.Data == "style" {
|
|
inStyle = true
|
|
}
|
|
case xhtml.EndTagToken:
|
|
if t.Data == "style" {
|
|
inStyle = false
|
|
}
|
|
case xhtml.TextToken:
|
|
if inStyle && dangerousScheme.MatchString(t.Data) {
|
|
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
|
|
}
|
|
case xhtml.CommentToken:
|
|
if dangerousScheme.MatchString(t.Data) {
|
|
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
|
|
}
|
|
}
|
|
}
|
|
}
|