Browse Source

zlib: fix node crashing on invalid options

This is a partial backport of semver-patch bits of
9e4660b518.

This commit fixes the Node process crashing when constructors of classes
of the zlib module are given invalid options.

* Throw an Error when the zlib library rejects the value of windowBits,
  instead of crashing with an assertion.

* Treat windowBits and memLevel options consistently with other ones and
  don't crash when non-numeric values are given.

Backport-PR-URL: https://github.com/nodejs/node/pull/14860
PR-URL: https://github.com/nodejs/node/pull/13098
Fixes: https://github.com/nodejs/node/issues/13082
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
v4.x-staging
Alexey Orlenko 8 years ago
committed by Myles Borins
parent
commit
2a8ef7a1af
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 4
      doc/api/zlib.md
  2. 14
      lib/zlib.js
  3. 13
      src/node_zlib.cc
  4. 13
      test/parallel/test-zlib-failed-init.js

4
doc/api/zlib.md

@ -373,6 +373,10 @@ added: v0.5.8
Returns a new [DeflateRaw][] object with an [options][].
*Note*: The zlib library rejects requests for 256-byte windows (i.e.,
`{ windowBits: 8 }` in `options`). An `Error` will be thrown when creating a
[DeflateRaw][] object with this specific value of the `windowBits` option.
## zlib.createGunzip([options])
<!-- YAML
added: v0.5.8

14
lib/zlib.js

@ -372,9 +372,19 @@ function Zlib(opts, mode) {
var strategy = exports.Z_DEFAULT_STRATEGY;
if (typeof opts.strategy === 'number') strategy = opts.strategy;
this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
var windowBits = exports.Z_DEFAULT_WINDOWBITS;
if (opts.windowBits && typeof opts.windowBits === 'number') {
windowBits = opts.windowBits;
}
var memLevel = exports.Z_DEFAULT_MEMLEVEL;
if (opts.memLevel && typeof opts.memLevel === 'number') {
memLevel = opts.memLevel;
}
this._handle.init(windowBits,
level,
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
memLevel,
strategy,
opts.dictionary);

13
src/node_zlib.cc

@ -469,16 +469,19 @@ class ZCtx : public AsyncWrap {
CHECK(0 && "wtf?");
}
if (ctx->err_ != Z_OK) {
ZCtx::Error(ctx, "Init error");
}
ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
ctx->dictionary_len_ = dictionary_len;
ctx->write_in_progress_ = false;
ctx->init_done_ = true;
if (ctx->err_ != Z_OK) {
if (dictionary != nullptr) {
delete[] dictionary;
ctx->dictionary_ = nullptr;
}
ctx->env()->ThrowError("Init error");
}
}
static void SetDictionary(ZCtx* ctx) {

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

@ -0,0 +1,13 @@
'use strict';
require('../common');
const assert = require('assert');
const zlib = require('zlib');
// For raw deflate encoding, requests for 256-byte windows are rejected as
// invalid by zlib.
// (http://zlib.net/manual.html#Advanced)
assert.throws(() => {
zlib.createDeflateRaw({ windowBits: 8 });
}, /^Error: Init error$/);
Loading…
Cancel
Save