Karta.Mail.SafeHTML (Karta v1.0.0)

The HTML of a message, made safe to show in the admin interface: nothing in the result runs, and nothing in it is loaded from anywhere else.

The admin interface shows the result in a sandboxed frame under a strict content security policy; this module is the first line, so that nothing remote is even requested and the frame's other defences are never needed.

The document is parsed as a browser parses it (LazyHTML, an HTML5 parser), then written again from what is kept:

  • Elements on a list of harmless ones (text, tables, lists, headings, img, style, ...) are kept. Elements that run, load, embed or submit something (script, iframe, object, link, meta, base, form controls, svg, math, template, media, ...) are removed with their content. Any other element (form, noscript, vendor tags such as o:p, custom elements) is replaced by its content. Comments are removed.
  • Attributes on a list of harmless ones are kept (style, class, align, width, bgcolor, alt, title, ...); all others, event handlers included, are removed. background, srcset, poster and every other attribute that names a resource are not on the list.
  • Links do not navigate: an href of http:, https: or mailto: becomes the link's title, so that it can still be read; every href is removed.
  • Images keep src only for data: URIs of PNG, GIF, JPEG and WebP images, and for cid: references to the message's own images, which inline turns into data: URIs (at most 2 MiB of them in all). Any other src is removed.
  • CSS (style attributes and elements) loses its comments, its backslash escapes and every <; @import is removed, url() keeps only data: URIs of the image types above, and image(), image-set(), cross-fade(), element(), src() and expression() are made invalid.

The result is a whole document (<!DOCTYPE html>, UTF-8).

Summary

Types

Finds an image of the message by its Content-ID: {content_type, data} or nil.

Functions

CSS made unable to load anything: comments, backslash escapes, < and @import removed, url() kept only for data: images, other functions that load images made invalid.

The safe document for html; inline finds the images cid: URLs name.

Types

inline()

@type inline() :: (String.t() -> {String.t(), binary()} | nil)

Finds an image of the message by its Content-ID: {content_type, data} or nil.

Functions

css(css)

@spec css(String.t()) :: String.t()

CSS made unable to load anything: comments, backslash escapes, < and @import removed, url() kept only for data: images, other functions that load images made invalid.

document(html, inline \\ fn _cid -> nil end)

@spec document(String.t(), inline()) :: String.t()

The safe document for html; inline finds the images cid: URLs name.