Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: fix node crashing on invalid options #13098

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ function zlibOnError(message, errno) {
var error = new Error(message);
error.errno = errno;
error.code = codes[errno];
this.emit('error', error);

if (this._initSuccess) {
this.emit('error', error);
} else {
process.nextTick(() => {
this.emit('error', error);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this error should still be thrown synchronously, if there is one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll make it impossible to catch this error since there's on object returned to userland code yet to listen to the error event on. That's basically the reason for all this machinery, otherwise @cjihrig's one-line fix would be just fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could change the ZCtx::Error(ctx, "Init error"); line to throw an error instead … that should work, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... yes, it should. And there would be no need for all those changes in zlib.js then.

}

function flushCallback(level, strategy, callback) {
Expand Down Expand Up @@ -222,20 +229,32 @@ class Zlib extends Transform {
this._handle = new binding.Zlib(mode);
this._handle.onerror = zlibOnError.bind(this);
this._hadError = false;
this._initSuccess = false;

var level = constants.Z_DEFAULT_COMPRESSION;
if (typeof opts.level === 'number') level = opts.level;

var strategy = constants.Z_DEFAULT_STRATEGY;
if (typeof opts.strategy === 'number') strategy = opts.strategy;

this._handle.init(opts.windowBits || constants.Z_DEFAULT_WINDOWBITS,
level,
opts.memLevel || constants.Z_DEFAULT_MEMLEVEL,
strategy,
opts.dictionary);
var windowBits = constants.Z_DEFAULT_WINDOWBITS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't these both just be ternaries?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They could, and I'd like them to be, but I just wrote these in a style consistent with the code above. How about me addressing this comment in a follow-up PR, together with some more refactoring like replacing most vars with lets and consts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

if (typeof opts.windowBits === 'number') windowBits = opts.windowBits;

var memLevel = constants.Z_DEFAULT_MEMLEVEL;
if (typeof opts.memLevel === 'number') memLevel = opts.memLevel;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change alters the existing behaviour a little. If we pass NaN or Zero, they used to pick the default value earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, thanks. It uncovers a whole bunch of bugs in the validation logic above, i.e., I wanted to say that zero would be caught before this line, but after trying it out it appeared to be not. And it was also possible to pass NaN as the value of some of the other options, like level.


this._initSuccess = this._handle.init(windowBits,
level,
memLevel,
strategy,
opts.dictionary);

if (this._initSuccess) {
this._buffer = Buffer.allocUnsafe(this._chunkSize);
} else {
this._buffer = null;
}

this._buffer = Buffer.allocUnsafe(this._chunkSize);
this._offset = 0;
this._level = level;
this._strategy = strategy;
Expand Down Expand Up @@ -467,7 +486,9 @@ function _close(engine, callback) {
if (!engine._handle)
return;

engine._handle.close();
if (engine._initSuccess)
engine._handle.close();

engine._handle = null;
}

Expand Down
14 changes: 11 additions & 3 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
namespace node {

using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
Expand Down Expand Up @@ -480,9 +481,11 @@ class ZCtx : public AsyncWrap {
memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len);
}

Init(ctx, level, windowBits, memLevel, strategy,
dictionary, dictionary_len);
bool result = Init(ctx, level, windowBits, memLevel, strategy,
dictionary, dictionary_len);
SetDictionary(ctx);

args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), result));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m pretty sure you don’t need Boolean::New here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, didn't know that :)

}

static void Params(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -499,7 +502,7 @@ class ZCtx : public AsyncWrap {
SetDictionary(ctx);
}

static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
static bool Init(ZCtx *ctx, int level, int windowBits, int memLevel,
int strategy, char* dictionary, size_t dictionary_len) {
ctx->level_ = level;
ctx->windowBits_ = windowBits;
Expand Down Expand Up @@ -553,6 +556,9 @@ class ZCtx : public AsyncWrap {

if (ctx->err_ != Z_OK) {
ZCtx::Error(ctx, "Init error");
if (dictionary != nullptr)
delete[] dictionary;
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d have to admit I prefer the solution suggested by @cjihrig in #13082 (comment) (or just move this error-checking block to the end of the Init() function, after ctx->init_done_ = true)

}


Expand All @@ -561,6 +567,8 @@ class ZCtx : public AsyncWrap {

ctx->write_in_progress_ = false;
ctx->init_done_ = true;

return true;
}

static void SetDictionary(ZCtx* ctx) {
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-zlib-failed-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const common = require('../common');

const assert = require('assert');
const zlib = require('zlib');

// For raw deflate or gzip encoding, a request for a 256-byte window is
// rejected as invalid, since only zlib headers provide means of transmitting
// the window size to the decompressor.
// (http://zlib.net/manual.html#Advanced)
const deflate = zlib.createDeflateRaw({ windowBits: 8 });
deflate.on('error', common.mustCall((error) => {
assert.ok(/Init error/.test(error));
}));