sanitize.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package email
  2. /*
  3. sanitize.go
  4. Cleans HTML mail before it reaches the reading pane.
  5. The Mail app renders message HTML inside a sandboxed iframe (no scripts, no
  6. forms, no top navigation) with a Content-Security-Policy, which is the real
  7. security boundary. This pass is the second layer and the privacy layer:
  8. - removes scripts, frames, plug-ins, forms, meta refreshes, <base> and
  9. event handler attributes, and javascript:/data: links;
  10. - resolves cid: references to the message's own inline images;
  11. - blocks remote images, backgrounds and style sheets unless the user
  12. allowed them (tracking pixels are the main reason mail clients do
  13. this), reporting hasRemote so the UI can offer "Load images";
  14. - drops relative URLs, which would otherwise resolve against ArozOS.
  15. */
  16. import (
  17. "bytes"
  18. "regexp"
  19. "strings"
  20. "golang.org/x/net/html"
  21. )
  22. type sanitizeOptions struct {
  23. allowRemote bool
  24. cid map[string]string //Lower-case Content-ID -> data: URI
  25. }
  26. type sanitizeResult struct {
  27. html string
  28. hasRemote bool
  29. }
  30. // Elements removed together with everything inside them.
  31. var droppedElements = map[string]bool{
  32. "script": true, "noscript": true, "iframe": true, "frame": true, "frameset": true,
  33. "object": true, "embed": true, "applet": true, "param": true, "base": true,
  34. "meta": true, "link": true, "title": true, "template": true, "svg": true,
  35. "math": true, "input": true, "select": true, "textarea": true, "option": true,
  36. "video": true, "audio": true, "source": true, "track": true, "canvas": true,
  37. "portal": true, "dialog": true, "xml": true, "xss": true,
  38. }
  39. // Elements replaced by their children.
  40. var unwrappedElements = map[string]bool{
  41. "form": true, "button": true, "label": true, "fieldset": true, "legend": true,
  42. }
  43. // Attributes removed wherever they appear.
  44. var droppedAttributes = map[string]bool{
  45. "formaction": true, "action": true, "srcdoc": true, "ping": true,
  46. "http-equiv": true, "dynsrc": true, "lowsrc": true, "integrity": true,
  47. "nonce": true, "autofocus": true, "contenteditable": true, "draggable": true,
  48. "accesskey": true, "tabindex": true, "manifest": true,
  49. }
  50. var (
  51. cssURLPattern = regexp.MustCompile(`(?i)url\(\s*(?:"([^"]*)"|'([^']*)'|([^)]*?))\s*\)`)
  52. cssImportPattern = regexp.MustCompile(`(?i)@import\s+[^;]*;?`)
  53. cssDangerPattern = regexp.MustCompile(`(?i)(expression\s*\(|behavior\s*:|-moz-binding|javascript\s*:|vbscript\s*:)`)
  54. )
  55. // sanitizeHTML cleans an HTML document. The result is a complete document
  56. // (html/head/body) ready to be placed in an iframe's srcdoc.
  57. func sanitizeHTML(input string, options sanitizeOptions) sanitizeResult {
  58. result := sanitizeResult{}
  59. document, err := html.Parse(strings.NewReader(input))
  60. if err != nil {
  61. //html.Parse only fails on reader errors; fall back to escaped text
  62. return sanitizeResult{html: "<pre>" + html.EscapeString(input) + "</pre>"}
  63. }
  64. var clean func(node *html.Node)
  65. clean = func(node *html.Node) {
  66. for child := node.FirstChild; child != nil; {
  67. next := child.NextSibling
  68. switch child.Type {
  69. case html.CommentNode, html.DoctypeNode:
  70. //Comments carry Outlook conditional markup only Outlook renders
  71. node.RemoveChild(child)
  72. case html.ElementNode:
  73. name := strings.ToLower(child.Data)
  74. if child.Namespace != "" && child.Namespace != "html" {
  75. node.RemoveChild(child)
  76. } else if droppedElements[name] {
  77. node.RemoveChild(child)
  78. } else if unwrappedElements[name] {
  79. clean(child)
  80. for grandchild := child.FirstChild; grandchild != nil; {
  81. nextGrandchild := grandchild.NextSibling
  82. child.RemoveChild(grandchild)
  83. node.InsertBefore(grandchild, child)
  84. grandchild = nextGrandchild
  85. }
  86. node.RemoveChild(child)
  87. } else {
  88. sanitizeAttributes(child, options, &result)
  89. if name == "style" {
  90. sanitizeStyleElement(child, options, &result)
  91. } else {
  92. clean(child)
  93. }
  94. }
  95. }
  96. child = next
  97. }
  98. }
  99. clean(document)
  100. var buffer bytes.Buffer
  101. if err := html.Render(&buffer, document); err != nil {
  102. return sanitizeResult{html: "<pre>" + html.EscapeString(input) + "</pre>"}
  103. }
  104. result.html = buffer.String()
  105. return result
  106. }
  107. func sanitizeAttributes(node *html.Node, options sanitizeOptions, result *sanitizeResult) {
  108. name := strings.ToLower(node.Data)
  109. kept := node.Attr[:0]
  110. for _, attribute := range node.Attr {
  111. key := strings.ToLower(attribute.Key)
  112. value := attribute.Val
  113. if attribute.Namespace != "" || strings.HasPrefix(key, "on") || droppedAttributes[key] || strings.Contains(key, ":") {
  114. continue
  115. }
  116. switch key {
  117. case "href":
  118. safe, ok := safeLinkURL(value)
  119. if !ok {
  120. continue
  121. }
  122. value = safe
  123. case "src":
  124. resolved, remote, ok := resolveImageURL(value, options)
  125. if remote {
  126. result.hasRemote = true
  127. if !options.allowRemote {
  128. kept = append(kept, html.Attribute{Key: "data-remote-src", Val: value})
  129. continue
  130. }
  131. }
  132. if !ok {
  133. continue
  134. }
  135. value = resolved
  136. case "srcset":
  137. if !options.allowRemote {
  138. if strings.Contains(strings.ToLower(value), "http") {
  139. result.hasRemote = true
  140. }
  141. continue
  142. }
  143. case "background", "poster":
  144. resolved, remote, ok := resolveImageURL(value, options)
  145. if remote {
  146. result.hasRemote = true
  147. if !options.allowRemote {
  148. continue
  149. }
  150. }
  151. if !ok {
  152. continue
  153. }
  154. value = resolved
  155. case "style":
  156. value = sanitizeCSS(value, options, result)
  157. case "target", "rel":
  158. //Replaced below for links
  159. if name == "a" || name == "area" {
  160. continue
  161. }
  162. }
  163. kept = append(kept, html.Attribute{Key: key, Val: value})
  164. }
  165. node.Attr = kept
  166. if name == "a" || name == "area" {
  167. node.Attr = append(node.Attr,
  168. html.Attribute{Key: "target", Val: "_blank"},
  169. html.Attribute{Key: "rel", Val: "noopener noreferrer nofollow"})
  170. }
  171. }
  172. func sanitizeStyleElement(node *html.Node, options sanitizeOptions, result *sanitizeResult) {
  173. var css strings.Builder
  174. for child := node.FirstChild; child != nil; child = child.NextSibling {
  175. if child.Type == html.TextNode {
  176. css.WriteString(child.Data)
  177. }
  178. }
  179. for child := node.FirstChild; child != nil; {
  180. next := child.NextSibling
  181. node.RemoveChild(child)
  182. child = next
  183. }
  184. cleaned := sanitizeCSS(css.String(), options, result)
  185. //A literal "</style" inside the text would end the element early
  186. cleaned = strings.ReplaceAll(cleaned, "</", "<\\/")
  187. node.AppendChild(&html.Node{Type: html.TextNode, Data: cleaned})
  188. }
  189. // sanitizeCSS neutralises legacy script hooks and controls url() loads.
  190. func sanitizeCSS(css string, options sanitizeOptions, result *sanitizeResult) string {
  191. css = cssDangerPattern.ReplaceAllString(css, "blocked-")
  192. if cssImportPattern.MatchString(css) {
  193. result.hasRemote = true
  194. css = cssImportPattern.ReplaceAllString(css, "")
  195. }
  196. return cssURLPattern.ReplaceAllStringFunc(css, func(match string) string {
  197. groups := cssURLPattern.FindStringSubmatch(match)
  198. raw := ""
  199. for _, group := range groups[1:] {
  200. if group != "" {
  201. raw = group
  202. break
  203. }
  204. }
  205. resolved, remote, ok := resolveImageURL(raw, options)
  206. if remote {
  207. result.hasRemote = true
  208. if !options.allowRemote {
  209. return "none"
  210. }
  211. }
  212. if !ok {
  213. return "none"
  214. }
  215. return `url("` + strings.ReplaceAll(resolved, `"`, "%22") + `")`
  216. })
  217. }
  218. // safeLinkURL accepts absolute web, mail and phone links and in-page anchors.
  219. func safeLinkURL(raw string) (string, bool) {
  220. value := strings.TrimSpace(raw)
  221. if value == "" {
  222. return "", false
  223. }
  224. if strings.HasPrefix(value, "#") {
  225. return value, true
  226. }
  227. lower := strings.ToLower(stripControlChars(value))
  228. for _, scheme := range []string{"http://", "https://", "mailto:", "tel:", "sms:", "ftp://"} {
  229. if strings.HasPrefix(lower, scheme) {
  230. return value, true
  231. }
  232. }
  233. //Protocol-relative links default to https
  234. if strings.HasPrefix(lower, "//") {
  235. return "https:" + value, true
  236. }
  237. return "", false
  238. }
  239. // resolveImageURL decides what an image reference becomes. remote reports a
  240. // network fetch; ok reports whether the (possibly rewritten) value may stay.
  241. func resolveImageURL(raw string, options sanitizeOptions) (resolved string, remote bool, ok bool) {
  242. value := strings.TrimSpace(raw)
  243. lower := strings.ToLower(stripControlChars(value))
  244. switch {
  245. case value == "":
  246. return "", false, false
  247. case strings.HasPrefix(lower, "cid:"):
  248. id := strings.ToLower(strings.Trim(strings.TrimSpace(value[4:]), "<>"))
  249. if data, found := options.cid[id]; found {
  250. return data, false, true
  251. }
  252. return "", false, false
  253. case strings.HasPrefix(lower, "data:image/"):
  254. return value, false, true
  255. case strings.HasPrefix(lower, "http://"), strings.HasPrefix(lower, "https://"):
  256. return value, true, true
  257. case strings.HasPrefix(lower, "//"):
  258. return "https:" + value, true, true
  259. }
  260. return "", false, false
  261. }
  262. func stripControlChars(value string) string {
  263. return strings.Map(func(r rune) rune {
  264. if r <= 0x20 || r == 0x7f {
  265. return -1
  266. }
  267. return r
  268. }, value)
  269. }