Skip to main content
Platform security guideReviewed

React security: tracing untrusted data through your components

Text a component renders is only one of the ways data reaches a React page.

Raw HTML, link addresses, style values, refs, third-party scripts, browser storage and build variables each take another route. This guide shows what happens at each one, according to React's documentation, MDN, OWASP, the build tools' documentation and a lab run on React 18.3.1, and gives you a worksheet to trace every flow in your own components.

React untrusted-data trace worksheet with sanitisation fixtures

A worksheet with one flow record for each place in your components where outside data reaches HTML, a URL, a style, a script, a ref, browser storage or a build variable, with what React's documentation, MDN, OWASP and the build tools' documentation say about each and what to record. A fixtures lab follows, with three files and outputs copied in from a real run on React 18.3.1.

react.txt

Introduction17 lines

REACT UNTRUSTED-DATA TRACE WORKSHEET AND SANITISATION FIXTURES ILLUSTRATION AND TEMPLATE. Part A is a blank worksheet to copy into a ticket or a spreadsheet: one flow record for each place in your components where data your team did not write reaches HTML, a URL, a style, a script, a DOM node through a ref, browser storage or a build variable. Part B is a fixtures lab that renders invented components with react-dom/server and prints what React wrote, with every output copied in from a real run. Part C says what the lab does not show. Every component, host name, input and token in Part B is invented. Part A only reads. The person filling it in is the application's owner, or a developer working for them, and the code and systems it covers are theirs. You read source files, build configuration and the HTML your own application serves, and you write down what you find. No step changes a setting, rotates or reveals a secret, or sends traffic to a service you do not own. Record the names of variables, storage keys and cookies, never their values, and never paste a real token into the worksheet, a ticket or the lab. HOW TO FILL IT - Use one copy per application. For a large application, use one copy per area, such as the customer portal or the staff screens. - Build the list of lines to trace first. Part B's list-flows.mjs prints the lines in your source that match the code names in section 0, or you can search your code for the same names. Markdown and rich-text renderers, tag manager snippets, libraries handed a DOM node and define entries need a search of their own. - Fill one flow record for each line. An answer you do not have is written as "unknown" and logged as a finding to resolve. - "React's documentation says", "React's reference for version 19.3 says" and "Source" name the page behind a statement, as read on 14 November 2026. Lines marked "This page's advice" or "This page's reading" are this guide's own suggestions; React, the build tools and the browser vendors did not write them. - Log every finding on its own line at the end, naming the person who can make the change as its owner. Application: react-dom version in the deployed build (from the lockfile of the deployed commit, not the range in package.json): Build tool and its version: Completed by (name and role): Date:

React security: what React handles and what your components decide

React is the layer that turns your components into the page a visitor's browser shows.

React's guide to refs puts the arrangement this way: React automatically updates the DOM to match your render output, so your components won't often need to manipulate it. React security comes down to where that arrangement holds and where your own code steps around it, which is why this guide works component by component rather than from a list of attacks.

Start with what holds. In the lab on this page, react-dom/server 18.3.1 wrote a string placed as a child into the HTML as text: the angle brackets and quotes of the HTML element inside it came out as entities. Raw HTML goes in through a prop with its own name, dangerouslySetInnerHTML.

React's reference says that building the {__html} object close to where the HTML is generated ensures that all raw HTML in your code is explicitly marked as such, and that only variables you expect to contain HTML are passed to it.

The doors are the places where a component hands React, or the browser, something other than text: an HTML string, an address for href or src, a CSS value, a script element, a DOM node reached through a ref, a value kept in browser storage, or a build variable compiled into the JavaScript.

Each one behaves differently, and for links the behaviour changed between React versions. The trace worksheet on this page records each kind of flow in your own components, with what React's documentation says about it or, for browser storage and build variables, what MDN, OWASP, Vite and Next.js say.

This guide covers client-side React. Next.js routing, server components and framework advisories have their own guide, whose advisory ledger covers React security vulnerabilities in React Server Components as they reached Next.js, and hosting on Vercel has another.

