Back
Backend

Rails strip_tags is not a sanitizer: what it really returns

Rails strip_tags removes tags, escapes entities and returns HTML-safe text. That combination matters when you squish, truncate or join the result.

JB
Josué Barros
••3 min read
Rails strip_tags is not a sanitizer: what it really returns

strip_tags sounds like a purely structural operation: remove the tags, keep the text. In Rails it does that, and then it does two more things that change how you should use the result.

It escapes HTML-sensitive characters and returns an HTML-safe string.

That behavior is useful for the common case — rendering user-provided text in a view — but it is surprising when the result is treated as an intermediate value.

What the method actually does

The implementation is short:

def strip_tags(html)
  stripped = self.class.full_sanitizer.sanitize(html)
  ERB::Util.html_escape_once(stripped) if stripped
end

The full sanitizer removes tags and comments. html_escape_once escapes characters such as &, < and >. The returned SafeBuffer tells Rails it can be rendered without another escape pass.

So this:

<%= strip_tags("<b>Tom & Jerry</b>") %>

renders:

Tom &amp; Jerry

The browser displays Tom & Jerry, but the string Rails returns is already in HTML form.

Where it goes wrong

The trouble starts when the escaped, HTML-safe string is not the final value. Consider:

text = strip_tags(user_html).squish

squish returns a new string. Depending on the operation and the Rails version, that string may no longer be marked HTML-safe. When the view escapes it again, the already-escaped &amp; can become &amp;amp;, and the user sees the entity instead of the character.

The same class of problem appears with truncate, joining several stripped values, or doing length checks after escaping. The visible length is no longer the text length; &amp; is five characters before the browser turns it into &.

The safer order

Do text transformations before the HTML boundary:

text = truncate(user_html, length: 120)
<%= strip_tags(text) %>

Or keep the value as plain text internally and call strip_tags only at the rendering edge.

If you need to preserve some markup, strip_tags is the wrong tool. Use sanitize with an explicit allowlist. If the value goes into an attribute, a script or a style block, use the appropriate escaping for that context instead of relying on strip_tags.

Why document it

The method name describes the first step, not the whole contract. That is the kind of gap that produces bugs which look like encoding problems and are really API misunderstandings.

I opened rails/rails#60026 to make the contract explicit in the API docs: strip_tags returns HTML-escaped, HTML-safe text, and further transformations should run before it. The docs-only change was merged on November 30, 2026.

What I keep from this merge

A method name is not a specification. If a helper changes escaping, safety or type, document that next to the helper. The next developer should not have to learn it from a double-escaped apostrophe in production.

The pull request is rails/rails#60026.

If you work with Rails view helpers, connect with me on LinkedIn. I write about the contracts behind merged fixes.

FAQ

Does Rails strip_tags escape HTML?
Yes. It strips tags and applies html_escape_once, so characters such as &, < and > are escaped in the returned text.
Is strip_tags enough for user-provided HTML?
It is for plain-text output only. If you need to keep some markup, use sanitize with an allowlist. Do not use strip_tags output inside attributes or script/style contexts.
Why does truncate after strip_tags double-escape text?
The transformation can produce a new string that is no longer marked HTML-safe. When the view escapes it again, already-escaped entities can become visible entities such as &amp;amp;.
#rails#ruby#strip-tags#html#security#documentation

Join the Newsletter

Subscribe to get my latest content by email.

We won't send you spam. Unsubscribe at any time.