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, and event handler attributes, and javascript:/data: links; - resolves cid: references to the message's own inline images; - blocks remote images, backgrounds and style sheets unless the user allowed them (tracking pixels are the main reason mail clients do this), reporting hasRemote so the UI can offer "Load images"; - drops relative URLs, which would otherwise resolve against ArozOS. */ import ( "bytes" "regexp" "strings" "golang.org/x/net/html" ) type sanitizeOptions struct { allowRemote bool cid map[string]string //Lower-case Content-ID -> data: URI } type sanitizeResult struct { html string hasRemote bool } // Elements removed together with everything inside them. var droppedElements = map[string]bool{ "script": true, "noscript": true, "iframe": true, "frame": true, "frameset": true, "object": true, "embed": true, "applet": true, "param": true, "base": true, "meta": true, "link": true, "title": true, "template": true, "svg": true, "math": true, "input": true, "select": true, "textarea": true, "option": true, "video": true, "audio": true, "source": true, "track": true, "canvas": true, "portal": true, "dialog": true, "xml": true, "xss": true, } // Elements replaced by their children. var unwrappedElements = map[string]bool{ "form": true, "button": true, "label": true, "fieldset": true, "legend": true, } // Attributes removed wherever they appear. var droppedAttributes = map[string]bool{ "formaction": true, "action": true, "srcdoc": true, "ping": true, "http-equiv": true, "dynsrc": true, "lowsrc": true, "integrity": true, "nonce": true, "autofocus": true, "contenteditable": true, "draggable": true, "accesskey": true, "tabindex": true, "manifest": true, } var ( cssURLPattern = regexp.MustCompile(`(?i)url\(\s*(?:"([^"]*)"|'([^']*)'|([^)]*?))\s*\)`) cssImportPattern = regexp.MustCompile(`(?i)@import\s+[^;]*;?`) cssDangerPattern = regexp.MustCompile(`(?i)(expression\s*\(|behavior\s*:|-moz-binding|javascript\s*:|vbscript\s*:)`) ) // sanitizeHTML cleans an HTML document. The result is a complete document // (html/head/body) ready to be placed in an iframe's srcdoc. func sanitizeHTML(input string, options sanitizeOptions) sanitizeResult { result := sanitizeResult{} document, err := html.Parse(strings.NewReader(input)) if err != nil { //html.Parse only fails on reader errors; fall back to escaped text return sanitizeResult{html: "
" + 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) }