Updated: `simple_format` sanitizes HTML instead of escaping it

Posted . Visible to the public. Auto-destruct in 60 days
  • Clarified how simple_format sanitizes
  • Several quirks you likely don't want to encounter
  • Clearer suggestion on when to use format_linebreaks and when using simple_format with sanitize_options

Changes

  • -[`simple_format`](https://apidock.com/rails/v3.2.8/ActionView/Helpers/TextHelper/simple_format) ignores Rails' XSS protection. Even when called with an unsafe string, HTML characters will *not* be escaped or stripped!
  • +The [`simple_format`](https://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format) helper method in Rails converts linebreaks of a given string into HTML paragraphs (`<p>...</p>`) or linebreaks (`<br>`).
  • -Instead `simple_format` calls `sanitize` on each of the generated paragraphs.
  • -```rb
  • -ActionView::Base.sanitized_allowed_tags
  • -# => #<Set: {"small", "dfn", "sup", "sub", "pre", "blockquote", "ins", "ul", "var", "samp", "del", "h6", "h5", "h4", "h3", "h2", "h1", "span", "br", "hr", "em", "address", "img", "kbd", "tt", "a", "acronym", "abbr", "code", "p", "i", "b", "strong", "dd", "dt", "dl", "ol", "li", "div", "big", "cite"}>
  • +It does not return a plain `String`, but produces `html_safe` output (i.e. a `SafeBuffer`). The input String is cleaned up via [`sanitize`](https://apidock.com/rails/ActionView/Helpers/SanitizeHelper/sanitize).
  • +
  • +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:
  • +
  • +```ruby
  • +>> 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:
  • +
  • +```ruby
  • +>> 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:
  • +
  • +```ruby
  • +>> 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:
  • +
  • +```ruby
  • +>> simple_format("Hi <script>alert(1)</script> there")
  • +=> "<p>Hi alert(1) there</p>"
  • ```
  • -
  • -If you don't want user input with markup to appear as HTML, you need to escape yourself:
  • -```rb
  • -simple_format(h(user_input))
  • -
  • +Unexpected: An unclosed `<` followed by a letter swallows the rest of the text, including later paragraphs:
  • +
  • +```ruby
  • +>> 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:
  • +
  • +```ruby
  • +>> 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>"
  • +
  • ```
  • -## Custom sanitization
  • -If you're using Rails 7.1 you can also customize your sanitize opions that `simple_format` uses. E.g if you want to disallow all HTML code in the output:
  • +### 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:
  • ```ruby
  • -user_input = "
  • -Hello World
  • +>> 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.
  • +
  • +> [!NOTE "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](/makandra/52268).
  • +> 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.
  • +
  • -<img src="evil.png">
  • +## Allowing some HTML in user input
  • -<script>alert(1)</script>
  • -"
  • -simple_format(user_input, { class: 'paragraph' }, { sanitize_options: { tags: [], attributes: [] } })
  • -
  • +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:
  • +
  • +```ruby
  • +>> 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>"
  • +
  • ```
  • -will result in
  • -```html
  • -<p class="paragraph">Hello World</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`:
  • -<p class="paragraph">alert(1)</p>
  • +```ruby
  • +>> simple_format("Hello <b>World</b>, write to <jane@example.com>", {}, sanitize_options: { tags: [], attributes: [] })
  • +=> "<p>Hello World, write to </p>"
  • ```
  • -For Rails versions that don't support this option, consider [using a custom method](/makandra/52268).
  • -
  • +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
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2026-11-24 08:34)