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.
Diagram
From a formatted field to someone else’s browser
Step 1 of 4
Write
A person submits formatted text. You intended a few tags, not a program.
# Someone wrote a paragraph. # The feature allows a few tags, not a new document.
The field is stored as submitted.
It is inserted as HTML.
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.
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.
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.
Links and contexts
- What fails
- An allowed anchor is still a problem if the href can be a script URL, or if the same string is later placed in an attribute or a script block.
- Who is affected
- The person who clicks, and the person whose page embedded the fragment.
- What you would notice
- href values that are not https, or one sanitizer used for HTML and a different context that does not sanitize.
- What you change
- Allow only https links on output. Encode for the context if the string is not being inserted as that sanitized HTML.
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.
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.