Reflected XSS via dangerouslySetInnerHTML: When the API Is Safe but the Frontend Isn’t
A search results page rendered the user-supplied query through React's dangerouslySetInnerHTML, so a crafted query string was inserted into the page as raw HTML. An attacker could send a victim a link that, when opened, ran JavaScript in the application's origin.
Published on medium.com
Reflected Xss
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
This write-up looks at a search feature on a web application. The feature takes a query string, sends it to a backend endpoint, and renders the results on a page. The endpoint in question is /api/feedback, and the query parameter is q. The report does not name the application or the program; it was published as a standalone write-up by Neel Chauhan on 18 September 2026.
The interesting part of this target is that the search feature has two distinct rendering stages: a JSON API response and a React frontend that consumes it. The vulnerability sits entirely in the second stage.
The weakness is classified as CWE-79 (Improper Neutralization of Input During Web Page Generation), with a CVSS 3.1 score of 6.1 (Medium) and the vector AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N.
Setup
No accounts, roles or special tooling are described. The work was done by inspecting the API response directly and then reading the frontend's compiled JavaScript bundle. Exploitation requires a victim who is authenticated and who clicks a crafted link.
Finding it
The researcher started with the API rather than the browser, on the reasoning that a search feature's request/response cycle separates two questions: whether the backend encodes data properly, and whether the frontend renders it safely. Treating those as one failure point leads to misdiagnosing the fix.
The first request was a plain search:
GET /api/feedback?q=test HTTP/2
The response came back as:
HTTP/2 200 OKContent-Type: application/json
with the body:
{ "ok": true, "search_query": "test", "results": []}
The key detail is Content-Type: application/json. The API returns a properly typed JSON response, and JSON string encoding on its own already neutralises anything that would be dangerous in an HTML context. Had this been a server-side issue, the expectation would have been raw HTML in the Content-Type header instead. That told the researcher, before writing a single payload, that any vulnerability here was almost certainly a client-side DOM sink issue rather than a backend encoding failure.
To find the sink, the researcher pulled apart the frontend's compiled JavaScript to see exactly how search_query gets rendered. The line that mattered was:
dangerouslySetInnerHTML: { __html: "Results for: ".concat(p)}
dangerouslySetInnerHTML is React's explicit opt-out of its default automatic HTML escaping. React named it that deliberately, as a warning label. Any value passed through it is inserted into the DOM as raw HTML, tags, attributes and all, executed exactly as if it were part of the page's original markup.
Right next to it in the same bundle, a different piece of feedback-message rendering used ordinary JSX interpolation:
<p>{e.message}</p>
That side-by-side comparison matters: it shows the vulnerability is not a framework limitation, because React's default behaviour is already safe. It is a single deliberate override of that default, in one specific component.
The data flow the researcher traced is:
/api/feedback?q=<payload> → JSON field "search_query" → React state variable `p` → dangerouslySetInnerHTML → raw DOM insertion → script execution
Five hops, and the escaping that should happen at the last step never runs.
Exploitation
Step 1: Confirm the API reflects the payload as JSON
The researcher sent the payload through the same endpoint:
GET /api/feedback?q=<img src=x onerror=alert(document.domain)> HTTP/2
The response was:
HTTP/2 200 OKContent-Type: application/json
with the body:
{ "ok": true, "search_query": "<img src=x onerror=alert(document.domain)>", "results": []}
The API faithfully returns the payload as an encoded JSON string value, exactly as it should. This confirms again that the backend is doing its job correctly. The vulnerability only manifests once the frontend takes that string and decides to render it as raw HTML instead of as text.
Step 2: Load the results page in a browser
Loading the results page in an actual browser with this query fired the payload. The broken image tag triggers its onerror handler, which executes the injected JavaScript. alert(document.domain) is the standard, low-impact way to prove execution without doing anything invasive: if it fires, code execution in the page's own origin is proven, with no ambiguity.
Why a reflected GET payload
Because the payload lives entirely in a URL query parameter and is not stored anywhere server-side, this is reflected rather than stored XSS. That has a specific implication for weaponisation: an attacker needs to deliver a crafted link to the victim (a phishing email, a message, a malicious ad, a shortened URL), and the victim needs to click it while authenticated. It requires user interaction, which is reflected in the CVSS vector's UI:R, but a single click on a link that appears to point to a legitimate feature on a trusted domain is a low bar in practice.
Root cause
The mistake is a single deliberate override of React's default escaping. The search_query value from the JSON response is placed into a React state variable p, and that variable is concatenated into the __html property of dangerouslySetInnerHTML. React's default JSX interpolation, as used for the feedback message in the same bundle, would have escaped the value; the explicit dangerouslySetInnerHTML bypasses that escaping and inserts the string as raw HTML.
The backend defence did work: the API returned application/json and encoded the payload as a JSON string value. That defence is simply irrelevant once the frontend chooses to treat the decoded string as HTML. The report does not describe any Content Security Policy or other client-side control that would have stopped the handler from running.
Impact
Once arbitrary JavaScript executes inside an authenticated session on the application's own origin, a lot becomes reachable:
- Reading
document.cookieto exfiltrate the session token. In this case the session cookie was correctly flaggedHttpOnly, which blocks that specific vector; the researcher calls this out explicitly as a partial mitigation rather than ignoring it. - Making authenticated requests silently on the victim's behalf, using their live session, meaning this could be chained to trigger other state-changing actions the victim never intended.
- Injecting fake UI elements, such as a convincing credential-harvesting overlay, directly into a page the victim already trusts because it genuinely is that page.
Exploitation requires the victim to click a crafted link while authenticated. The CVSS vector reflects the required user interaction with UI:R.
Fix
The implied fix follows from the root cause: render search_query through ordinary JSX interpolation, as the feedback message already does, so React escapes it, and remove the dangerouslySetInnerHTML use for this value. Where raw HTML is genuinely required, it would need to be sanitised before being passed to __html, but the report does not discuss that option.
Variants to test
- Other fields feeding the same sink. Look for any other JSON field that ends up in a
dangerouslySetInnerHTML__htmlvalue. A positive result is the same broken-image or similar handler firing when the field is set through its normal request. - Other components in the same bundle. Search the compiled JavaScript for every
dangerouslySetInnerHTMLoccurrence and trace each one back to its data source. A positive result is any occurrence whose input is attacker-controllable. - Stored variants of the same flow. If a similar value is persisted server-side (for example a saved search or a profile field) and later rendered through the same sink, the XSS becomes stored. A positive result is the payload firing on a page load without the payload being present in the URL.
- Cookie exposure. Check whether the session cookie carries
HttpOnly. A positive result for an attacker is a session cookie withoutHttpOnly, which turns the XSS into direct session theft. - Chaining to state-changing actions. With the XSS firing, attempt an authenticated request from the injected script. A positive result is a state change performed with the victim's session and no additional interaction.
What happened, in plain words
A website had a search box. You typed something into it, the site sent your words to its server, and the server sent back a list of results. The page then showed those results, including a line like "Results for: your words".
That last part, showing your words back on the page, is where the problem was. The page was built with a tool called React, which by default treats anything you type as plain text, so it can never become code. But in one specific place, the developers told React to stop doing that and to treat the text as real page markup instead. That single decision meant a visitor could put a small piece of code into the search box, and the page would run it as if the site's own developers had written it.
This kind of bug is called cross-site scripting, usually shortened to XSS. The researcher who found it, Neel Chauhan, published the write-up on 18 September 2026. The report does not name the application or the bug bounty program.
The weakness is classified as CWE-79 (Improper Neutralization of Input During Web Page Generation), with a CVSS 3.1 score of 6.1 (Medium) and the vector AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N. We will unpack those labels later; for now, think of CVSS as a standard scorecard for how serious a security bug is, and CWE as a standard catalogue of bug types.
The pieces involved
Before the steps, a few words that come up constantly.
- API: the part of a website that answers requests with data rather than with a web page. Think of a restaurant: the page you see is the dining room, the API is the kitchen, and your request is the order. You do not go into the kitchen; you send an order and get a plate back.
- Endpoint: one specific address inside that API, like one dish on the menu. Here the endpoint is
/api/feedback, and the search term travels in a parameter calledq. - Parameter: the part of a web address after the question mark. In
?q=test, the parameter name isqand its value istest. - JSON: a simple text format for sending structured data, made of names and values in curly braces. It is how the kitchen hands you the plate in a labelled, predictable shape.
- React: a popular toolkit for building web pages. It lets developers write small pieces called components, and it escapes text by default so that user input cannot turn into code.
- DOM: the browser's live model of the page, the tree of elements you can see and interact with. When something is inserted into the DOM as raw HTML, the browser treats it as real page structure, not as text.
- Cookie: a small piece of data the site stores in your browser so it can recognise you on the next request. A session cookie is the one that proves you are logged in, a bit like a wristband at an event.
- Token: a value used in place of a password to prove identity. A session token is what the cookie usually carries.
- XSS (cross-site scripting): a bug where an attacker gets their own code to run inside someone else's web page, in that page's own context, so the browser trusts it as if the site had written it.
- Reflected vs stored: reflected means the malicious input bounces straight back from the request and is not saved anywhere. Stored means it is saved on the server and shown to later visitors. Reflected needs the victim to click a crafted link; stored waits for victims to visit.
Why the researcher checked the API response before touching the browser
A search feature has two separate stages: the server answering with data, and the page drawing that data. Those are two different places where things can go wrong, and mixing them up leads to fixing the wrong one. So the researcher started with the server, not the browser.
The first request was an ordinary search for the word test:
GET /api/feedback?q=test HTTP/2
Line by line:
GETis the type of request that asks for something without changing anything on the server, like asking to see a menu rather than ordering./api/feedbackis the endpoint being asked.?q=testis the search term being sent, withqas the parameter name.HTTP/2is the version of the language the browser and server use to talk.
The server answered:
HTTP/2 200 OKContent-Type: application/json
200 OKmeans the request succeeded.Content-Type: application/jsonis the server saying what kind of data it is sending back. This is the important line, and we will come back to it.
And the body of the answer was:
{ "ok": true, "search_query": "test", "results": []}
"ok": truesays the request was handled."search_query": "test"echoes back the search term that was sent."results": []is an empty list, because nothing matched.
Why does Content-Type: application/json matter so much? Because JSON is a data format, not a page format. When a value is placed inside a JSON string, characters that would be dangerous in a web page are encoded so they stay as text. If the server had instead answered with Content-Type: text/html, the browser would treat the answer as a web page, and raw markup in the answer could be dangerous. Seeing application/json told the researcher, before writing a single attack string, that the server was doing its job. If a bug existed, it was almost certainly on the page side, in the code that draws the results, not in the server's handling of the data.
Finding the actual sink in the JavaScript bundle
A sink is the place in code where data finally gets used in a way that can be dangerous, like the drain at the bottom of a pipe. To find it, the researcher read the frontend's compiled JavaScript, the code the browser actually downloads and runs. The line that mattered was:
dangerouslySetInnerHTML: { __html: "Results for: ".concat(p)}
Line by line:
dangerouslySetInnerHTMLis a React feature whose name is a deliberate warning. React normally escapes text so it can never become code. This feature is the explicit opt-out: it tells React to insert the value as raw HTML, tags and attributes included, exactly as if the site's own developers had written it into the page.__htmlis the property that holds the raw markup to insert."Results for: ".concat(p)builds the string by gluing the fixed textResults for:onto the variablep. Whatever is inpends up inside the raw HTML.
React named the feature that way on purpose, as a label on a dangerous tool. Any value passed through it is inserted into the DOM as real page structure, and any code inside it runs.
Right next to it in the same bundle, a different piece of the page, a feedback message, used ordinary React interpolation:
<p>{e.message}</p>
Line by line:
<p>is a paragraph element.{e.message}inserts the value ofe.messageas text. React escapes it, so if the value contained markup, the browser would show the characters rather than treat them as structure.
That side-by-side comparison is worth keeping in any write-up, because it shows the bug is not a limitation of React. React's default behaviour is already safe. This is one deliberate override of that default, in one specific component.
The data flow
The researcher traced the path the data takes:
/api/feedback?q=<payload> → JSON field "search_query" → React state variable `p` → dangerouslySetInnerHTML → raw DOM insertion → script execution
Read it left to right as five hops:
- The search term travels in the URL to the API.
- The server puts it into the JSON field
search_query. - The page stores that value in a React state variable called
p. A state variable is just a named box the page keeps a value in while it is running. - The value is passed into
dangerouslySetInnerHTML. - The browser inserts it into the DOM as raw HTML, and any script inside it runs.
Five hops, and the escaping that should happen at the last step never runs.
Building and firing the proof of concept
A proof of concept, or PoC, is a small, safe demonstration that a bug is real. The researcher sent this request:
GET /api/feedback?q=<img src=x onerror=alert(document.domain)> HTTP/2
Line by line:
GET /api/feedback?q=is the same request as before.<img src=x onerror=alert(document.domain)>is the payload, the attack string. It is an image tag whose source isx, a file that does not exist, so the image fails to load. Theonerrorpart is a handler that runs when loading fails. The handler callsalert(document.domain), which pops up a browser dialog showing the current site's domain name.
The server answered:
HTTP/2 200 OKContent-Type: application/json
with the body:
{ "ok": true, "search_query": "<img src=x onerror=alert(document.domain)>", "results": []}
Line by line:
"ok": trueagain says the request was handled."search_query"now contains the payload, returned as a JSON string value. The server did exactly what it should: it treated the payload as data and encoded it as a string."results": []is still an empty list.
This confirms again that the server is doing its job correctly. The bug only appears when the page takes that string and decides to render it as raw HTML instead of as text.
Loading the results page in a real browser with this query fired the payload. The broken image tag triggered its onerror handler, which ran the injected JavaScript. alert(document.domain) is the standard, low-impact way to prove code execution without doing anything invasive. If the dialog appears, code has run in the page's own origin, which means in the site's own security context, with no ambiguity.
Why a reflected GET payload, and what that means for exploitability
Because the payload lives entirely in a URL parameter and is not saved anywhere on the server, this is reflected XSS rather than stored XSS. That has a specific implication for how it gets used in practice. An attacker has to deliver a crafted link to the victim, through a phishing email, a message, a malicious ad or a shortened URL, and the victim has to click it while logged in.
It requires user interaction, which is what the UI:R in the CVSS vector records. But a single click on a link that appears to point to a legitimate feature on a trusted domain is a low bar in practice.
Why this is worse than it looks in isolation
Once arbitrary JavaScript runs inside a logged-in session on the site's own origin, a lot becomes reachable:
- Reading
document.cookieto steal the session token. In this case the session cookie was correctly flaggedHttpOnly, which blocks that specific route.HttpOnlyis a setting that tells the browser to keep the cookie away from JavaScript. The researcher calls this out explicitly as a partial mitigation rather than ignoring it. - Making authenticated requests silently on the victim's behalf, using their live session. This could be chained to trigger other actions the victim never intended.
- Injecting fake interface elements, such as a convincing login overlay that harvests credentials, directly into a page the victim already trusts, because it genuinely is that page.
Root cause
The mistake is a single deliberate override of React's default escaping. The search_query value from the JSON response is placed into the React state variable p, and that variable is concatenated into the __html property of dangerouslySetInnerHTML. React's default interpolation, as used for the feedback message in the same bundle, would have escaped the value. The explicit dangerouslySetInnerHTML bypasses that escaping and inserts the string as raw HTML.
The server-side defence did work: the API returned application/json and encoded the payload as a JSON string value. That defence is simply irrelevant once the page chooses to treat the decoded string as HTML. The report does not describe any Content Security Policy or other browser-side control that would have stopped the handler from running.
Impact
Exploitation requires the victim to click a crafted link while logged in. The CVSS vector reflects the required user interaction with UI:R.
Fix
The implied fix follows from the root cause: render search_query through ordinary React interpolation, as the feedback message already does, so React escapes it, and remove the dangerouslySetInnerHTML use for this value. Where raw HTML is genuinely required, it would need to be sanitised before being passed to __html, but the report does not discuss that option.
Variants to test
- Other fields feeding the same sink. Look for any other JSON field that ends up in a
dangerouslySetInnerHTML__htmlvalue. A positive result is the same broken-image or similar handler firing when the field is set through its normal request. - Other components in the same bundle. Search the compiled JavaScript for every
dangerouslySetInnerHTMLoccurrence and trace each one back to its data source. A positive result is any occurrence whose input an attacker can control. - Stored variants of the same flow. If a similar value is saved on the server, for example a saved search or a profile field, and later rendered through the same sink, the XSS becomes stored. A positive result is the payload firing on a page load without the payload being present in the URL.
- Cookie exposure. Check whether the session cookie carries
HttpOnly. A positive result for an attacker is a session cookie withoutHttpOnly, which turns the XSS into direct session theft. - Chaining to state-changing actions. With the XSS firing, attempt an authenticated request from the injected script. A positive result is a state change performed with the victim's session and no additional interaction.
Glossary
- Endpoint: one specific address inside an API, like one dish on a menu.
- Parameter: the part of a web address after the question mark, carrying a named value.
- JSON: a text format for structured data, made of names and values in curly braces.
- Content-Type: a header in which the server says what kind of data it is sending back.
- Header: a line of metadata sent alongside a request or response.
- React: a toolkit for building web pages from small pieces called components.
- Component: one self-contained piece of a React page.
- JSX: the syntax React uses to describe page elements, with values inserted in curly braces.
- Escaping: converting characters that would be treated as code into plain text, so they are shown rather than run.
- Sink: the place in code where data is finally used in a way that can be dangerous.
dangerouslySetInnerHTML: a React feature that inserts a value as raw HTML instead of escaped text, named as a warning.__html: the property ofdangerouslySetInnerHTMLthat holds the raw markup to insert.- State variable: a named box a page keeps a value in while it is running.
- XSS (cross-site scripting): a bug where an attacker's code runs inside someone else's web page, in that page's own context.
- Reflected XSS: XSS where the payload bounces straight back from the request and is not saved on the server.
- Stored XSS: XSS where the payload is saved on the server and shown to later visitors.
- Payload: the attack string sent to trigger the bug.
- Proof of concept (PoC): a small, safe demonstration that a bug is real.
onerror: a handler that runs when an element, such as an image, fails to load.- Origin: the combination of scheme, host and port that defines a page's security context.
- Cookie: a small piece of data a site stores in your browser to recognise you on later requests.
- Session cookie: the cookie that proves you are logged in.
HttpOnly: a cookie setting that keeps the cookie away from JavaScript.document.cookie: the browser property through which JavaScript can read and write cookies that are notHttpOnly.- CWE-79: the standard catalogue entry for improper neutralisation of input during web page generation, the XSS class.
- CVSS: a standard scorecard for the severity of a security bug.
- CVSS vector: the string of codes behind a CVSS score, recording the conditions of the bug.
UI:R: the CVSS code meaning the attack requires user interaction.S:C: the CVSS code meaning the impact crosses a security scope, here that the script runs in the page's origin.- Content Security Policy: a browser-side rule set that can restrict which scripts a page may run.
- Sanitise: clean a value so that any markup in it is safe before it is inserted as HTML.
- Phishing: tricking someone into clicking a link or handing over credentials by pretending to be trustworthy.
- Compiled JavaScript bundle: the combined, processed JavaScript file the browser downloads and runs.