Summary
A cross-site scripting (XSS) issue in React Router's <ScrollRestoration> API can occur during server-side rendering in Framework Mode when getKey or storageKey props are derived from untrusted input.
If an attacker can influence these values, they may be able to trigger arbitrary JavaScript execution in the rendered HTML during SSR, leading to script injection in users' browsers.
Affected context
This vulnerability affects applications that:
- Use React Router or @remix-run/react in Framework Mode with server-side rendering enabled, and
- Use the
<ScrollRestoration>component withgetKeyand/orstorageKeyprops, and - Allow untrusted or user-controlled data to influence the scroll restoration keys.
Applications using Declarative Mode (<BrowserRouter>) or Data Mode (createBrowserRouter / <RouterProvider>), or that have Framework Mode SSR disabled, are generally not affected by this specific behavior.
Exploit preconditions
- Attack surface: Web application front-end rendered via React Router in Framework Mode SSR.
- Required access: Ability to control or influence data used to generate the
getKeyorstorageKeyvalues (e.g., via query params, path segments, headers, or persisted data that flows into these props). - Trigger point: A server-side render that includes
<ScrollRestoration>with keys derived from attacker-influenced data; the SSR output is then delivered to the browser and interpreted as HTML/JavaScript. - Why exploitation is possible: The scroll restoration keys are incorporated into the SSR output in a way that can break out of intended encoding and inject script-capable content. Because the keys are not strictly constrained or safely encoded for the context in which they are embedded, attacker-controlled values can result in XSS.
Why this happens
The root cause falls under CWE-79: Improper Neutralization of Input During Web Page Generation (Cross-Site Scripting).
In Framework Mode SSR, React Router's <ScrollRestoration> uses getKey/storageKey values to create identifiers used for preserving scroll position. When these keys are derived from untrusted input and embedded in the SSR HTML without sufficient context-aware encoding, they can introduce executable JavaScript into the page.
Conceptually, this is an output encoding problem: values that should behave as inert data (keys) are allowed to influence the structure of the rendered HTML/JavaScript during SSR. This insecure pattern combines:
- treating attacker-controlled values as trusted,
- rendering them in a sensitive HTML/JS context,
- and relying on application code to avoid hazardous characters rather than enforcing strict constraints or automatic escaping.
Potential impact
- Execution of arbitrary JavaScript in the context of the affected web application.
- Theft of session tokens, API tokens, or other sensitive information accessible to the browser.
- Unauthorized actions performed on behalf of authenticated users (e.g., account changes, data manipulation).
- Defacement of application content or injection of malicious UI elements (phishing overlays, fake dialogs).
- Use of the site as a delivery vehicle for further client-side exploitation or malware distribution.
General mitigation guidance
Mitigation centers on treating all data that can reach getKey/storageKey as untrusted and ensuring it cannot affect HTML/JS structure:
- Prefer static or strictly controlled keys for
<ScrollRestoration>rather than dynamically building them from raw user input. - If dynamic keys are required, ensure they are normalized and constrained (e.g., limited character sets, fixed formats, length restrictions) before they are used in the component.
- Apply context-aware output encoding where keys may flow into HTML or script contexts within SSR templates, so that they remain inert data and cannot break out into executable code.
- Architect routes and scroll restoration logic so that identifiers come from trusted application state or server-generated tokens instead of directly from request parameters or unvalidated headers.
- Adopt a secure-by-default SSR pattern: centralize how values are serialized into the HTML response and enforce escaping/encoding at that boundary rather than in individual components.
These patterns help ensure that even if untrusted data is present in routing or state, it cannot be rendered as executable script by the server.
How teams detect issues like this
Effective detection involves both code-level inspection and automated checks:
- Static analysis and linters: Configure rules to flag:
- Uses of
<ScrollRestoration>wheregetKeyorstorageKeyare derived (directly or indirectly) fromlocation, query params, request headers, or other user-controlled inputs. - Custom serialization logic that embeds route-related data into inline scripts or data attributes without explicit encoding.
- Code review heuristics:
- Trace the data flow for any values passed into
getKey/storageKeyand verify that they are either constant, server-controlled, or rigorously validated. - Look for ad-hoc escaping or manual string concatenation involving routing state in SSR code; treat these as high-risk and require justification.
- Security testing:
- Incorporate XSS payload testing into automated SSR integration tests for routes that use
<ScrollRestoration>in Framework Mode. - Use dynamic scanners or security proxies to attempt injection via parameters known to influence routing and keys.
- Policy enforcement:
- Establish project guidelines that any data rendered into SSR output must pass through well-defined serialization/encoding functions.
- Optionally, integrate SAST/DAST tools into CI to highlight unescaped uses of routing state in server-rendered HTML.

