Stored XSS in rich text

A comment, a profile, or a document that allows formatting is stored and later parsed as HTML for someone else. The sanitizer is the control, and it has to match the context.

Lesson 53 of 69 · 15 min read · practice only with authorization

Diagram

From a formatted field to someone else’s browser

Follow the path. One example at a time.

Step 1 of 4

Write

A person submits formatted text. You intended a few tags, not a program.

Illustration: the draft is not a whole page
# Someone wrote a paragraph.
# The feature allows a few tags, not a new document.
What changes the outcome
Save

The field is stored as submitted.

Render

It is inserted as HTML.

Reader

Their session runs whatever parsed.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Why rich text is a parser problem

    What fails
    The product asked for bold and links. The browser will also accept script, event handlers, and URLs you did not mean, if you give it the raw string.
    Who is affected
    The next person who opens the document, not only the author.
    What you would notice
    A field stored and later sent with a content type or a template that parses HTML.
    What you change
    Decide the tag list first. Reject everything that is not on it.
  2. Sanitize on the way out too

    What fails
    A bug in an older sanitizer, or a write that bypassed it, stays in the database. Output is the last place you can refuse.
    Who is affected
    Everyone who views a row that was stored before the fix.
    What you would notice
    A migration that only cleans new writes.
    What you change
    Run the same allow-list when you render. Do not trust the column just because you wrote it.
  3. The reader’s session

    What fails
    Stored XSS runs as the reader. A staff tool that renders customer text is how a customer reaches a staff session.
    Who is affected
    Staff and other customers who open the record.
    What you would notice
    An admin view that “just displays the ticket” by inserting HTML.
    What you change
    Sanitize the admin view too. HttpOnly cookies and a strict content security policy limit the leftovers.

Formatting is a product decision

Plain text is the easy case: encode it for HTML and you are done. Rich text means you promised bold, italics, lists, or links. That promise is an allow-list. Everything else — event handler attributes, unexpected tags, unexpected URL schemes — is a bug, not a feature the editor happened to permit.

Build the list with someone who owns the product, write it down, and pass it to a sanitizer you keep updated. A visual editor is not a sanitizer. It runs in the author’s browser. The reader’s browser will parse whatever you stored.

Clean on write and on read

Sanitize before you store so the database is not a warehouse of discarded attempts. Sanitize again when you render, because old rows, imports, and a missed code path will be in there. The render path is the one the victim’s browser hits.

If the same column is also placed into an attribute, a JSON string inside a script, or an email, the HTML sanitizer’s output is not automatically safe there. Encode for that context, or do not reuse the HTML fragment.

The reader is the person affected

Stored XSS is aimed at the next session that renders the field. In a support tool that is an agent. In a shared document that is a coworker. In a public profile that is anyone who looks. The author being authenticated does not make their text trustworthy to other origins’ users. It is still their text.

You would notice a staff page that inserts ticket bodies as HTML, a missing content security policy, and links that are not restricted to https. The finding names the field, the reader, and the allow-list. It does not ship a sample document.

What you change

Adopt one sanitizer, configure the tag and attribute list, restrict anchors to https, and use it on write and on read. Encode the plain-text parts of the page the way you always should. Mark session cookies HttpOnly. Send a content security policy without inline script.

Re-render old rows through the sanitizer in a migration if you want the column itself clean, but do not wait for that migration to protect readers. The output path has to be safe on the day you deploy.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A comment field allows bold and links. The raw string is stored and inserted as HTML when someone else opens the thread. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapCross-site scripting: reflected, stored, and DOM-based, and the defenses that stackHow untrusted text becomes script in another user’s browser, how to tell reflected, stored, and DOM-based XSS apart in a review, and why output encoding, a strict CSP, and HttpOnly cookies work together.
  1. 01DOM XSS: sinks in the browserA page can be safe on the wire and still treat a URL, a message, or a stored string as HTML once script already on the page assigns it.

Practice

  1. LabControl benchA failure is described. Choose the control that changes the outcome.

Glossary

  1. CardCross-site scriptingA page treats attacker-controlled text as code in someone else's browser, which can steal sessions or rewrite the UI.