zlib: expose rejectGarbageAfterEnd option

Document rejectGarbageAfterEnd as a public decompression option and
validate it as a boolean.

Add coverage for stream, async convenience, and sync convenience APIs
across zlib, gzip, Brotli, and Zstd-backed decompression.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64023
Fixes: https://github.com/nodejs/node/issues/58247
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Filip Skokan authored and Antoine du Hamel committed 2026-07-30 13:51:43 +02:00
1 parent e334d30b4c
commit fd0fb00164
3 files changed
+170

No files matched your search

+18
View File
@@ -803,6 +803,9 @@ These advanced options are available for controlling decompression:
<!-- YAML
added: v0.11.1
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64023
description: The `rejectGarbageAfterEnd` option was added.
- version:
- v14.5.0
- v12.19.0
@@ -838,6 +841,10 @@ ignored by the decompression classes.
* `info` {boolean} (If `true`, returns an object with `buffer` and `engine`.)
* `maxOutputLength` {integer} Limits output size when using
[convenience methods][]. **Default:** [`buffer.kMaxLength`][]
* `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when
trailing input is detected after the end of the compressed stream. This
includes unreadable bytes and, when decompressing gzip, additional gzip
members following the first member. **Default:** `false`
See the [`deflateInit2` and `inflateInit2`][] documentation for more
information.
@@ -847,6 +854,9 @@ information.
<!-- YAML
added: v11.7.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64023
description: The `rejectGarbageAfterEnd` option was added.
- version:
- v14.5.0
- v12.19.0
@@ -865,6 +875,8 @@ Each Brotli-based class takes an `options` object. All options are optional.
* `maxOutputLength` {integer} Limits output size when using
[convenience methods][]. **Default:** [`buffer.kMaxLength`][]
* `info` {boolean} If `true`, returns an object with `buffer` and `engine`. **Default:** `false`
* `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when
input remains after the first complete compressed stream. **Default:** `false`
For example:
@@ -1088,6 +1100,10 @@ the inflate and deflate algorithms.
added:
- v23.8.0
- v22.15.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64023
description: The `rejectGarbageAfterEnd` option was added.
-->
<!--type=misc-->
@@ -1104,6 +1120,8 @@ Each Zstd-based class takes an `options` object. All options are optional.
* `dictionary` {Buffer} Optional dictionary used to
improve compression efficiency when compressing or decompressing data that
shares common patterns with the dictionary.
* `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when
input remains after the first complete compressed stream. **Default:** `false`
For example:
+8
View File
@@ -65,6 +65,7 @@ const {
const { owner_symbol } = require('internal/async_hooks').symbols;
const {
checkRangesOrGetDefault,
validateBoolean,
validateFunction,
validateUint32,
validateFiniteNumber,
@@ -246,6 +247,13 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
opts.maxOutputLength, 'options.maxOutputLength',
1, kMaxLength, kMaxLength);
if (opts.rejectGarbageAfterEnd !== undefined) {
validateBoolean(
opts.rejectGarbageAfterEnd,
'options.rejectGarbageAfterEnd',
);
}
if (opts.encoding || opts.objectMode || opts.writableObjectMode) {
opts = { ...opts };
opts.encoding = null;
@@ -0,0 +1,144 @@
'use strict';
require('../common');
const assert = require('assert');
const test = require('node:test');
const { finished } = require('stream/promises');
const zlib = require('zlib');
const trailingJunkError = {
code: 'ERR_TRAILING_JUNK_AFTER_STREAM_END',
name: 'TypeError',
};
function callAsync(fn, input, options) {
return new Promise((resolve, reject) => {
fn(input, options, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
async function collect(stream, input) {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.end(input);
await finished(stream);
return Buffer.concat(chunks);
}
const cases = [
{
label: 'inflate',
compress: zlib.deflateSync,
decompress: zlib.inflate,
decompressSync: zlib.inflateSync,
createDecompress: zlib.createInflate,
defaultOutput: 'a',
},
{
label: 'inflateRaw',
compress: zlib.deflateRawSync,
decompress: zlib.inflateRaw,
decompressSync: zlib.inflateRawSync,
createDecompress: zlib.createInflateRaw,
defaultOutput: 'a',
},
{
label: 'gunzip',
compress: zlib.gzipSync,
decompress: zlib.gunzip,
decompressSync: zlib.gunzipSync,
createDecompress: zlib.createGunzip,
defaultOutput: 'aa',
},
{
label: 'unzip',
compress: zlib.gzipSync,
decompress: zlib.unzip,
decompressSync: zlib.unzipSync,
createDecompress: zlib.createUnzip,
defaultOutput: 'aa',
},
{
label: 'brotli',
compress: zlib.brotliCompressSync,
decompress: zlib.brotliDecompress,
decompressSync: zlib.brotliDecompressSync,
createDecompress: zlib.createBrotliDecompress,
defaultOutput: 'a',
},
{
label: 'zstd',
compress: zlib.zstdCompressSync,
decompress: zlib.zstdDecompress,
decompressSync: zlib.zstdDecompressSync,
createDecompress: zlib.createZstdDecompress,
defaultOutput: 'a',
},
];
for (const {
label,
compress,
decompress,
decompressSync,
createDecompress,
defaultOutput,
} of cases) {
test(`rejectGarbageAfterEnd rejects trailing input for ${label}`, async () => {
const compressed = compress(Buffer.from('a'));
const withTrailingInput = Buffer.concat([compressed, compressed]);
assert.strictEqual(decompressSync(withTrailingInput).toString(), defaultOutput);
assert.strictEqual(
(await callAsync(decompress, withTrailingInput)).toString(),
defaultOutput,
);
assert.strictEqual(
(await collect(createDecompress(), withTrailingInput)).toString(),
defaultOutput,
);
assert.throws(
() => decompressSync(withTrailingInput, { rejectGarbageAfterEnd: true }),
trailingJunkError,
);
await assert.rejects(
callAsync(decompress, withTrailingInput, { rejectGarbageAfterEnd: true }),
trailingJunkError,
);
await assert.rejects(
collect(
createDecompress({ rejectGarbageAfterEnd: true }),
withTrailingInput,
),
trailingJunkError,
);
});
}
test('rejectGarbageAfterEnd must be a boolean', () => {
const compressed = zlib.deflateSync(Buffer.from('a'));
for (const value of [1, 'true', null]) {
assert.throws(
() => zlib.inflateSync(compressed, { rejectGarbageAfterEnd: value }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
},
);
assert.throws(
() => zlib.createInflate({ rejectGarbageAfterEnd: value }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
},
);
}
});