`simple_format` sanitizes HTML instead of escaping it

Updated . Posted . Visible to the public. Repeats.

The simple_format Show archive.org snapshot helper method in Rails converts linebreaks of a given string into HTML paragraphs (<p>...</p>) or linebreaks (<br>).

It does not return a plain String, but produces html_safe output (i.e. a SafeBuffer). The input String is cleaned up via sanitize Show archive.org snapshot .

This is not an XSS vulnerability: scripts, event handlers and javascript: URLs are removed. [1]
But simple_format does treat its input as HTML. When it is actually plain text (e.g. something a user typed into a textarea), this has unwanted side effects.

Side Effects

1. Users can add markup

Everything on the sanitizer's safe list is rendered as HTML:

>> ActionView::Base.sanitized_allowed_tags.sort
=> ["a", "abbr", "acronym", "address", "b", "big", "blockquote", "br", "cite", "code", "dd", "del", "dfn", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", "kbd", "li", "mark", "ol", "p", "pre", "samp", "small", "span", "strong", "sub", "sup", "time", "tt", "ul", "var"]

>> ActionView::Base.sanitized_allowed_attributes.sort
=> ["abbr", "alt", "cite", "class", "datetime", "height", "href", "lang", "name", "src", "title", "width", "xml:lang"]

So users can add headings, links, remote images (think tracking pixels) and any of your CSS classes, but not inherently dangerous attributes like onclick, or javascript: hrefs. Still, this is likely undesired in most cases. Example:

>> simple_format('<a href="javascript:alert(1)" onclick="x()" class="btn">link</a> <img src="https://evil.example.com/tracking-pixel.png">')
=> "<p><a class=\"btn\">link</a> <img src=\"https://evil.example.com/tracking-pixel.png\"></p>"

2. Text that looks like HTML is dropped

Anything the HTML parser considers a tag is removed if it is not on the safe list. The user never meant it as markup, but it is gone:

>> simple_format("Contact Jane <jane@example.com> for help")
=> "<p>Contact Jane  for help</p>"

>> simple_format("Use Array<String> here")
=> "<p>Use Array here</p>"

>> simple_format("Write comments like <!-- example --> in HTML")
=> "<p>Write comments like  in HTML</p>"

Note that a disallowed tag is removed, but its content stays:

>> simple_format("Hi <script>alert(1)</script> there")
=> "<p>Hi alert(1) there</p>"

Unexpected: An unclosed < followed by a letter swallows the rest of the text, including later paragraphs:

>> simple_format("Price <x and more text\n\nSecond paragraph")
=> "<p>Price </p>"

(Side note: A < not followed by a letter is fine, e.g. simple_format("I <3 Ruby") => "<p>I &lt;3 Ruby</p>")

3. Users can produce invalid HTML

The text is sanitized as a whole before it is split into paragraphs, so the result may even be invalid HTML:

>> simple_format("<strong>bold\n\nnext paragraph")
=> "<p><strong>bold</p>\n\n<p>next paragraph</strong></p>"

>> simple_format("<h1>Huge</h1>")
=> "<p><h1>Huge</h1></p>"

4. Odd behavior with blank input

  • Blank input renders a paragraph, e.g. simple_format("") → "<p></p>".
  • Leading blank lines produce an empty paragraph: simple_format("\n\nfoo") → "<p></p>\n\n<p>foo</p>".

Formatting plain-text user input

You can escape the text before formatting it with simple_format. Example:

>> simple_format(h("Contact Jane <jane@example.com>\nfor help"))
=> "<p>Contact Jane &lt;jane@example.com&gt;\n<br />for help</p>"

While this works, it is easy to forget the h.
Nothing will fail when you do: output for common input looks correct until a user types something that resembles a tag.

We recommend not calling simple_format to format plain-text input.

Use a well-named helper method that does only linebreak formatting, like the format_linebreaks helper we posted in a separate card.
If you only need to preserve line breaks, plain CSS (white-space: pre-line or pre-wrap) on regularly escaped output needs no helper at all.

Allowing some HTML in user input

To allow some HTML, it makes sense to use simple_format, but specify which HTML is allowed.

Since Rails 7.1, you can pass sanitize_options to change the safe list:

>> simple_format(
  'Some <strong>bold</strong> and <em>italic</em> text, but <h1>no heading</h1> and no <a href="https://example.com">link</a>',
  {},
  sanitize_options: { tags: %w[strong em], attributes: [] }
)
=> "<p>Some <strong>bold</strong> and <em>italic</em> text, but no heading and no link</p>"

Note that this only changes which tags are stripped. It still does not escape anything, so an empty safe list is no replacement for h:

>> simple_format("Hello <b>World</b>, write to <jane@example.com>", {}, sanitize_options: { tags: [], attributes: [] })
=> "<p>Hello World, write to </p>"

Never pass sanitize: false for user input. The text is then returned as html_safe without any sanitizing, which is an XSS vulnerability.


[1] In ancient Rails 2, simple_format neither escaped nor sanitized its input. Calling it with user input led to XSS unless you escaped the input yourself.

Profile picture of Arne Hartherz
Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2012-09-18 09:04)