Skip to content

Lesson 12 of 14 · 18m

React Security: XSS and the Supply Chain

JSX escapes children by default — dangerouslySetInnerHTML, javascript: hrefs, and npm dependencies are the three real holes.

React does one genuinely important security thing for free: {userText} in JSX is rendered as a text node, never parsed as HTML. The XSS that would fire in server-rendered HTML or innerHTML assignment is inert by default. That single guarantee creates the dangerous assumption — "React escapes everything" — and it has exactly three holes: the explicit escape hatch, URL attributes, and the supply chain your bundle is built from.

The named escape hatch

dangerouslySetInnerHTML={{__html: content}} is named that way so the code review writes itself. The moment you render user HTML — a markdown-rendered comment, a CMS bio — you're back in raw-DOM XSS territory, and the fix is sanitization before the sink, not hope:

import DOMPurify from "dompurify";
// sanitize at the sink — the payload is inert text until it hits the DOM
<div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(rendered) }} />

Keep DOMPurify updated — mutation-XSS (mXSS) bypasses against it are a recurring research class, patched across its versions; pinning an old copy is worse than knowing you need it. If the content doesn't legitimately need HTML, render markdown to it server-side or don't render markup at all — the safest dangerouslySetInnerHTML is the one that isn't there.

javascript: doesn't need dangerouslySetInnerHTML

<a href={user.website}>their site</a>

If user.website is javascript:alert(document.cookie), React renders it and the click executes it — React warned in dev builds since 16.9 (August 2019) that javascript: URLs in props are a security surface, but a warning is not a block. The defense is validating the URL scheme at input time (allow https:/mailto:, reject the rest) or rendering a <button> that navigate()s — never trusting a string into an href. The same arithmetic applies to target="_blank": modern browsers imply rel="noopener" now, so the old window.opener hijack is mostly historical — but rel="noopener noreferrer" costs six characters and kills the question entirely. And javascript: isn't the only scheme worth rejecting — data:text/html, URLs in an href are a different failure shape. Top-level data: navigation doesn't run in your origin at all: it lands on an opaque origin, which is precisely why javascript: is the scheme that executes as you — but data: still hands the click a complete attacker-authored HTML document, enough of a phishing surface that browsers now block that navigation outright. Whitelist schemes at the input boundary; don't pattern-match for the ones you remembered to fear.

The hole React can't see: your dependencies

The biggest real-world frontend compromises don't exploit React — they ride in through npm. event-stream (2018) shipped a bitcoin-stealing payload through a transitive dep nobody audited; ua-parser-js was hijacked in October 2021; the polyfill.io CDN was sold to Funnull in February 2024 and caught serving malware that June to the ~384,000 sites loading it directly; in October 2024 @lottiefiles/lottie-player shipped a crypto-drainer across three patch versions — 2.0.5, 2.0.6, 2.0.7. A package-lock.json diff that nobody reviews, a ^ that happily installed 2.0.5, a CDN script tag without integrity — React is innocent of all of it and it owns none of the fix. The practice: review lockfile changes in PRs, pin CDN assets with SRI hashes or self-host them, run npm audit in CI and actually read it, and treat "it was in a dependency" as the answer it increasingly is.

Field notes

  • CSP is the second layer for everything above: a strict script-src turns a stored XSS from an exploit into a console error.
  • <script src="https://cdn.example/x.js" integrity="sha384-…" crossorigin> — SRI is a one-line attribute that makes a hijacked CDN harmless.
  • Don't build your own sanitizer. mXSS has defeated better-funded attempts than yours.
  • rel="noreferrer" also hides the Referer — relevant for outbound links where the destination shouldn't learn the source URL.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.