Skip to content
Putting technology to work.
Insights to guide decisions and action.

Search articles

mysql2 3.25.0 fixes an injection possible under specific SQL modes

Table of contents · 7 items

Passing input from search forms or APIs through placeholders, as in connection.query('... WHERE name = ?', [name]), is the standard way to prevent SQL injection. In mysql2 for Node.js, however, a single setting on the database side meant that values could escape out of the string literal even with this approach.

mysql2, the driver for connecting to MySQL and MariaDB from Node.js, released 3.25.0 on November 30, Japan time (23:12 UTC on November 29 according to the npm record). The previous version was 3.24.6, released on October 13. 3.25.0 includes a fix for a problem where value escaping does not work correctly when the SQL mode NO_BACKSLASH_ESCAPES is enabled.

At 10:13 (Japan time) on the same day, advisory GHSA-f763-mv98-7rv9 for this issue was published in the mysql2 repository. The severity is High (7.4 under CVSS 3.1), all versions up to and including 3.24.6 are affected, and the fixed version is 3.25.0. However, as of around 10:30 Japan time, it was not yet listed in the GitHub Advisory Database, and an npm advisory lookup did not return this issue for 3.24.6. At that point, npm audit would not alert you to it.

Based on the advisory, changelog and documentation in the mysql2 repository, and on the results of running 3.24.6 and 3.25.0 against MariaDB in our test environment, this article summarizes what was happening, which systems are affected, and what to check before upgrading.

What was happening

NO_BACKSLASH_ESCAPES is an SQL mode that treats backslashes (\) inside strings as ordinary characters rather than escape characters. The mysql2 documentation explains that in this mode, "escaping quotes with a backslash no longer keeps the value inside the string."

mysql2 embeds values passed to query() into the SQL string on the client side. Up to 3.24.6, it escaped values by adding a backslash before quotes, regardless of the server's mode. For example, the value ' OR 1=1 -- becomes the following SQL.

SELECT id, name, secret FROM users WHERE name = '\' OR 1=1 -- '

In the normal mode, \' is read as "a quote character," and the whole thing becomes a single string. Under NO_BACKSLASH_ESCAPES, '\' closes as a string consisting of a single backslash, and the following OR 1=1 is read as a condition.

In 3.25.0, the connection tracks the mode through the state information it receives from the server (status flags), and while the mode is enabled it switches to doubling quotes (''). Values that contain backslashes are rewritten into an expression using REPLACE() so that they are read as the same value in either mode.

Results on MariaDB in our test environment

We set up MariaDB 10.11.14 in an isolated environment and created a test table with three columns, id, name and secret, containing only two rows (alice and bob). We connected from Node.js v22.23.5 with mysql2 3.24.6 and 3.25.0 and passed the same value ' OR 1=1 -- as name (once each).

Diagram comparing mysql2 3.24.6 and 3.25.0. When a value was passed to query() on a connection with NO_BACKSLASH_ESCAPES enabled, 3.24.6 returned both rows and 3.25.0 returned 0 rows. execute() returned 0 rows in both versions. When a value containing one backslash was saved, 3.24.6 saved it with the backslash doubled, while 3.25.0 saved it unchanged.

  • When the mode is not enabled: both versions returned 0 rows. The problem does not occur with the default settings
  • When enabled within the connection with SET SESSION sql_mode: 3.24.6 returned 2 rows with query(), exposing even other users' secret column. 3.25.0 returned 0 rows
  • When enabled server-wide with SET GLOBAL sql_mode and the app does nothing: 3.24.6 returned 2 rows and 3.25.0 returned 0 rows. Looking at the app code does not tell you whether the mode is enabled
  • execute() (prepared statements): both versions returned 0 rows, because values are sent separately from the SQL
  • Saving values that contain backslashes: when C:\dir was saved, 3.24.6 stored it with the backslash doubled, while 3.25.0 stored it unchanged. Apart from injection, this was also a bug that altered values when they were saved

Conditions for affected systems

You are affected when both of the following apply (our editorial summary).

  1. NO_BACKSLASH_ESCAPES is included in the MySQL or MariaDB sql_mode. This applies whether it comes from the server configuration file, SET GLOBAL, a init_connect run when connecting, or the app's SET SESSION
  2. You embed values with mysql2's query(), format() or escape()

The advisory also defines the scope as values that pass through connection.query(), connection.escape() and SqlString.escape(), and excludes connection.execute(). The advisory's reproduction steps use a MySQL 8.4 server, so the problem is not limited to MariaDB. The advisory also explains that this mode is not a MySQL default but is enabled through server configuration.

In the default configuration of MariaDB 10.11.14 (the Ubuntu 24.04 package) in our test environment, NO_BACKSLASH_ESCAPES was not included. On the other hand, server configuration is managed separately from the app code. Because settings the app team is not aware of may be in place, check the actual values (our editorial recommendation).

