Action Text stores the editor's HTML unsanitized; when is it sanitized, and which ways of outputting it skip that step?
answer
- sanitized at render, not on save
- render_action_text_content in ContentHelper
- safe list plus action-text-attachment
- allowed_tags and allowed_attributes overrides
- body.to_html with raw skips it
basics
~20 sAction Text keeps the submitted HTML as is and sanitizes it when rendering: <%= record.notes %> runs it through a safe-list sanitizer after rendering attachments. Outputting body.to_html or to_trix_html with raw or html_safe skips that, and to_plain_text is not HTML-safe.
solid answer
~40 sThe `body` column stores whatever HTML the form sent, canonicalized but not sanitized. Sanitizing happens on output: `<%= @lesson.notes %>` calls `to_s`, which renders the content partial, whose `render_action_text_content` first renders attachments and then runs `sanitize_action_text_content`, a safe-list sanitizer. With `load_defaults` 7.1 or later the sanitizer is `Rails::HTML::Sanitizer.best_supported_vendor`, the HTML5 one where available. The allow-list is the sanitizer's defaults plus `action-text-attachment`, `figure`, `figcaption` and Action Text's attachment attributes; you change it through `ActionText::ContentHelper.allowed_tags`, `allowed_attributes` or a `scrubber`. Paths that skip it: `notes.body.to_html` or `to_trix_html` marked `raw`/`html_safe`, JSON output of the body (`Content#as_json` is `to_html`) handed to a client that inserts it as HTML, and `to_plain_text` used outside ERB escaping. Attachment partials render inside the content, so sanitizing also applies to their markup.
code
erb · 8 lines<%# safe: sanitized after attachments are rendered %>
<%= @lesson.notes %>
<%# unsafe: stored HTML marked safe without sanitizing %>
<%= raw @lesson.notes.body.to_html %>
<%# safe in ERB: plain text is escaped, but it is not html_safe %>
<%= truncate(@lesson.notes.to_plain_text, length: 140) %>go deeper
Recall that <%= record.notes %> is the safe way to show rich text and that to_plain_text is for excerpts, not raw output.
Explain that sanitizing happens at render time in render_action_text_content, which sanitizer and allow-list apply, and how to adjust them.
Audit for bypasses (body.to_html with raw, JSON APIs, to_plain_text in unescaped contexts) and plan allow-list changes knowing they apply to all stored content.
Set the team's policy for user-authored HTML across views, APIs and emails, and where sanitizing is enforced.
## Where the HTML comes from and how it is stored Trix produces HTML in the browser and puts it in a hidden input. Anything can be posted to that field, so the server must assume the HTML is hostile. When `Lesson#notes=` assigns it, Action Text wraps it in **`ActionText::Content`**, which canonicalizes `<action-text-attachment>` elements, and the `body` column stores the result **without sanitizing it**. A `<script>` or an `onclick` attribute submitted by a crafted request is still in the database. ## The render pipeline `<%= @lesson.notes %>` sanitizes on the way out: 1. `ActionText::RichText#to_s` delegates to `ActionText::Content#to_s`, which renders the `action_text/contents/content` partial inside the `layouts/action_text/contents/_content` layout (a `div.trix-content`). 2. The partial calls **`render_action_text_content(content)`** from `ActionText::ContentHelper`. 3. That method renders every attachment (blobs through `active_storage/blobs/_blob`, attachables through their partials, galleries through the gallery partial) into the HTML. 4. It then calls **`sanitize_action_text_content`**, which passes the whole HTML through `ActionText::ContentHelper.sanitizer` with the allowed tags and attributes, and marks the result `html_safe`. ```erb <%= @lesson.notes %> <%# => <div class="trix-content"><h1>Scales</h1><figure ...><img ...></figure></div> %> ``` Because attachments are rendered before sanitizing, their partials are sanitized too: a custom attachable partial cannot rely on attributes the allow-list drops. ## Which sanitizer and which allow-list | Setting | Rails 8.1 value | |---|---| | `ActionText::ContentHelper.sanitizer` | from `config.action_text.sanitizer_vendor`, set by `load_defaults 7.1` to `Rails::HTML::Sanitizer.best_supported_vendor` (HTML5 parsing when the installed Nokogiri supports it); the bare default is the HTML4 safe-list sanitizer | | allowed tags | the sanitizer's safe-list tags plus `action-text-attachment`, `figure`, `figcaption` | | allowed attributes | the sanitizer's safe-list attributes plus `sgid`, `content-type`, `url`, `href`, `filename`, `filesize`, `width`, `height`, `previewable`, `presentation`, `caption`, `content` | To adjust the list, for example to allow a `<u>` tag or a `data-` attribute that a custom attachable needs: ```ruby # config/initializers/action_text.rb Rails.application.config.after_initialize do ActionText::ContentHelper.allowed_tags = ActionText::ContentHelper.sanitizer.class.allowed_tags + %w[action-text-attachment figure figcaption u] end ``` Setting `allowed_tags` replaces Action Text's additions, so include them again, as above. A `scrubber` (a `Rails::HTML::PermitScrubber` subclass) gives full control. ## Ways to skip the sanitizer - **`raw @lesson.notes.body.to_html`** or `.to_html.html_safe`: outputs the stored HTML unsanitized. - **`@lesson.notes.to_trix_html`** outside the editor: meant only for refilling the hidden input, which the form helper escapes. - **JSON APIs**: serializing the rich text puts its `body` through `ActionText::Content#as_json`, which returns `to_html`, the stored HTML, unsanitized. A front end that assigns it to `innerHTML` executes whatever is in it; render it through Action Text's helpers on the server or sanitize it on the client. - **`to_plain_text`**: strips markup but is **not HTML-safe**; HTML entities in the content decode back into characters, so `<script>` becomes `<script>`. ERB escapes it in `<%= %>`, but `raw`, `html_safe` or JavaScript string interpolation does not. ## A review checklist 1. Render rich text only through `<%= record.attribute %>` or `render_action_text_content`. 2. Grep for `body.to_html`, `to_trix_html` and `html_safe` near rich-text attributes. 3. Check API serializers for raw `body` output. 4. When extending the allow-list, add only what a specific feature needs, and keep Action Text's own tag and attributes. 5. Remember that changing the sanitizer or allow-list applies to all existing content at the next render, because nothing was sanitized at save time.
- If you tighten the allowed tags today, what happens to lessons saved last year?They are affected immediately. Action Text stores the submitted HTML without sanitizing it and sanitizes on every render, so the new allow-list applies the next time old content is displayed. The flip side is that a loosened allow-list also exposes markup stored long ago.
- Why does to_plain_text need care even though it removes tags?Converting to text decodes entities, so content that displayed as the literal text `<script>` becomes a real `<script>` string. The result is not marked HTML-safe, so `<%= %>` escapes it, but putting it in `raw`, an `html_safe` call or a JavaScript string without escaping reintroduces markup.
- Action Text sanitizes the HTML before saving it, so stored content is always safe
- to_plain_text output is HTML-safe and can be rendered with raw
- body.to_html returns the same sanitized markup as rendering the rich text
- Setting allowed_tags adds tags to Action Text's defaults instead of replacing them
- Attachment partials are inserted after sanitizing, so they bypass the allow-list
Assumes Rails 8.1 with load_defaults 8.1, where config.action_text.sanitizer_vendor is best_supported_vendor (set since load_defaults 7.1); without it Action Text falls back to the HTML4 safe-list sanitizer.