Short answer
React escapes text by default, so XSS in React apps almost always goes through dangerouslySetInnerHTML. When the HTML comes from users (comments, profiles, Markdown rendered to HTML, AI output), anyone can put markup there that runs JavaScript in another user’s session. Render Markdown with react-markdown instead, or sanitize with DOMPurify right before the HTML reaches the DOM, and keep raw HTML only for content you wrote yourself.
Ask an AI about this page:ChatGPTClaude
On this page
XSS through dangerouslySetInnerHTML happens when a React component puts HTML into the page that someone else wrote, such as a comment, a profile bio, Markdown converted to HTML or a chatbot answer. Markup like an image with an onerror handler then runs JavaScript in the browser of everyone who views it, with their session. It is rated high because one stored comment can act as every user who opens the page: read what they can see, send requests as them, or grab the session stored in their browser.
Why it matters
React escapes everything you render with {value}, which is why most React apps don’t have XSS. dangerouslySetInnerHTML is the deliberate way around that protection. React’s own docs say it “should be used with extreme caution” and spell out the rule: only use it with trusted and sanitized data. OWASP’s XSS Cheat Sheet lists React’s dangerouslySetInnerHTML without sanitizing as one of the typical ways frameworks still end up with XSS.
AI-built apps use it most often to render Markdown:
import { marked } from "marked";
export function Comment({ body }: { body: string }) {
// body was written by another user: any <img onerror> or <script>-like markup runs here.
return <div className="prose" dangerouslySetInnerHTML={{ __html: marked.parse(body, { async: false }) }} />;
}Markdown allows raw HTML, so whatever a user types ends up in the page as live markup. The same pattern appears for rich text editors, email previews, AI chat answers and “description” fields loaded from the database.
In a Supabase app, the script runs on your domain with the signed-in user’s session, so it can call your API with their rights. Row level security limits it to what that user may see and do, which for an admin is a lot.
How to find it in your app
Search for raw HTML sinks:
grep -rn "dangerouslySetInnerHTML\|innerHTML\s*=\|insertAdjacentHTML\|outerHTML\s*=" src
grep -rn "rehype-raw\|marked\|markdown-it\|showdown" src package.jsonFor every hit, follow the value back to where it comes from:
- Constants or your own config (for example the
<style>block in shadcn/ui’schart.tsx): fine. - Anything from the database, a URL, an API, a file or an AI model: the defect, unless it goes through a sanitizer right before rendering.
Also check react-markdown usages: it is safe by default, but adding the rehype-raw plugin turns raw HTML back on.
How to fix it
For Markdown, don’t produce HTML at all. react-markdown turns Markdown into React elements, and its README describes it as secure by default: raw HTML in the input is not rendered.
import Markdown from "react-markdown";
export function Comment({ body }: { body: string }) {
// react-markdown builds React elements and does not render raw HTML by default.
return (
<div className="prose">
<Markdown>{body}</Markdown>
</div>
);
}Leave out rehype-raw for anything users or models write. If you do need some HTML, the react-markdown README recommends adding rehype-sanitize.
If you must render HTML, sanitize it with DOMPurify right before it goes into the page, as OWASP recommends:
import DOMPurify from "dompurify";
export function RichText({ html }: { html: string }) {
// Only when you really must render HTML: sanitize right before it reaches the DOM.
const clean = DOMPurify.sanitize(html);
return <div className="prose" dangerouslySetInnerHTML={{ __html: clean }} />;
}Two rules from OWASP apply: don’t change the string after sanitizing it, and keep DOMPurify up to date, because browsers change and bypasses are found.
Links need their own check. React 19 blocks javascript: URLs in href and src, but OWASP notes that doesn’t replace validating URLs from users. Add a Content Security Policy as a second layer.
Find every use of dangerouslySetInnerHTML, innerHTML, outerHTML and insertAdjacentHTML in the frontend. For each, trace where the HTML comes from. Leave uses with constant or developer-written content unchanged and list them. For anything that can contain user, database, URL, API or AI model content: if it is Markdown, replace the HTML rendering with react-markdown and no rehype-raw plugin; otherwise sanitize with DOMPurify.sanitize directly before rendering and do not modify the string afterwards.
Add dompurify (or react-markdown) as a dependency if needed. Keep the visible formatting the same for normal content. Afterwards, list every component you changed and where its data comes from.
Check that the fix works
- On your own app, signed in with a test account, save this as a comment, bio or other field that used to be rendered as HTML:
<img src=x onerror=alert(document.domain)> - Open the page where it is shown. No alert may appear; you should see either the text itself or nothing.
- Check that normal formatting still works: bold, lists and links in Markdown render as before.
- Run the search above again. Every remaining hit must be constant content or wrapped in
DOMPurify.sanitize.
Frequently asked questions
Is every dangerouslySetInnerHTML a vulnerability?
No. It is only a problem when the HTML can contain something a user or an outside source controls. The shadcn/ui chart component, for example, uses it to inject a style block built from your own config. Follow the value back to its source before you change anything.
Is AI output safe to render as HTML?
Treat it like user input. A model can repeat text from a user prompt, a web page or a document, including markup. Render it as Markdown with react-markdown, without raw HTML.
Doesn’t a Content Security Policy solve this?
A strict CSP makes many XSS payloads fail and is worth adding, but it is a second layer. The fix is to stop putting untrusted HTML into the page.
Do I need to sanitize on the server too?
Sanitize at the point where HTML enters the DOM, because that is the only place that sees the final string. Cleaning on save is fine as an extra step, but data can reach the page in other ways, such as old rows or imports.
Sources
- 1.Common components (e.g. <div>): dangerouslySetInnerHTML — React Docs
- 2.Cross Site Scripting Prevention Cheat Sheet — OWASP Cheat Sheet Series
- 3.react-markdown — remarkjs on GitHub
- 4.DOMPurify — cure53 on GitHub
- 5.CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') — MITRE