Skip to content

Commit 631e433

Browse files
bnoordhuiscodebytere
authored andcommittedJun 7, 2020
zlib: reject windowBits=8 when mode=GZIP
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: #33045 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent 642f813 commit 631e433

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed
 

‎lib/zlib.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,11 @@ function Zlib(opts, mode) {
614614
mode === UNZIP)) {
615615
windowBits = 0;
616616
} else {
617+
// `{ windowBits: 8 }` is valid for deflate but not gzip.
618+
const min = Z_MIN_WINDOWBITS + (mode === GZIP ? 1 : 0);
617619
windowBits = checkRangesOrGetDefault(
618620
opts.windowBits, 'options.windowBits',
619-
Z_MIN_WINDOWBITS, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS);
621+
min, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS);
620622
}
621623

622624
level = checkRangesOrGetDefault(

‎test/parallel/test-zlib-failed-init.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ assert.throws(
2121
code: 'ERR_OUT_OF_RANGE',
2222
name: 'RangeError',
2323
message: 'The value of "options.windowBits" is out of range. It must ' +
24-
'be >= 8 and <= 15. Received 0'
24+
'be >= 9 and <= 15. Received 0'
2525
}
2626
);
2727

‎test/parallel/test-zlib-zero-windowBits.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ const zlib = require('zlib');
2828
code: 'ERR_OUT_OF_RANGE',
2929
name: 'RangeError',
3030
message: 'The value of "options.windowBits" is out of range. ' +
31-
'It must be >= 8 and <= 15. Received 0'
31+
'It must be >= 9 and <= 15. Received 0'
3232
});
3333
}

‎test/parallel/test-zlib.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const fixtures = require('../common/fixtures');
2929

3030
// Should not segfault.
3131
assert.throws(() => zlib.gzipSync(Buffer.alloc(0), { windowBits: 8 }), {
32-
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
33-
name: 'Error',
34-
message: 'Initialization failed',
32+
code: 'ERR_OUT_OF_RANGE',
33+
name: 'RangeError',
34+
message: 'The value of "options.windowBits" is out of range. ' +
35+
'It must be >= 9 and <= 15. Received 8',
3536
});
3637

3738
let zlibPairs = [

0 commit comments

Comments
 (0)
Please sign in to comment.