Skip to content

Commit 736ca65

Browse files
bnoordhuiscodebytere
authored andcommitted
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 <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent ef25033 commit 736ca65

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
@@ -613,9 +613,11 @@ function Zlib(opts, mode) {
613613
mode === UNZIP)) {
614614
windowBits = 0;
615615
} else {
616+
// `{ windowBits: 8 }` is valid for deflate but not gzip.
617+
const min = Z_MIN_WINDOWBITS + (mode === GZIP ? 1 : 0);
616618
windowBits = checkRangesOrGetDefault(
617619
opts.windowBits, 'options.windowBits',
618-
Z_MIN_WINDOWBITS, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS);
620+
min, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS);
619621
}
620622

621623
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)