package email
/*
sanitize.go
Cleans HTML mail before it reaches the reading pane.
The Mail app renders message HTML inside a sandboxed iframe (no scripts, no
forms, no top navigation) with a Content-Security-Policy, which is the real
security boundary. This pass is the second layer and the privacy layer:
- removes scripts, frames, plug-ins, forms, meta refreshes,
" + html.EscapeString(input) + ""} } var clean func(node *html.Node) clean = func(node *html.Node) { for child := node.FirstChild; child != nil; { next := child.NextSibling switch child.Type { case html.CommentNode, html.DoctypeNode: //Comments carry Outlook conditional markup only Outlook renders node.RemoveChild(child) case html.ElementNode: name := strings.ToLower(child.Data) if child.Namespace != "" && child.Namespace != "html" { node.RemoveChild(child) } else if droppedElements[name] { node.RemoveChild(child) } else if unwrappedElements[name] { clean(child) for grandchild := child.FirstChild; grandchild != nil; { nextGrandchild := grandchild.NextSibling child.RemoveChild(grandchild) node.InsertBefore(grandchild, child) grandchild = nextGrandchild } node.RemoveChild(child) } else { sanitizeAttributes(child, options, &result) if name == "style" { sanitizeStyleElement(child, options, &result) } else { clean(child) } } } child = next } } clean(document) var buffer bytes.Buffer if err := html.Render(&buffer, document); err != nil { return sanitizeResult{html: "
" + html.EscapeString(input) + ""} } result.html = buffer.String() return result } func sanitizeAttributes(node *html.Node, options sanitizeOptions, result *sanitizeResult) { name := strings.ToLower(node.Data) kept := node.Attr[:0] for _, attribute := range node.Attr { key := strings.ToLower(attribute.Key) value := attribute.Val if attribute.Namespace != "" || strings.HasPrefix(key, "on") || droppedAttributes[key] || strings.Contains(key, ":") { continue } switch key { case "href": safe, ok := safeLinkURL(value) if !ok { continue } value = safe case "src": resolved, remote, ok := resolveImageURL(value, options) if remote { result.hasRemote = true if !options.allowRemote { kept = append(kept, html.Attribute{Key: "data-remote-src", Val: value}) continue } } if !ok { continue } value = resolved case "srcset": if !options.allowRemote { if strings.Contains(strings.ToLower(value), "http") { result.hasRemote = true } continue } case "background", "poster": resolved, remote, ok := resolveImageURL(value, options) if remote { result.hasRemote = true if !options.allowRemote { continue } } if !ok { continue } value = resolved case "style": value = sanitizeCSS(value, options, result) case "target", "rel": //Replaced below for links if name == "a" || name == "area" { continue } } kept = append(kept, html.Attribute{Key: key, Val: value}) } node.Attr = kept if name == "a" || name == "area" { node.Attr = append(node.Attr, html.Attribute{Key: "target", Val: "_blank"}, html.Attribute{Key: "rel", Val: "noopener noreferrer nofollow"}) } } func sanitizeStyleElement(node *html.Node, options sanitizeOptions, result *sanitizeResult) { var css strings.Builder for child := node.FirstChild; child != nil; child = child.NextSibling { if child.Type == html.TextNode { css.WriteString(child.Data) } } for child := node.FirstChild; child != nil; { next := child.NextSibling node.RemoveChild(child) child = next } cleaned := sanitizeCSS(css.String(), options, result) //A literal "")) if data, found := options.cid[id]; found { return data, false, true } return "", false, false case strings.HasPrefix(lower, "data:image/"): return value, false, true case strings.HasPrefix(lower, "http://"), strings.HasPrefix(lower, "https://"): return value, true, true case strings.HasPrefix(lower, "//"): return "https:" + value, true, true } return "", false, false } func stripControlChars(value string) string { return strings.Map(func(r rune) rune { if r <= 0x20 || r == 0x7f { return -1 } return r }, value) }