Cross-site scripting as a whole, with its encoding contexts and browser checks, sits in the XSS guide; this page deals with React XSS at the component level, where untrusted data travels inside React components.

dangerouslySetInnerHTML: where an HTML string enters a component

React's reference for version 19.3 describes dangerouslySetInnerHTML as taking an object of the form {__html} with a raw HTML string or a TrustedHTML value inside, and overriding the innerHTML property of the DOM node.

It says that if the HTML inside is not trusted, for example if it is based on user data, you risk introducing an XSS vulnerability, and to use the prop only with trusted and sanitised data. In the lab on react-dom/server 18.3.1, the same string that came out as text when passed as a child came out as an element in the HTML when passed this way.

The component-level question is where the {__html} object gets built. The reference says to create it as close as possible to where the HTML is generated, and that creating it inline in the JSX is not recommended. On this page's reading, an HTML string that travels down several components as an ordinary prop, under a name such as bodyHtml, has lost that marking by the time it reaches the sink, and a reviewer reading the last component cannot tell where it came from. The worksheet asks where each HTML string is produced and where its {__html} object is created.

Where HTML has to render, a sanitiser does the filtering, and the order of steps matters. DOMPurify's README warns that modifying HTML after sanitising it might void the effects of sanitisation, and asks that any library the sanitised markup is passed to does not change the HTML on its own.

It also says that on the server DOMPurify needs a DOM to be present, and recommends keeping jsdom up to date.

This page's advice is to sanitise last, after any Markdown or template step, at the point where the {__html} object is created, and to record the sanitiser and its version.

Markdown is one source of HTML strings in React apps. The react-markdown README describes that library as safe by default, with no dangerouslySetInnerHTML, and says it typically escapes HTML or ignores it with the skipHtml option.

It shows the rehype-raw plugin for a trusted environment where you trust the Markdown, and warns that overriding urlTransform with something insecure opens you up to XSS vectors and that the plugins and components you use may be insecure.

For each renderer, the worksheet records its version, any plugin that lets raw HTML through, any urlTransform override and any custom component that sets HTML itself.

One more HTML flow is easy to miss: an object from an API response rendered as a child. React's changelog entry for 0.14.0, dated 7 October 2015, says including untrusted objects as children previously could result in an XSS vulnerability, and that React tags elements with an ES2015 Symbol, in browsers that support it, so that it never considers untrusted JSON to be a valid element.

Its 18.0.0 entry, dated 29 March 2022, says React now depends on modern browser features including Symbol.

In the lab, react-dom/server 18.3.1 refused an object parsed from JSON in the shape of an element with an error. On this page's reading, the Symbol tag stops an object parsed from JSON passing as an element, in the browsers those entries describe, and it does not apply to code that builds elements from data, such as calling createElement with an element type taken from a response. That code is a flow to trace on its own.

Style props: what react-dom/server 18.3.1 wrote for a value carrying two declarations

React's reference says the style prop takes an object of CSS styles with camelCase property names, that values can be strings or numbers, and that React appends px to a number unless the property is unitless.

It recommends style only for dynamic styles whose values are not known ahead of time, and plain CSS classes through className otherwise.

The lab shows what react-dom/server 18.3.1 did with values from outside. A width value that carried a semicolon and a background-image declaration was written into the style attribute as given, so the attribute held two declarations. Quotes and angle brackets inside a value became entities, so the value stayed inside its attribute.

A string passed in place of the object threw an error saying the prop expects a mapping from style properties to values. On this page's reading of that output, react-dom/server 18.3.1 kept a style value inside its attribute but did not check that it was a single CSS value. The lab shows server output only; client rendering of the same value was not tested.

This page's advice is to convert a style value from data to the type it ought to be before it reaches the object: a number clamped to a range, as the lab's toPercent function does when it turns 250 into 100%, or one entry from a fixed list.

