GHSA-rgwj-5xj2-c3m3: Medium severity npm/mysql2 vulnerability
Vulnerability Details
File: lib/compressedprotocol.js Line: 43 (zlib.inflate(body, (err, data) => { ... }) inside handleCompressedPacket)
Root Cause When a connection is created with compress: true (and the server advertises CLIENTCOMPRESS), every incoming packet is unwrapped by handleCompressedPacket() in lib/compressedprotocol.js, which calls:
js zlib.inflate(body, (err, data) => { ... });
No options object (in particular, no maxOutputLength) is passed. Node's zlib convenience methods default maxOutputLength to buffer.kMaxLength, which on this platform is Number.MAXSAFEINTEGER — i.e. effectively unbounded until the process runs out of memory. The 3-byte "length of payload before compression" field in the compressed-packet header is read (packet.readInt24()) but is only used to branch on !== 0; it is never used to cap or validate the actual inflate output size, and the real decompressed size is determined purely by the attacker-supplied deflate stream.
Because DEFLATE can reach compression ratios over 1000:1 for crafted repetitive input, an attacker who controls (or MITMs, on a non-TLS connection) the MySQL server endpoint can send a single small compressed packet that expands to gigabytes in the client's memory — a classic decompression-bomb / "zip bomb" applied to MySQL's client-compression protocol.
Attack Scenario 1. Application connects with mysql2/mysql2/promise using compress: true (a documented option for reducing bandwidth, commonly used for cloud/WAN DB connections). 2. The connection target is attacker-controlled or attacker-compromised, or an attacker MITMs a non-TLS connection. 3. Right after authentication succeeds, the malicious endpoint sends one crafted compressed packet whose deflate stream is small on the wire (hundreds of KB) but decompresses to several GB. 4. zlib.inflate() starts allocating memory for the full decompressed output with no ceiling. 5. The Node.js process's RSS grows uncontrolled until OOM-kill or crash — no query needs to be issued by the client; the malicious packet alone is enough.
Impact Denial of Service of the client application (process crash / OOM) — not the database itself. No authentication bypass or data exposure. Requires compress: true plus a malicious/compromised server or MITM position.
Vulnerable Code js function handleCompressedPacket(packet) { const connection = this; const deflatedLength = packet.readInt24(); const body = packet.readBuffer();
if (deflatedLength !== 0) { connection.inflateQueue.push((task) => { zlib.inflate(body, (err, data) => { if (err) { connection.handleNetworkError(err); return; } connection.bumpCompressedSequenceId(packet.numPackets); connection.inflatedPacketsParser.execute(data); task.done(); }); }); } else { ... } }
Recommended Fix js const MAXINFLATEDPACKETSIZE = 1 1024 1024 1024; // e.g. 1 GiB, ideally configurable
zlib.inflate(body, { maxOutputLength: MAXINFLATEDPACKETSIZE }, (err, data) => { if (err) { connection.handleNetworkError(err); return; } ... }); maxOutputLength makes zlib.inflate abort with ERRBUFFERTOOLARGE as soon as the decompressed size would exceed the cap, routing into the exact same (already-existing) err → connection.handleNetworkError(err) path, so no new error-handling logic is required.
Verification Dynamically confirmed on v3.23.0 (HEAD) using a minimal rogue "MySQL server" built on node-mysql2's own server-mode helpers (mysql.createServer, Packets.Handshake, connection.writeOk()). The rogue server completes a real handshake advertising CLIENTCOMPRESS, then writes one raw compressed frame (509,604 bytes on the wire — a zlib deflate of 500 MB of zero bytes, ratio 1028.8:1) directly to the socket. A normal mysql.createConnection({ ..., compress: true }) victim client — which never issues any query — had its RSS grow from 74.3 MB to 1115.0 MB after receiving that single packet, before erroring out with PROTOCOLUNEXPECTEDPACKET once the client tried to parse the inflated zero-filled buffer as MySQL packets. The memory allocation happens unconditionally before any content validation.
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
npm/mysql2to a version that resolves this vulnerability.Fixed in 3.23.1
Event History
Frequently Asked Questions
Which deployments are exposed?
Connections created with compress: true are exposed when the MySQL server advertises CLIENT_COMPRESS. Connections that do not negotiate compressed packets do not invoke the affected decompression path.
What must an attacker be able to do?
The attacker must control the MySQL server endpoint, or be able to man-in-the-middle a non-TLS connection. They can then send a crafted compressed packet whose actual decompressed output is far larger than its compressed input.
What can be done before a patch is applied?
Disable connection compression so that compressed packets are not processed by handleCompressedPacket(). Use TLS for MySQL connections to prevent an attacker from modifying traffic in transit.