Home / patterns

Rendering user or AI output as raw HTML (XSS)

Quick answer: AI-built apps often render rich text, markdown previews or chatbot replies with dangerouslySetInnerHTML, innerHTML or a markdown plugin that allows raw HTML. If any of that text comes from users or from an AI model, an attacker can slip in script that runs in other users' browsers.

Last updated November 12, 2026

Steps

  1. Search your code for dangerouslySetInnerHTML, innerHTML, outerHTML, insertAdjacentHTML and document.write. For each one, trace where the string comes from.
  2. If the content is plain text, delete the raw-HTML call and render it as a normal React child or with textContent. React escapes it for you.
  3. If you really need HTML (rich-text posts, email previews), sanitize it right before rendering with a maintained sanitizer such as DOMPurify, and don't change the HTML after sanitizing.
  4. Markdown: react-markdown escapes HTML by default. If you added rehype-raw or a custom urlTransform, either remove it or add rehype-sanitize with an allow-list.
  5. Treat AI chatbot output exactly like user input. Don't render model output as raw HTML, and never pass it to eval, a shell or a database query.
  6. Add a Content Security Policy as a backstop, and test by saving a harmless test string such as <img src=x onerror=alert(1)> in every field that ends up displayed to someone else.

Why it matters

React's documentation warns that dangerouslySetInnerHTML should be used with extreme caution: if the HTML isn't from a completely trusted source, it's trivial to introduce an XSS vulnerability. OWASP's XSS Prevention Cheat Sheet recommends HTML sanitization with DOMPurify when you must render user HTML, and warns that modifying content after sanitizing can undo it.

OWASP's Top 10 for LLM Applications (2025) lists Improper Output Handling as LLM05. One of its examples is JavaScript or Markdown generated by a model and returned to a user, where the browser interprets it and the result is XSS. Its advice is to treat the model like any other user and encode its output for the context where it's used.

The react-markdown docs say the library is secure by default, but that overriding urlTransform insecurely or adding plugins can open XSS holes, and they recommend rehype-sanitize to keep content safe after plugins run.

Sources

React Docs: Common components (dangerouslySetInnerHTML) — https://react.dev/reference/react-dom/components/common

OWASP Cross Site Scripting Prevention Cheat Sheet — https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html

OWASP Top 10 for LLM Applications 2025, LLM05: Improper Output Handling — https://genai.owasp.org/llmrisk/llm052025-improper-output-handling/

react-markdown README (Security section) — https://github.com/remarkjs/react-markdown

DOMPurify — https://github.com/cure53/DOMPurify

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.