- unsafeMarkupAt runs the existing HTML reading, then a tag reading in which attribute quoting alone decides where a tag ends (NextIsNotRawText after raw-text start tags, comment openers blanked) - both readings share unsafeAttrs; existing refusals keep their message and snippet - svg/style end tag inside a quoted attribute is refused at check, save and send; README and docs/services/mail.md describe the second reading
306 lines
12 KiB
Go
306 lines
12 KiB
Go
package postcard
|
|
|
|
import (
|
|
"html"
|
|
"regexp"
|
|
"strings"
|
|
|
|
xhtml "golang.org/x/net/html"
|
|
)
|
|
|
|
// scriptURLText finds a script URL in text: javascript: or vbscript: at a
|
|
// scheme boundary (whitespace may follow the colon of a script URL, so
|
|
// "javascript: Acme" matches), or a data URL: data: at a scheme boundary,
|
|
// then an optional MIME type and the ";" or "," every data URL has
|
|
// ("data:text/html,x", "data:,x"). A boundary is the start of the text or a
|
|
// character that cannot belong to a URL scheme, so "Metadata: Weekly" does
|
|
// not match; nor do "Big Data: Summit" and "Data:Lab", which are no data
|
|
// URLs.
|
|
var scriptURLText = regexp.MustCompile(`(?i)(?:^|[^a-z0-9+.\-])(?:(?:javascript|vbscript):|data:\s*(?:[a-z]+/[a-z0-9.+\-]+\s*)?[;,])`)
|
|
|
|
// UnsafeText reports whether a text that a layout prints, such as the site
|
|
// name of the built-in layouts, must be kept out of mail. A text is unsafe
|
|
// when, HTML-escaped as html/template prints text, it fails the rendered-HTML
|
|
// safety checks in the places the built-in layouts print it, or when it holds
|
|
// a script URL: javascript: or vbscript: at a scheme boundary, or a data:
|
|
// URL. Escaped text is never markup, so "Big Data: Summit", "Data:Lab",
|
|
// "Online=Shop" and "Shop onclick=x" are safe; "javascript: Acme" and
|
|
// "data:text/html,x" are not.
|
|
func UnsafeText(s string) bool {
|
|
return validateHTML(printContext(html.EscapeString(s))) != nil || scriptURLText.MatchString(s)
|
|
}
|
|
|
|
// printContext is the markup the built-in layouts print an escaped site name
|
|
// in: after "© <year> " in the footer paragraph, in the header's span and in
|
|
// the logo's alt attribute. A text is checked there, not on its own, so a
|
|
// check that depends on what precedes the text (a whitespace-prefixed
|
|
// handler) sees what a send renders.
|
|
func printContext(esc string) string {
|
|
return "<p>© 2000 " + esc + "</p><span>" + esc + "</span><img alt=\"" + esc + "\">"
|
|
}
|
|
|
|
// 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 containsDangerousScheme finds a scheme
|
|
// anywhere in it.
|
|
var urlListAttributes = map[string]bool{
|
|
"srcset": true,
|
|
"ping": true,
|
|
"archive": true,
|
|
"values": true,
|
|
}
|
|
|
|
// stripURLSpace removes every ASCII whitespace and control character
|
|
// (U+0000 to U+0020 and U+007F) from val and lower-cases it, the way
|
|
// browsers read a URL ("java\tscript:" is javascript:).
|
|
func stripURLSpace(val string) string {
|
|
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)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// hasDangerousScheme reports whether a decoded URL attribute value starts
|
|
// with javascript:, vbscript: or data:, once stripURLSpace has removed
|
|
// whitespace and control characters.
|
|
func hasDangerousScheme(val string) bool {
|
|
v := stripURLSpace(val)
|
|
return strings.HasPrefix(v, "javascript:") || strings.HasPrefix(v, "vbscript:") || strings.HasPrefix(v, "data:")
|
|
}
|
|
|
|
// containsDangerousScheme reports whether dangerousScheme matches anywhere
|
|
// in a decoded value (a URL list, CSS, meta content or a comment) once
|
|
// stripURLSpace has removed whitespace and control characters, so a
|
|
// tab-split "java	script:" is found as in a single-URL attribute.
|
|
func containsDangerousScheme(val string) bool {
|
|
return dangerousScheme.MatchString(stripURLSpace(val))
|
|
}
|
|
|
|
// 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, and its tag reading reads their content as markup in place,
|
|
// calling NextIsNotRawText after their start tags.
|
|
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
|
|
// 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.
|
|
//
|
|
// 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.
|
|
//
|
|
// After this HTML reading, the same text is read a second time as tags only
|
|
// (unsafeTagReading). The tokenizer ends a raw-text element at the first
|
|
// matching end tag and a comment at the first "-->", even when that end sits
|
|
// inside a quoted attribute; a parser that reads the content as markup keeps
|
|
// the attribute together, so in <a title="</style>" href="javascript:x"> the
|
|
// href is live although the HTML reading saw it as text. The tag reading lets
|
|
// attribute quoting alone decide where a tag ends: it does not stop at
|
|
// raw-text or comment boundaries and applies the same attribute checks to
|
|
// every start tag. It runs after the HTML reading, so every refusal of the
|
|
// HTML reading keeps its message and snippet, and it runs at every depth, so
|
|
// content re-read as markup is read with quoting deciding too. Each reading
|
|
// is one linear pass; the depth limit bounds the recursion.
|
|
//
|
|
// Rejected alternatives: re-checking from a raw-text element's content start
|
|
// to the end of the document re-reads the rest of the document once per
|
|
// raw-text element and comment inside the recursion, which is quadratic or
|
|
// worse on hostile input; using x/net/html's parse tree as the check loses
|
|
// the raw snippet the editor shows, reads xmp and noembed as raw text and
|
|
// ends a conditional comment at the first "-->", while their content is
|
|
// checked here as markup.
|
|
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)})}
|
|
}
|
|
if err := unsafeHTMLReading(doc, depth); err != nil {
|
|
return err
|
|
}
|
|
return unsafeTagReading(doc)
|
|
}
|
|
|
|
// unsafeAttrs checks the attributes of start tag t, found at loc in doc:
|
|
// event handlers and dangerous URL schemes. The snippet points at the tag.
|
|
func unsafeAttrs(t xhtml.Token, doc string, loc []int) error {
|
|
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] && containsDangerousScheme(a.Val),
|
|
key == "style" && containsDangerousScheme(a.Val),
|
|
t.Data == "meta" && key == "content" && containsDangerousScheme(a.Val):
|
|
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// unsafeHTMLReading reads doc as x/net/html's tokenizer does: it checks
|
|
// start-tag attributes, the text of style elements and comments, and checks
|
|
// the content of raw-text elements and comments again as markup one level
|
|
// deeper.
|
|
func unsafeHTMLReading(doc string, depth int) error {
|
|
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(raw)
|
|
loc := []int{start, offset}
|
|
t := z.Token()
|
|
inRaw := rawTag != ""
|
|
rawTag = ""
|
|
switch tt {
|
|
case xhtml.StartTagToken, xhtml.SelfClosingTagToken:
|
|
if err := unsafeAttrs(t, doc, loc); err != nil {
|
|
return err
|
|
}
|
|
// 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:
|
|
if t.Data == "style" {
|
|
inStyle = false
|
|
}
|
|
case xhtml.TextToken:
|
|
if inStyle && containsDangerousScheme(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 <a> in it is
|
|
// text to every reader.
|
|
if inRaw {
|
|
if err := unsafeMarkupAt(raw, depth+1); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case xhtml.CommentToken:
|
|
if containsDangerousScheme(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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// unsafeTagReading reads doc as tags only, with attribute quoting alone
|
|
// deciding where a tag ends, and checks every start tag's attributes. It
|
|
// calls NextIsNotRawText after each raw-text start tag, so the element's
|
|
// content is read as tags, and it tokenizes a copy of doc in which the "<"
|
|
// of every "<!--" is a space, so no comment is recognised. Only the "<" is
|
|
// blanked: the copy has the length of doc, so offsets and snippets point
|
|
// into doc, and an attribute value holding a literal "<!--" does not turn
|
|
// into a scheme prefix once stripURLSpace removes the space.
|
|
func unsafeTagReading(doc string) error {
|
|
z := xhtml.NewTokenizer(strings.NewReader(strings.ReplaceAll(doc, "<!--", " !--")))
|
|
offset := 0
|
|
for {
|
|
tt := z.Next()
|
|
if tt == xhtml.ErrorToken {
|
|
return nil
|
|
}
|
|
start := offset
|
|
offset += len(z.Raw())
|
|
if tt != xhtml.StartTagToken && tt != xhtml.SelfClosingTagToken {
|
|
continue
|
|
}
|
|
t := z.Token()
|
|
if rawTextElements[t.Data] {
|
|
z.NextIsNotRawText()
|
|
}
|
|
if err := unsafeAttrs(t, doc, []int{start, offset}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|