Skip to content

mysql2 patches a bug that made parameterized queries unsafe

The Node.js MySQL driver, pulled 23.7 million times a week on npm, escaped quotes the same way no matter what SQL mode the server was running. One non-default mode was enough to turn a placeholder into an injection point.

mysql2, the Node.js MySQL driver pulled 23.7 million times a week on npm, has patched a SQL injection bug that worked even through parameterized queries. Version 3.25.0, published 30 November, fixes it.

Every version before that always escaped quotes in connection.query() and connection.escape() by prefixing them with a backslash, regardless of the server’s SQL mode. If the server had NO_BACKSLASH_ESCAPES enabled, MySQL treats that backslash as a literal character instead of an escape, so the quote still closes the string literal and a value passed through a ? placeholder can append its own SQL. GitHub’s advisory, GHSA-f763-mv98-7rv9, rates it 7.4 (High) and notes connection.execute(), which uses real prepared statements, was never affected, only the text-protocol query() path.

// vulnerable even though it looks parameterized
await conn.query('SELECT id FROM users WHERE name = ?', [name])

Why it matters: placeholders are supposed to be the safe way to build a query, and here they weren’t, if the connected server had that one non-default mode switched on. Check your server’s sql_mode, upgrade to mysql2 3.25.0 or later, and set the new noBackslashEscapes: true connection option as a second line of defence.

The caveat: there’s no sign of active exploitation, and the bug only bites when NO_BACKSLASH_ESCAPES is enabled on the server, which isn’t MySQL’s default.

Share

Related reading

More from The Wire

All briefs

node-tar patches a Windows RCE bug triggered by npm install

A newline-injection flaw in node-tar let a crafted archive forge its own file paths and run code during npm install on Windows. The fix landed in a string of releases up to 8.1.0 on 2 December.

via GitHub Security Advisories (isaacs/node-tar)