Sitelet https://github.com/SimenB/node/commit/b42c0ccfa3bfd8800c432080a166b49c5c166206
Skip to content

Commit b42c0cc

Browse files
authored
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: nodejs#66359 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 7bdd1f6 commit b42c0cc

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎lib/zlib.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,13 @@ class ZlibBase extends Transform {
302302
);
303303
}
304304

305-
if (opts.encoding || opts.objectMode || opts.writableObjectMode) {
305+
if (opts.encoding || opts.objectMode || opts.writableObjectMode ||
306+
opts.decodeStrings === false) {
306307
opts = { ...opts };
307308
opts.encoding = null;
308309
opts.objectMode = false;
309310
opts.writableObjectMode = false;
311+
opts.decodeStrings = true;
310312
}
311313
}
312314

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
// Strings must be decoded before they reach the native handle, even when
4+
// decodeStrings: false is passed. This used to abort the process.
5+
6+
const common = require('../common');
7+
const assert = require('assert');
8+
const zlib = require('zlib');
9+
10+
const input = 'héllo wörld';
11+
12+
const pairs = [
13+
[zlib.createGzip, zlib.gunzipSync],
14+
[zlib.createDeflate, zlib.inflateSync],
15+
[zlib.createDeflateRaw, zlib.inflateRawSync],
16+
[zlib.createBrotliCompress, zlib.brotliDecompressSync],
17+
[zlib.createZstdCompress, zlib.zstdDecompressSync],
18+
];
19+
20+
for (const [createCompress, decompressSync] of pairs) {
21+
const compress = createCompress({ decodeStrings: false });
22+
const chunks = [];
23+
compress.on('data', (chunk) => chunks.push(chunk));
24+
compress.on('end', common.mustCall(() => {
25+
assert.strictEqual(decompressSync(Buffer.concat(chunks)).toString(), input);
26+
}));
27+
compress.end(input);
28+
}
29+
30+
zlib.gzip(input, { decodeStrings: false }, common.mustSucceed((compressed) => {
31+
assert.strictEqual(zlib.gunzipSync(compressed).toString(), input);
32+
}));

0 commit comments

Comments
 (0)