Rendering user or AI output as raw HTML (XSS)
Last updated November 12, 2026
Steps
- Search your code for dangerouslySetInnerHTML, innerHTML, outerHTML, insertAdjacentHTML and document.write. For each one, trace where the string comes from.
- 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.
- 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.
- 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.
- 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.
- 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
- Prompt injection in your app's AI chatbot or AI features
- Missing security headers on a vibe-coded site
- JWTs or session tokens stored in localStorage
- Cookie-based login with no CSRF protection on state-changing requests
- Running AI or user output with eval, exec or new Function (code injection)
- Server-side template injection from unsanitized template strings
- Deserializing untrusted data (pickle, unserialize or Java objects)
- Session cookies missing Secure, HttpOnly or SameSite
- CSV exports that run formulas when opened in a spreadsheet (CSV injection)
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 StorenTechVibe 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.