Keep property names in code, and never build a style object's keys from data. CSS contexts outside React are covered in the XSS guide.

Refs and effects: where code steps outside React

React's guide to refs explains that when React creates the DOM node for an element with a ref, it puts the node into the ref's current property.

It calls refs an escape hatch, for when you have to "step outside React", such as managing focus, scroll position, or calling browser APIs that React does not expose. It says modifying the DOM directly can risk conflicting with the changes React is making, and to avoid changing DOM nodes managed by React, while noting that doing so requires caution rather than being ruled out: a part of the DOM React has no reason to update, such as a div that is always empty in the JSX, can be changed safely.

That guidance is about keeping React working; the security point is this page's own. Once code holds a DOM node, a write to its innerHTML, an attribute or a src goes straight to the browser, and React's handling of children and props takes no part in it.

MDN notes that setting innerHTML parses the value as HTML. A ref is therefore a place where an HTML, URL or script flow can start again outside React, and each write through one is traced as its own sink.

Libraries count too. Trace a rich-text editor, map, chart or chat widget that receives a DOM node from your component, and an effect that creates a script element or saves to storage, as flows of their own. The lab's list-flows.mjs lists useRef calls, ref props, script elements and storage calls alongside the props it searches for, and the worksheet records which library receives each node and at what version.

Third-party scripts run with your page's privileges

MDN's Web Storage API overview explains that third-party code added to a site with a script element runs in the browsing context of the embedder, and that from the browser's perspective there is no difference between first-party and third-party code when a script tag is used.

OWASP's Third Party JavaScript Management Cheat Sheet puts it in terms of privileges: when the code executes, the third party is granted the exact same privileges that were granted to the user. It adds that there is usually no guarantee the code hosted by the third party will remain the same as the code developers and testers saw, and that the content a tag manager returns can be changed by staff at the host site through the tag manager's interface.

React's reference for the script component, written for version 19.3, lists integrity, a cryptographic hash of the script to verify its authenticity, and nonce, a cryptographic nonce to allow the resource under a strict Content Security Policy, among the props it supports. It says React can move a script with src and async={true} into the document head and de-duplicate scripts with the same src, and that it then ignores changes to props after the script has been rendered.

The React 19 release post presents that async script support as part of React 19.

In the lab, React 18.3.1 wrote each script element with exactly the attributes it was given, including a src taken from data, and its development build gave a javascript: src the same warning as an href.

MDN's Subresource Integrity page says the browser loads a resource only when its hash matches a value in the integrity attribute, and that a cross-origin request using it must include the crossorigin attribute. OWASP lists in-house script mirroring and Subresource Integrity among the typical defences, and notes that with Subresource Integrity in place you can sometimes end up with secure but not working third-party code when the vendor decides to update it.

This page's reading is that a fixed hash suits a script whose content you control or mirror; for a script the vendor changes on its own schedule, the worksheet records who owns the vendor relationship and who can change the tag manager.

Tokens in client-side code: build variables, start-up config and browser storage

Anything a build tool compiles into the JavaScript reaches every browser that loads it.

Vite's documentation says variables prefixed with VITE_ are exposed in client-side source code after bundling, that their values are bundled into the source at build time, and that they should not contain sensitive information such as API keys. Its shared options page gives VITE_ as the default for envPrefix, says envPrefix should not be set to an empty string because that would expose every environment variable, and says Vite throws an error when it detects one; it also notes that an unprefixed variable can be exposed through define.

Next.js has its own prefix, NEXT_PUBLIC_: its documentation says the values of variables with that prefix are inlined at build time into the JavaScript bundle delivered to the browser.

The Next.js guide covers that framework. For any other tool, the worksheet asks for the tool's own documentation page and the date you read it.

A server can hand tokens to the page without any build variable. The lab writes a start-up config object into a data attribute twice: the whole object put an invented admin token into the HTML, and a copy holding only the fields picked as public did not.

This page's advice is to build that object from a short list of named public fields, never by passing a server-side settings object through.

