Skip to main content
Vulnerability Database/CVE-2024-24558

CVE-2024-24558: TanStack React Query XSS Vulnerability

CVE-2024-24558 is a cross-site scripting flaw in TanStack React Query Next Experimental that enables attackers to inject malicious scripts. This post covers technical details, affected versions, and mitigation steps.

Published:

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Sources: National Vulnerability Database, CISA KEV, Exploit-DB

CVE-2024-24558 Overview

CVE-2024-24558 is a cross-site scripting (XSS) vulnerability in the @tanstack/react-query-next-experimental NPM package. The package supplies asynchronous state management, server-state utilities, and data fetching for Next.js applications. The flaw lets attackers inject malicious script content through unsanitized data returned from endpoints or supplied as input. When this data is serialized and streamed into HTML during server-side rendering, the browser executes attacker-controlled JavaScript. The vulnerability is tracked under [CWE-79: Improper Neutralization of Input During Web Page Generation].

Critical Impact

Attackers who control endpoint responses or user-supplied input can execute arbitrary JavaScript in victim browsers, enabling session theft, credential capture, and client-side account takeover.

Affected Products

  • @tanstack/react-query-next-experimental versions prior to 5.18.0
  • Next.js applications consuming the experimental streaming hydration provider
  • Node.js server-side rendering pipelines using the affected package

Discovery Timeline

  • 2024-01-30 - CVE-2024-24558 published to the National Vulnerability Database
  • 2026-06-17 - Last updated in NVD database

Technical Details for CVE-2024-24558

Vulnerability Analysis

The vulnerability resides in the HydrationStreamProvider component of the experimental Next.js integration. The provider serializes server-fetched query state into inline <script> tags that hydrate React Query caches on the client. Before the fix, serialization did not escape HTML-sensitive characters inside JSON strings. An attacker who could influence any field returned by a fetched endpoint — through a stored payload, open API field, or malicious third-party service — could break out of the JSON string context and inject executable JavaScript. User interaction is required, since the payload executes when a victim loads the affected page.

Root Cause

The root cause is missing output encoding when embedding JSON data inside HTML <script> elements. Characters such as <, >, &, and the Unicode line and paragraph separators (\\u2028, \\u2029) carry special meaning in HTML parsing contexts. Without escaping these characters, a JSON payload containing a sequence like </script> terminates the script tag early, allowing adjacent markup to be interpreted as new DOM content.

Attack Vector

Exploitation requires an attacker to place a malicious string into any data source that the server fetches through React Query. Common vectors include writable fields in a backing database, upstream APIs that mirror user input, and shared caches. When the server renders the page, the poisoned payload is serialized into the hydration script and delivered to every user requesting the view.

typescript
// Patched sanitizer added in packages/react-query-next-experimental/src/htmlescape.ts
const ESCAPE_LOOKUP: { [match: string]: string } = {
  "&": "\\\u0026",
  ">": "\\\u003e",
  "<": "\\\u003c",
  "\\u2028": "\\\u2028",
  "\\u2029": "\\\u2029",
};

export const ESCAPE_REGEX = /[&><\\u2028\\u2029]/g;

export function htmlEscapeJsonString(str: string): string {
  return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match]);
}

Source: TanStack query commit f2ddaf2

Detection Methods for CVE-2024-24558

Indicators of Compromise

  • Inline <script> blocks in server-rendered HTML containing unescaped <, >, or </script> sequences inside JSON payloads
  • Unexpected outbound requests from browsers loading pages that use HydrationStreamProvider
  • Content Security Policy violation reports referencing inline script execution from hydration payloads

Detection Strategies

  • Audit package-lock.json and yarn.lock for @tanstack/react-query-next-experimental versions earlier than 5.18.0
  • Scan server responses for the string pattern </script> embedded inside React Query hydration JSON
  • Add Software Composition Analysis (SCA) checks in CI pipelines that flag the vulnerable package range

Monitoring Recommendations

  • Enable Content Security Policy (CSP) reporting to capture unexpected script execution on hydrated pages
  • Monitor web application firewall logs for payloads containing </script>, \\u2028, or \\u2029 sequences in request bodies
  • Alert on anomalous changes to upstream API responses that feed React Query hooks

How to Mitigate CVE-2024-24558

Immediate Actions Required

  • Upgrade @tanstack/react-query-next-experimental to version 5.18.0 or later across all Next.js projects
  • Rebuild and redeploy any production bundles that pin the vulnerable version
  • Review stored data sources accessed through server-rendered React Query hooks for existing malicious payloads

Patch Information

The fix was committed in TanStack query commit f2ddaf2 and published in release 5.18.0. The patch introduces an htmlEscapeJsonString helper, mirrored from Next.js, that escapes &, <, >, \\u2028, and \\u2029 before serialized state is written to the hydration stream. Full details are available in GitHub Security Advisory GHSA-997g-27x8-43rf.

Workarounds

  • Sanitize all user-controlled fields on write using an allowlist encoder before they reach any endpoint consumed by React Query
  • Enforce a strict Content Security Policy that disallows inline scripts without a per-request nonce
  • Avoid using HydrationStreamProvider for views that render data from untrusted or externally-sourced APIs until the upgrade is complete
bash
# Upgrade the vulnerable package to the fixed release
npm install @tanstack/react-query-next-experimental@^5.18.0

# Verify no vulnerable versions remain in the dependency tree
npm ls @tanstack/react-query-next-experimental

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Sources: National Vulnerability Database, CISA KEV, Exploit-DB

Experience the Most Advanced Cybersecurity Platform

See how the world’s most intelligent, autonomous cybersecurity platform can protect your organization today and into the future.