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

CVE-2024-21509: Sidorares Mysql2 Prototype Poisoning Flaw

CVE-2024-21509 is a prototype poisoning vulnerability in Sidorares Mysql2 affecting versions before 3.9.4. This flaw stems from insecure object creation and improper input sanitization. This article covers technical details, affected versions, security impact, and mitigation strategies.

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-21509 Overview

CVE-2024-21509 is a Prototype Poisoning vulnerability affecting the mysql2 Node.js package before version 3.9.4. The flaw originates in the result object creation logic within text_parser.js and binary_parser.js, where parsed data is assigned to plain JavaScript objects without sanitizing properties such as __proto__ or constructor. A malicious or compromised MySQL server can return crafted column names that pollute the prototype chain of result objects on the client. The issue is tracked under CWE-1321: Improperly Controlled Modification of Object Prototype Attributes.

Critical Impact

Attackers controlling query results can inject properties into result objects, enabling integrity and availability impacts on downstream Node.js application logic.

Affected Products

  • sidorares/node-mysql2 versions prior to 3.9.4
  • Node.js applications using mysql2 as a MySQL/MariaDB client driver
  • Downstream frameworks and ORMs that depend on mysql2 for result parsing

Discovery Timeline

  • 2024-04-10 - CVE-2024-21509 published to NVD
  • 2026-06-17 - Last updated in NVD database

Technical Details for CVE-2024-21509

Vulnerability Analysis

The mysql2 package dynamically generates parser functions through parserFn to transform raw MySQL protocol rows into JavaScript objects. In vulnerable builds, the generated code initialized each row as const result = {};, inheriting from Object.prototype. Column names returned by the server were then written directly as properties on this object. When a server returns a column named __proto__ or constructor, the parser writes attacker-controlled data into locations that affect prototype chain lookups across the running Node.js process.

Exploitation requires a client to connect to a hostile MySQL server, or to a legitimate server that returns attacker-influenced column aliases. Once polluted, every subsequent object literal in the process may expose or inherit the injected properties, altering control flow in security-sensitive code paths such as access checks, template rendering, and configuration loading.

Root Cause

The root cause is insecure object construction combined with missing input sanitization on column identifiers passed through the dynamically compiled parser. Because the result object inherited from Object.prototype, any property assignment with a reserved key was interpreted as a prototype modification instead of a plain data write.

Attack Vector

The vulnerability is reachable over the network through standard MySQL protocol responses. An attacker who controls the database endpoint, performs a successful machine-in-the-middle attack on an unencrypted MySQL connection, or injects crafted column aliases into a trusted server can trigger the condition without authentication to the Node.js application itself.

javascript
// Security patch in lib/parsers/binary_parser.js (#2574)
   if (options.rowsAsArray) {
     parserFn(`const result = new Array(${fields.length});`);
   } else {
-    parserFn('const result = {};');
+    parserFn('const result = Object.create(null);');
+    parserFn(`Object.defineProperty(result, "constructor", {
+      value: Object.create(null),
+      writable: false,
+      configurable: false,
+      enumerable: false
+    });`);
   }

   // Global typeCast

Source: GitHub commit 4a964a3. The fix constructs result objects with a null prototype and locks the constructor property so attacker-supplied column names cannot reach Object.prototype.

Detection Methods for CVE-2024-21509

Indicators of Compromise

  • Node.js processes importing mysql2 at versions below 3.9.4 identified through npm ls mysql2 or software composition analysis.
  • MySQL protocol responses containing column names equal to __proto__, constructor, or prototype.
  • Unexpected properties appearing on arbitrary objects within application runtime, visible through heap dumps or logging.
  • Outbound MySQL connections from application servers to unapproved or unrecognized database hosts.

Detection Strategies

  • Run dependency scanners such as npm audit or Snyk against package-lock.json to flag mysql2 versions earlier than 3.9.4.
  • Enable application-layer logging of column metadata returned by MySQL servers and alert on reserved JavaScript property names.
  • Use runtime application self-protection or Node.js diagnostic hooks to detect writes to Object.prototype during request handling.

Monitoring Recommendations

  • Monitor egress traffic on TCP port 3306 and alert on connections to database endpoints outside the approved allowlist.
  • Track changes to package.json and package-lock.json in source control to catch downgrades of mysql2.
  • Review MySQL query logs for SELECT ... AS "__proto__" patterns indicating probing or exploitation attempts.

How to Mitigate CVE-2024-21509

Immediate Actions Required

  • Upgrade mysql2 to version 3.9.4 or later across all Node.js services and container images.
  • Audit transitive dependencies with npm ls mysql2 to identify indirect usage through ORMs or frameworks.
  • Enforce TLS on all MySQL client connections to prevent injection of malicious column metadata by network attackers.
  • Restrict outbound database connections to a defined allowlist of trusted MySQL servers.

Patch Information

The fix is delivered in mysql2 release v3.9.4 via pull request #2574 and commit 4a964a3. The patch replaces plain object literals with Object.create(null) and defines a non-writable constructor property on each result row. Additional context is available in the Snyk advisory SNYK-JS-MYSQL2-6591084 and the Slonser research write-up.

Workarounds

  • Set rowsAsArray: true in query options so the parser allocates arrays instead of prototype-inheriting objects.
  • Harden application code with Object.freeze(Object.prototype) at startup to block prototype writes.
  • Use a custom typeCast handler that rejects or renames reserved property names before they reach the result object.
bash
# Upgrade mysql2 to a patched release
npm install mysql2@^3.9.4

# Verify installed version across the dependency tree
npm ls mysql2

# Optional: enforce TLS when connecting to MySQL
# node example
# mysql.createConnection({ host, user, password, ssl: { rejectUnauthorized: true } })

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.