Browser storage is the third route. MDN's localStorage reference says it gives access to a Storage object for the document's origin, with data saved across browser sessions and no expiration time, and its Web Storage API overview says every document with the same origin has access to the same localStorage area.

OWASP's HTML5 Security Cheat Sheet says a single cross-site scripting flaw can be used to steal all the data in these objects, so sensitive information should not be stored in local storage. It adds that one such flaw can also be used to load malicious data into them, so their contents should not be considered trusted, and it says not to store session identifiers in local storage, because the data is always accessible by JavaScript.

MDN's Set-Cookie reference says the HttpOnly attribute forbids JavaScript from accessing a cookie, while the cookie is still sent with requests JavaScript starts, such as fetch().

The JWT guide covers where a token should live, and the cookie security guide the attributes of the cookie that carries it; this page's advice is to trace any value your components read back from storage as untrusted input.

Trusted Types and Content Security Policy in a React app

React's reference for version 19.3 says that a site enforcing Trusted Types should pass a TrustedHTML value created by its security policy as __html, that React passes the value to the browser without converting it to a string so the browser can validate it, and that the policy must still ensure any input used to create the value is trusted and sanitised.

React's changelog lists "Enable Trusted Types API integration" among the notable changes in version 19.3.0, dated 9 September 2026. On MDN's Trusted Types page, the API does not itself supply a policy or any transformation functions: the developer defines their own.

When that MDN page was read on 14 November 2026, it labelled the API Baseline 2026, newly available, and said it might not work in older devices or browsers.

A Content Security Policy works at page level. MDN's CSP guide says setting a policy is not an alternative to sanitising input, and describes nonce- or hash-based fetch directives, which it calls a strict CSP, as the recommended practice for controlling script loading against XSS.

React's 19.3 reference for the script component lists a nonce prop for that case. This page treats both as backstops: the worksheet notes them in its last section, after the flows that need fixing in code.

React security checklist for your developers

These React security best practices are this page's own advice, drawn from the documentation cited above.

They are the questions the worksheet asks, turned into habits.

  • Render values from outside as children or ordinary props. Use dangerouslySetInnerHTML only where HTML must render, and build its {__html} object where that HTML is sanitised.
  • Sanitise HTML last, after any Markdown or template step, and change nothing afterwards. Record the sanitiser and its version.
  • Pass every href, src, action and formAction that comes from data through a URL-parser allow-list, whatever the React version.
  • Convert style values from data to numbers or fixed choices before they reach the style object, and keep property names in code.
  • Treat each write through a ref, and each DOM node handed to a library, as a sink to trace.
  • Keep a register of third-party scripts with an owner, the source of each src, integrity, nonce and who can change the tag manager.
  • Keep session identifiers out of localStorage and sessionStorage where you have the choice, and trace values read back from storage as untrusted.
  • Give the browser prefix only to values whose provider designed them for browsers, and record envPrefix and every define entry.
  • Record the react-dom version in the deployed build, and read React's changelog before relying on behaviour that changed between versions.

How to use the trace worksheet and the fixtures

The worksheet is built around one record per flow, so that a reviewer can see where each value enters, who can write it, which React API it reaches and what control sits in between.

Fill it from your source code and from the HTML your application serves. Every step reads and records; a change that a finding calls for is made later, as its own planned change.

  • Run list-flows.mjs from the lab against your source folder, or search for the names in section 0. Each line it prints is a place to look, and a line it misses is not cleared.
  • Fill one flow record for each line, copying the line from sections 1 to 7 that gives what the documentation says about that API.
  • Run the fixtures in a copy of your own project to see what your react-dom version prints. They are illustrations rather than maintained tools.
  • For third-party scripts, save the HTML of each kind of page your application serves and list its script tags, as step 6 of the lab does.
  • Record names of variables, storage keys and cookies only. A value never goes into the worksheet, and a real token never goes into the lab.
  • Give every finding an owner who can make the change, and revisit the worksheet when react-dom changes version or a new third-party script arrives.

