Skip to content

Commit 241eb61

Browse files
MylesBorinsaddaleax
authored andcommitted
zlib: gracefully set windowBits from 8 to 9
On 4 April 2017, Node.js versions v4.8.2 and v6.10.2 were released. These versions bumped the vendored zlib library from v1.2.8 to v1.2.11 in response to what it describes as low-severity CVEs. In zlib v1.2.9, a change was made that causes an error to be raised when a raw deflate stream is initialised with windowBits set to 8. In zlib v1.2.9, 8 become an invalid value for this parameter, and Node's zlib module will crash if you call this: ``` zlib.createDeflateRaw({windowBits: 8}) ``` On some versions this crashes Node and you cannot recover from it, while on some versions it throws an exception. The permessage-deflate library up to version v0.1.5 does make such a call with no try/catch This commit reverts to the original behavior of zlib by gracefully changed windowBits: 8 to windowBits: 9 for raw deflate streams. Original-PR-URL: nodejs-private/node-private#95 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> PR-URL: #16511 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 203b548 commit 241eb61

4 files changed

Lines changed: 31 additions & 20 deletions

File tree

‎doc/api/zlib.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,13 @@ added: v0.5.8
458458

459459
Creates and returns a new [DeflateRaw][] object with the given [options][].
460460

461-
*Note*: The zlib library rejects requests for 256-byte windows (i.e.,
462-
`{ windowBits: 8 }` in `options`). An `Error` will be thrown when creating
463-
a [DeflateRaw][] object with this specific value of the `windowBits` option.
461+
*Note*: An upgrade of zlib from 1.2.8 to 1.2.11 changed behavior when windowBits
462+
is set to 8 for raw deflate streams. zlib does not have a working implementation
463+
of an 8-bit Window for raw deflate streams and would automatically set windowBit
464+
to 9 if initially set to 8. Newer versions of zlib will throw an exception.
465+
This creates a potential DOS vector, and as such the behavior ahs been reverted
466+
in Node.js 8, 6, and 4. Node.js version 9 and higher will throw when windowBits
467+
is set to 8.
464468

465469
## zlib.createGunzip([options])
466470
<!-- YAML

‎lib/zlib.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ function Gunzip(opts) {
619619
inherits(Gunzip,Zlib);
620620

621621
functionDeflateRaw(opts){
622+
if(opts&&opts.windowBits===8)opts.windowBits=9;
622623
if(!(thisinstanceofDeflateRaw))
623624
returnnewDeflateRaw(opts);
624625
Zlib.call(this,opts,DEFLATERAW);

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,6 @@ const common = require('../common');
55
constassert=require('assert');
66
constzlib=require('zlib');
77

8-
// For raw deflate encoding, requests for 256-byte windows are rejected as
9-
// invalid by zlib (http://zlib.net/manual.html#Advanced).
10-
// This check was introduced in version 1.2.9 and prior to that there was
11-
// no such rejection which is the reason for the version check below
12-
// (http://zlib.net/ChangeLog.txt).
13-
if(!/^1\.2\.[0-8]$/.test(process.versions.zlib)){
14-
common.expectsError(
15-
()=>zlib.createDeflateRaw({windowBits: 8}),
16-
{
17-
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
18-
type: Error,
19-
message: 'Initialization failed'
20-
});
21-
}
22-
23-
// Regression tests for bugs in the validation logic.
24-
258
common.expectsError(
269
()=>zlib.createGzip({chunkSize: 0}),
2710
{

‎test/parallel/test-zlib.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const common = require('../common');
2424
constassert=require('assert');
2525
constzlib=require('zlib');
2626
conststream=require('stream');
27+
constfs=require('fs');
2728
constfixtures=require('../common/fixtures');
2829

2930
letzlibPairs=[
@@ -150,6 +151,28 @@ class SlowStream extends stream.Stream {
150151
}
151152
}
152153

154+
// windowBits: 8 shouldn't throw
155+
assert.doesNotThrow(()=>{
156+
zlib.createDeflateRaw({windowBits: 8});
157+
},'windowsBits set to 8 should follow legacy zlib behavior');
158+
159+
{
160+
constnode=fs.createReadStream(process.execPath);
161+
constraw=[];
162+
constreinflated=[];
163+
node.on('data',(chunk)=>raw.push(chunk));
164+
165+
// Usually, the inflate windowBits parameter needs to be at least the
166+
// value of the matching deflate’s windowBits. However, inflate raw with
167+
// windowBits = 8 should be able to handle compressed data from a source
168+
// that does not know about the silent 8-to-9 upgrade of windowBits
169+
// that older versions of zlib/Node perform.
170+
node.pipe(zlib.createDeflateRaw({windowBits: 9}))
171+
.pipe(zlib.createInflateRaw({windowBits: 8}))
172+
.on('data',(chunk)=>reinflated.push(chunk))
173+
.on('end',common.mustCall(
174+
()=>assert(Buffer.concat(raw).equals(Buffer.concat(reinflated)))));
175+
}
153176

154177
// for each of the files, make sure that compressing and
155178
// decompressing results in the same data, for every combination

0 commit comments

Comments
 (0)