diff --git a/doc/api/zlib.md b/doc/api/zlib.md index 18a2c07ed1f..b05adfb09c7 100644 --- a/doc/api/zlib.md +++ b/doc/api/zlib.md @@ -803,6 +803,9 @@ These advanced options are available for controlling decompression: @@ -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: diff --git a/lib/zlib.js b/lib/zlib.js index 342d2109639..8ebc4fde142 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -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; diff --git a/test/parallel/test-zlib-reject-garbage-after-end.js b/test/parallel/test-zlib-reject-garbage-after-end.js new file mode 100644 index 00000000000..8039865f5f1 --- /dev/null +++ b/test/parallel/test-zlib-reject-garbage-after-end.js @@ -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', + }, + ); + } +});