SELECT @@GLOBAL.sql_mode, @@SESSION.sql_mode;

The advisory states that the issue applies not only when calling connection.query() directly but also when going through ORMs or frameworks built on top of it. When you use an ORM (such as Sequelize or Knex), whether value embedding goes through mysql2's query() differs by library. We have not checked this in this article.

Caveats that remain in 3.25.0

The mysql2 documentation lists cases that cannot be tracked through status flags.

  • When sql_mode is changed inside a stored procedure or function
  • A mode set with init_connect is not reflected until the first statement returns
  • When multiple statements are sent in a single request and the mode is changed among them
  • pool.escape() and pool.format() are not tied to a specific connection, so they always escape with backslashes. The same applies to mysql.escape() and mysql.format()
  • A custom queryFormat runs only once, at the moment query() is called, and escapes according to the mode at that moment

In our check in the test environment as well, 3.25.0's pool.format() and mysql.format() built ' OR 1=1 -- as '\' OR 1=1 -- ', unchanged.

If your server may run in this mode, enable the noBackslashEscapes setting added in 3.25.0. In every case, values are then written in a form that is read the same way in either mode, which also prevents the exceptions above. In our test environment as well, a connection with this setting built values by doubling quotes even when the mode was not enabled.

const pool = mysql.createPool({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  database: process.env.DB_NAME,
  noBackslashEscapes: true,
});

According to the documentation, with this setting you get a syntax error in places where only quoted strings can be written, such as IDENTIFIED BY. In addition, if notation such as 'it\'s' remains inside an SQL string, calls that embed values will return TypeError.

What to check before upgrading (editorial proposal)

  1. Check sql_mode. Run the SQL above in both production and staging and see whether NO_BACKSLASH_ESCAPES is included. Also check the configuration files and init_connect
  2. Check your mysql2 version. Use npm ls mysql2 to look at both direct dependencies and dependencies pulled in through other packages. On November 27 (Japan time), advisory GHSA-7hh4-7hx5-vxx8 was also published in the repository, covering four other issues fixed in 3.19.1 in March (including one where query parameters in the connection string could override host or ssl). If a version older than 3.19.1 remains, it is affected by that advisory too
  3. Upgrade to 3.25.0. The same version also includes a change that stops with RangeError when more values than SQL placeholders are passed to execute(). Code that passed extra values will start failing after the upgrade
  4. If you use the mode, add noBackslashEscapes: true. After adding it, check that you are not running statements where only quoted strings can be written, such as user creation
  5. Move value embedding to execute() wherever you can. Values are sent separately from the SQL, so they are not affected by the SQL mode

As workarounds if you cannot upgrade right away, the advisory suggests switching every statement that contains untrusted values to execute(), and removing NO_BACKSLASH_ESCAPES from the server's sql_mode.

We covered a case where search conditions you thought were protected did not take effect in a NoSQL driver in our Mongoose article. If you are considering migrating to MariaDB, our MariaDB 13 article is also helpful for checking compatibility.

Scope of verification

On November 30, 2026, we reviewed the sidorares/node-mysql2 repository (advisories GHSA-f763-mv98-7rv9 and GHSA-7hh4-7hx5-vxx8, tags v3.24.6 and v3.25.0, the descriptions and diffs of fix commits ef0dfa49, 782afb99 and 9906f1e2, Changelog.md, and website/docs/documentation/extras.mdx), whether the advisories were listed in the GitHub Advisory Database, and the npm publication times and advisory lookup results (desk research). On the same day, we also ran mysql2 3.24.6 and 3.25.0 with MariaDB 10.11.14 and Node.js v22.23.5 and compared the results on a test table (hands-on testing; the scripts and output are saved in the article's evidence records). We could not open the MySQL documentation itself and did not test on a MySQL server (reproduction on MySQL 8.4 is based on the advisory). We have not checked the exceptions for stored procedures, init_connect and multiple statements, the behavior through ORMs, or when the two advisories will be listed in the GitHub Advisory Database.

For maintenance of business systems that use Node.js and databases, or for checking the impact of driver updates, please contact us through Development, AI and automation consultation.

References

Share this articleXFacebook
Kakeru Suzuki

Fascinated by the possibilities of technology, has had a deep interest in programming and digital art since student days

Turn this article's theme into your company's next step

Concrete steps forward for your organization.

We organize your desired architecture, legacy systems, and operational requirements to formulate your next steps toward execution.

  • Desired architecture
  • Integration with existing environments
  • Operational requirements
Consult on development & operations initiatives

You can consult with us from the initial conceptual stage. Details from this article will be carried over to the inquiry form.

Receive the latest articles by email