mysql2 (Node.js) · CVE-2026-139877
mysql2 escaping
Patch now if…Needs a database setting that's off by default.
TL;DR
mysql2, the popular Node.js MySQL driver, lets attackers inject SQL through ? placeholders. A proof of concept is public; no attacks seen. Only databases with NO_BACKSLASH_ESCAPES turned on are affected; it's off by default. Update to 3.25.0 or later.
More details
VulnCheck scores it 9.1 Critical under CVSS 4.0; under CVSS 3.1, VulnCheck and mysql2's advisory both give 7.4 High, with high complexity because the attacker can't turn the setting on. mysql2 always escaped a quote as \'; with the setting on, the database reads the backslash as a plain character, so the quote ends the string and the rest runs as SQL. The fix went public on 17 Nov; the older mysql driver and sqlstring carry the same caution in their READMEs.
How scary is it?
Are you affected? only if all are true
1mysql2 3.24.6 or older
mysql2's advisory lists every release through 3.24.6; the CVE record says before 3.25.0. Check which version your app installs with npm ls mysql2.1,2,12
2NO_BACKSLASH_ESCAPES is oncommand
The database session's sql_mode must include it. It isn't in the default mode of MySQL 8.4 or MariaDB, and it isn't part of MySQL's ANSI mode; someone has to set it in the server config, at startup or with SET sql_mode. If your app sets its own mode when it connects, check from the app's connection.1,5,6
SELECT @@SESSION.sql_mode;3Untrusted values go to query()
The bug is in the text-protocol path: values passed to connection.query(), connection.escape() or SqlString.escape(), directly or through an ORM or framework built on mysql2, including ordinary ? placeholders. connection.execute() (prepared statements) is not affected.1
What to do
1Update mysql2
3.25.0 fixes it by escaping for the setting whenever the server reports it. 3.26.0 (30 Nov) also fixes two Medium bugs: a rogue or on-path MySQL server can recover the password on connections without TLS, and the string 'false' in one option turns on the cleartext password plugin. It also fixes escaping on non-UTF-8 connection charsets, with no advisory yet. After 3.26.0, connections without TLS to accounts that need RSA full authentication can fail with PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED; mysql2's advisory lists the options.1,3,4,9,10,13,17
Latest safe versions checked 3 Dec
npm ls mysql2.
2Setting on? Add noBackslashEscapes
If your server runs with NO_BACKSLASH_ESCAPES, mysql2 suggests also setting the noBackslashEscapes: true connection option. 3.25.0's automatic fix can miss a stored routine that changes the mode, a mode set by init_connect, a mode change earlier in a multi-statement query, and pool.escape()/pool.format(); the option covers those. The static mysql.escape() and mysql.format() need { noBackslashEscapes: true } as their last argument, and a custom queryFormat escapes for the mode in effect when query() is called.1,14,15,16
3Can't update yet?
mysql2's advisory gives two workarounds: remove NO_BACKSLASH_ESCAPES from the server's sql_mode, or send untrusted values through connection.execute(). Prepared statements aren't available everywhere; mysql2 names Cloudflare Hyperdrive, which refuses them.1,14
4On mysql or sqlstring?
Their READMEs say their escaping only works when NO_BACKSLASH_ESCAPES is off, which is MySQL's default. If you use them, we'd keep it off.7,8
This page shows you how to check. It can't tell you you're safe.
Sources (17) and changelog
- mysql2 GHSA-f763: only under NO_BACKSLASH_ESCAPES, not a MySQL default; query() affected, execute() not; PoC; High 7.4; patched in 3.25.0 30 Nov
- CVE record: VulnCheck, mysql2 before 3.25.0, CVSS 4.0 9.1 Critical, CVSS 3.1 7.4 30 Nov
- mysql2 3.25.0 release notes: escape for NO_BACKSLASH_ESCAPES, noBackslashEscapes option 29 Nov
- mysql2 3.26.0 release notes 30 Nov
- MySQL 8.4 manual: default SQL mode, ANSI mode, SELECT @@SESSION.sql_mode
- MariaDB docs: default sql_mode
- sqlstring README: escaping only works with NO_BACKSLASH_ESCAPES disabled
- mysql README: escaping only works with NO_BACKSLASH_ESCAPES disabled
- mysql2 GHSA-cc6q: rogue or on-path server recovers the password without TLS, Medium, fixed in 3.26.0 1 Dec
- mysql2 GHSA-mccj: string 'false' enables the cleartext password plugin, Medium, fixed in 3.26.0 1 Dec
- CISA KEV catalog: not listed
- npm docs: npm ls lists installed packages
- mysql2 commit 4bd4699: keep escaped SQL intact on non-UTF-8 connection charsets 30 Nov
- mysql2 PR #4684: the fix, public 17 Nov; cases it misses; Hyperdrive refuses prepared statements 17 Nov
- mysql2 PR #4718: the noBackslashEscapes option and the cases it covers 29 Nov
- mysql2 docs: cases the status flags miss, static mysql.escape()/format(), custom queryFormat
- mysql2 security advisories: none for the 3.26.0 charset fix on 1 Dec
- Published.