What a review of your own components cannot show

A completed worksheet records the flows you found, at the React version and on the day you recorded.

It cannot show what a browser does with your pages at run time, what code a third-party script will serve tomorrow, or a flow the search did not find, such as a value passed through spread props or written to the DOM by a library. The lab adds server-rendered strings on React 18.3.1 only: no browser, no client-side rendering and no React 19.

It also says nothing about the server. React decides what a page shows; whether the API behind it returns another customer's data, or accepts a change it should refuse, is decided by server code, which a review of components does not read. That question belongs to a test of the running application and its API.

Where a Clever Ops review fits

This guide is informational, and nothing on it describes a test of React itself.

The worksheet and the fixtures are for your own team to keep and rerun.

Active testing of your own React application sits within a scoped web application penetration test. Clever Ops tests custom web applications and their APIs, such as applications built on Next.js, React or Node, in a signed-in test that covers what each role can see and change, and read-only code access to named areas can be added to confirm how a control is enforced.

A secure code review reads the source with read-only access to your repository, under a written scope that names what is read.

The website security check reads the page source and JavaScript a visitor receives for keys and secrets (WSTG v4.2, WSTG-INFO-05), and reviews a site's Content Security Policy against the OWASP Content Security Policy Cheat Sheet.

Where Clever Ops built or hosts the React application, the same work is called a security review, not an independent test, and the report names who built it and who hosts it.

Frequently asked questions

It depends on the version. React's changelog lists the deprecation of javascript: URLs in 16.9.0, and for 19.0.0 lists that they are replaced with functions that throw errors. In this page's lab, react-dom/server 18.3.1 printed a warning in its development build and still wrote the link into the HTML, and its production build wrote it with no warning. OWASP's XSS Prevention Cheat Sheet says React 19's blocking does not replace application-specific URL validation. This page's advice is to check the scheme in your own code on every version.

React's reference says to use it only with trusted and sanitised data, and to create the {__html} object as close as possible to where the HTML is generated; it does not recommend creating the object inline. DOMPurify's README warns that modifying HTML after sanitising it might void the effects of sanitisation, and says that on the server DOMPurify needs a DOM to be present, recommending that jsdom be kept up to date. Record where each HTML string comes from and which sanitiser and version handle it.

No. Vite's guide to env variables says variables prefixed with VITE_ are exposed in client-side source code after bundling, that their values are bundled in at build time, and that they should not contain sensitive information such as API keys. Vite's shared options page says envPrefix defaults to VITE_, and that Vite throws an error if envPrefix is set to an empty string, which would expose every variable.

Local storage is where OWASP's HTML5 Security Cheat Sheet advises against keeping session identifiers: it says data there is always accessible by JavaScript, and that a single cross-site scripting flaw can be used to steal all of it. MDN describes localStorage data as kept across browser sessions with no expiration time. For cookies, MDN's Set-Cookie reference says HttpOnly forbids JavaScript from accessing the cookie, which still goes out with requests JavaScript starts, such as fetch(). The JWT guide compares the options.

A ref hands your code the DOM node React created, and React's documentation calls refs an escape hatch for when you have to "step outside React". On this page's reading, writes made through it, such as setting innerHTML, go to the browser directly. MDN notes that setting innerHTML parses the value as HTML. Trace each write through a ref as its own sink.

React's reference for version 19.3 says that a site enforcing Trusted Types should pass a TrustedHTML value created by its security policy as __html, that React passes it to the browser without converting it to a string so the browser can validate it, and that the policy must still make sure any input used to create it is trusted and sanitised. React's changelog lists "Enable Trusted Types API integration" among the notable changes in 19.3.0, dated 9 September 2026. On 14 November 2026, MDN labelled the Trusted Types API Baseline 2026, newly available, and said it might not work in older devices or browsers.

Scope the right test for your application

Tell us what you have built, who uses it and when it needs to launch. You will get a plain-English recommendation: a website audit, a web application test or a referral to a specialist.