Short answer
Cross-site scripting (XSS) is when an app puts untrusted input into a page in a way that the browser runs it as code. The injected script runs with your app’s rights: it can read what the user sees, use their session and act on their behalf. React escapes text by default, so in AI-built apps XSS usually comes through dangerouslySetInnerHTML, markdown rendered as HTML or user-supplied links.
Ask an AI about this page:ChatGPTClaude
On this page
Cross-site scripting (XSS) is a weakness where an app includes untrusted input in a page in a way that makes the browser run it as code. MDN explains why that matters: the injected script runs with the same privileges as your own code, so it can read and change everything on the page, including data in local storage, and send requests as the signed-in user. MITRE files it as CWE-79 (opens in a new tab).
XSS in an AI-built app
React escapes anything you put between JSX tags, so <p>{comment.body}</p> is safe even if the comment contains HTML. The risk comes from the escape hatches. A common one in generated code is rendering rich text or markdown:
<div dangerouslySetInnerHTML={{ __html: comment.body }} />If comment.body comes from another user, it can contain an element with an event handler, and that handler runs in every visitor’s browser. React’s own docs warn that with untrusted HTML “it is trivial to introduce an XSS vulnerability this way”. Two other patterns to look for:
- Markdown or rich text rendered as raw HTML, for example a markdown library with HTML passthrough turned on.
- User-supplied links in
href, such as a profile website field. Ajavascript:URL in a link is code, not a destination. OWASP notes that React 19 blocks such URLs but says this does not replace checking URLs in your own app.
In a Supabase app this matters more than usual: supabase-js keeps the session in the browser’s local storage when persistSession is on, and a script running on your page can read it.
How to check your app
- Search your code for
dangerouslySetInnerHTML,innerHTML,eval(and markdown components with HTML enabled. For each hit, ask where the value comes from. Your own constants are fine; anything a user typed is not. - Check every
hrefandsrcthat comes from user data. - On your own app, save a harmless test value such as
<b>bold?</b>in a comment or profile field. If it shows up bold instead of as plain text, that spot renders HTML and needs a closer look.
How to fix or prevent it
Render user content as text wherever you can. Where you really need HTML, sanitize it with a maintained library such as DOMPurify, which both OWASP and MDN name, and allow only http: and https: links. Add a Content Security Policy as a second layer. The step-by-step fix for React, including a fix prompt for your AI builder, is in XSS via dangerouslySetInnerHTML; headers for Vite apps are in Security headers for Vite and Lovable apps.
Frequently asked questions
Does React protect me from XSS?
Mostly. React escapes values you put into JSX as text. It does not protect you when you use dangerouslySetInnerHTML with untrusted HTML, render markdown as raw HTML, or put a user-supplied URL into an href without checking its scheme.
What are stored, reflected and DOM-based XSS?
Stored XSS is saved, for example in a comment, and shown to other users later. Reflected XSS comes from the current request, often a crafted link. DOM-based XSS happens entirely in the browser, when your own script writes data into the page unsafely.
Is a Content Security Policy enough to stop XSS?
No. MDN and OWASP both describe CSP as an extra layer. Escaping and sanitizing come first; CSP limits the damage when they fail.
Sources
- 1.Cross Site Scripting Prevention Cheat Sheet — OWASP Cheat Sheet Series
- 2.CWE-79: Improper Neutralization of Input During Web Page Generation — MITRE
- 3.Cross-site scripting (XSS) — MDN Web Docs
- 4.Common components: dangerouslySetInnerHTML — React Docs