zlib: fix abort with decodeStrings: false

Passing decodeStrings: false to a zlib stream, or to an async
convenience method, let strings reach the native handle, which
aborts the process on its Buffer check. Force decodeStrings back to
true, as is already done for encoding and objectMode.

Signed-off-by: James Ross <james@jross.me>
PR-URL: https://github.com/nodejs/node/pull/66359
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
James Ross authored and GitHub committed 2026-09-29 21:48:17 +00:00
1 parent 7bdd1f6363
commit b42c0ccfa3
2 files changed
+35 -1

No files matched your search

+3 -1
View File
@@ -302,11 +302,13 @@ class ZlibBase extends Transform {
);
}
if (opts.encoding || opts.objectMode || opts.writableObjectMode) {
if (opts.encoding || opts.objectMode || opts.writableObjectMode ||
opts.decodeStrings === false) {
opts = { ...opts };
opts.encoding = null;
opts.objectMode = false;
opts.writableObjectMode = false;
opts.decodeStrings = true;
}
}
+32
View File
@@ -0,0 +1,32 @@
'use strict';
// Strings must be decoded before they reach the native handle, even when
// decodeStrings: false is passed. This used to abort the process.
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');
const input = 'héllo wörld';
const pairs = [
[zlib.createGzip, zlib.gunzipSync],
[zlib.createDeflate, zlib.inflateSync],
[zlib.createDeflateRaw, zlib.inflateRawSync],
[zlib.createBrotliCompress, zlib.brotliDecompressSync],
[zlib.createZstdCompress, zlib.zstdDecompressSync],
];
for (const [createCompress, decompressSync] of pairs) {
const compress = createCompress({ decodeStrings: false });
const chunks = [];
compress.on('data', (chunk) => chunks.push(chunk));
compress.on('end', common.mustCall(() => {
assert.strictEqual(decompressSync(Buffer.concat(chunks)).toString(), input);
}));
compress.end(input);
}
zlib.gzip(input, { decodeStrings: false }, common.mustSucceed((compressed) => {
assert.strictEqual(zlib.gunzipSync(compressed).toString(), input);
}));