Braces HTML-escapes the values a template writes, in every language: Handlebars as Handlebars.js does, Mustache as its spec says, and Liquid and the Jinja family by default, as a Jinja engine does with autoescaping on. This page covers what that protects, what it doesn't, and what to do when the people who write your templates aren't the people you trust with your page. Its examples are Handlebars, which the Jinja family and Liquid render by way of.
What's escaped
{{value}} escapes &, <, >, ", ', ` and =. That holds for every way a
template reaches a value: paths, lookup, #each, #with, block params, @ variables,
../, partials, and the result of a helper called as {{helper}}. It holds for every kind of
data: strings, numbers, lists, maps, structs, JSON from Braces.Handlebars.decode_json/1, and strings
that aren't valid UTF-8.
Data can't turn escaping off. The values written as they are, Braces.Handlebars.safe/1 text and
{:safe, iodata} tuples, are made by your code: decoded JSON, form params and database rows
can't make them.
What escaping doesn't protect
Escaping makes a value safe in HTML text and in a quoted attribute. It doesn't make it safe elsewhere:
| Template | Value | Result |
|---|---|---|
<a href="{{url}}"> | javascript:alert(1) | A link that runs JavaScript. Check URL schemes in a helper. |
<script>var a = "{{x}}", b = "{{y}}"</script> | \ and ;alert(1);// | The \ escapes the closing quote, and y runs. Keep values out of <script>: pass them in a data- attribute. |
<div style="color: {{c}}"> | red; background: url(//evil) | Injected CSS. Keep values out of style. |
<a href={{u}}> | x autofocus onfocus | Added attributes. = is escaped, so they get no values, but always quote attributes. |
Serve your pages as UTF-8, which Phoenix does.
What's written without escaping
These write values as they are, by design, as in Handlebars.js:
{{{value}}}and{{&value}}.- A block helper's result:
{{#lookup . "bio"}}{{/lookup}}writesbioas it is, and so does the raw block{{{{lookup . "bio"}}}}{{{{/lookup}}}}.#if,#unless,#eachand#withonly render their blocks, whose values are escaped. - Your helpers'
Braces.Handlebars.safe/1results, and what your block helpers return. A helper that builds HTML escapes what it puts in it withBraces.Handlebars.escape/1. - Rendering with
no_escape: true, for text that isn't HTML.Braces.Handlebars.HTML.render!/3marks its result as safe HTML even then. - Phoenix
.hbstemplates in formats that aren't HTML, such as.text.hbs,.xml.hbsor.svg.hbs, as Phoenix writes.eextemplates in those formats. Don't put data you don't trust in an.svg.hbs.
Templates people write
When people other than your developers write templates, such as email templates in your app, there are two risks:
- The author writes HTML. A template's text is HTML, so its author can write a
<script>. Render templates from authors you don't trust with your page in a sandbox, such as<iframe sandbox srcdoc="…">, or sanitize the HTML they render. - The author writes someone else's data as it is. A trusted author, such as your marketing
team, writes
{{{first_name}}}in a template sent to thousands of people, and one of them signs up with a name that is a<script>. Find these when the template is saved withBraces.Handlebars.unescaped_outputs/2, and reject it:
template = Braces.Handlebars.compile!(~s(Hi {{{first_name}}}! {{#lookup . "bio"}}{{/lookup}}))
for found <- Braces.Handlebars.unescaped_outputs(template, helpers: MyApp.EmailHelpers) do
"#{found.description}, on line #{found.line}"
end
#=> ["\"first_name\" is written without escaping, on line 1",
#=> "The block helper \"lookup\" returns a value that is written without escaping, on line 1"]Pass the helpers you render with, so a block call to one of them is listed too. For a template
in another language, Braces.unescaped_outputs/2 lists the values it writes as they are,
such as Jinja's |safe and Liquid's | raw. See
Validating on save for a changeset that does this.
Rendering doesn't generate code or create atoms, so a template can't run Elixir or exhaust the
atom table. A template can be slow, though: a deeply nested #each over large data renders
for each combination. Limit the size of templates and data you accept.
Errors contain data
A Braces.Handlebars.RenderError's message can contain the data's values, as in
Cannot use 'in' operator to search for 'x' in <img src=x>. HEEx escapes it in
{error.message}. Don't write it into HTML with raw/1.
Conversions
A conversion keeps which values are escaped: a value escaped in the source is escaped in the
converted template, and one written as it is, such as {{{value}}}, |safe or |raw, is
written as it is. Liquid and Jinja engines that don't autoescape get explicit filters:
| escape in Liquid. Braces.Liquid and the Jinja family's modules escape by default, where
their own engines may not; pass escape: false to read a template as an engine without
autoescaping does (see Conversions and
Liquid to Handlebars).