From d8a380e13665ef06ffbfa220cb3a7aaaaa17c9fd Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 24 Apr 2020 21:12:32 +0200 Subject: [PATCH] zlib: reject windowBits=8 when mode=GZIP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's also handled in C++ land now, per the previous commit, but intercepting it in JS land makes for prettier error messages. PR-URL: https://github.com/nodejs/node/pull/33045 Reviewed-By: Anna Henningsen Reviewed-By: Gerhard Stöbich Reviewed-By: David Carlier --- lib/zlib.js | 4 +++- test/parallel/test-zlib-failed-init.js | 2 +- test/parallel/test-zlib-zero-windowBits.js | 2 +- test/parallel/test-zlib.js | 7 ++++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index 502ee61aa3bd9d..be8f9401a310d0 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -613,9 +613,11 @@ function Zlib(opts, mode) { mode === UNZIP)) { windowBits = 0; } else { + // `{ windowBits: 8 }` is valid for deflate but not gzip. + const min = Z_MIN_WINDOWBITS + (mode === GZIP ? 1 : 0); windowBits = checkRangesOrGetDefault( opts.windowBits, 'options.windowBits', - Z_MIN_WINDOWBITS, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS); + min, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS); } level = checkRangesOrGetDefault( diff --git a/test/parallel/test-zlib-failed-init.js b/test/parallel/test-zlib-failed-init.js index 1f99a378fc6eda..95f401a3718f30 100644 --- a/test/parallel/test-zlib-failed-init.js +++ b/test/parallel/test-zlib-failed-init.js @@ -21,7 +21,7 @@ assert.throws( code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "options.windowBits" is out of range. It must ' + - 'be >= 8 and <= 15. Received 0' + 'be >= 9 and <= 15. Received 0' } ); diff --git a/test/parallel/test-zlib-zero-windowBits.js b/test/parallel/test-zlib-zero-windowBits.js index 730690a07c423f..a27fd6734a5425 100644 --- a/test/parallel/test-zlib-zero-windowBits.js +++ b/test/parallel/test-zlib-zero-windowBits.js @@ -28,6 +28,6 @@ const zlib = require('zlib'); code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "options.windowBits" is out of range. ' + - 'It must be >= 8 and <= 15. Received 0' + 'It must be >= 9 and <= 15. Received 0' }); } diff --git a/test/parallel/test-zlib.js b/test/parallel/test-zlib.js index 3af791c379a45f..65050b85a036cf 100644 --- a/test/parallel/test-zlib.js +++ b/test/parallel/test-zlib.js @@ -29,9 +29,10 @@ const fixtures = require('../common/fixtures'); // Should not segfault. assert.throws(() => zlib.gzipSync(Buffer.alloc(0), { windowBits: 8 }), { - code: 'ERR_ZLIB_INITIALIZATION_FAILED', - name: 'Error', - message: 'Initialization failed', + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: 'The value of "options.windowBits" is out of range. ' + + 'It must be >= 9 and <= 15. Received 8', }); let zlibPairs = [