Skip to content

zlib: fix raw inflate with custom dictionary #8512

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 additions & 2 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ class ZCtx : public AsyncWrap {
case INFLATERAW:
ctx->err_ = inflate(&ctx->strm_, ctx->flush_);

// If data was encoded with dictionary
if (ctx->err_ == Z_NEED_DICT && ctx->dictionary_ != nullptr) {
// If data was encoded with dictionary (INFLATERAW will have it set in
// SetDictionary, don't repeat that here)
if (ctx->mode_ != INFLATERAW &&
ctx->err_ == Z_NEED_DICT &&
ctx->dictionary_ != nullptr) {
// Load it
ctx->err_ = inflateSetDictionary(&ctx->strm_,
ctx->dictionary_,
Expand Down Expand Up @@ -552,6 +555,13 @@ class ZCtx : public AsyncWrap {
ctx->dictionary_,
ctx->dictionary_len_);
break;
case INFLATERAW:
// The other inflate cases will have the dictionary set when inflate()
// returns Z_NEED_DICT in Process()
ctx->err_ = inflateSetDictionary(&ctx->strm_,
ctx->dictionary_,
ctx->dictionary_len_);
break;
default:
break;
}
Expand Down
23 changes: 17 additions & 6 deletions test/parallel/test-zlib-dictionary-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,37 @@ const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

// Should raise an error, not trigger an assertion in src/node_zlib.cc
// String "test" encoded with dictionary "dict".
const input = Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]);

{
const stream = zlib.createInflate();

stream.on('error', common.mustCall(function(err) {
assert(/Missing dictionary/.test(err.message));
}));

// String "test" encoded with dictionary "dict".
stream.write(Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]));
stream.write(input);
}

// Should raise an error, not trigger an assertion in src/node_zlib.cc
{
const stream = zlib.createInflate({ dictionary: Buffer.from('fail') });

stream.on('error', common.mustCall(function(err) {
assert(/Bad dictionary/.test(err.message));
}));

// String "test" encoded with dictionary "dict".
stream.write(Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]));
stream.write(input);
}

{
const stream = zlib.createInflateRaw({ dictionary: Buffer.from('fail') });

stream.on('error', common.mustCall(function(err) {
// It's not possible to separate invalid dict and invalid data when using
// the raw format
assert(/invalid/.test(err.message));
}));

stream.write(input);
}
75 changes: 69 additions & 6 deletions test/parallel/test-zlib-dictionary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// test compression/decompression with dictionary

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

Expand Down Expand Up @@ -32,6 +32,7 @@ function basicDictionaryTest() {
let output = '';
const deflate = zlib.createDeflate({ dictionary: spdyDict });
const inflate = zlib.createInflate({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
inflate.write(chunk);
Expand All @@ -45,9 +46,9 @@ function basicDictionaryTest() {
inflate.end();
});

inflate.on('end', function() {
assert.equal(input, output);
});
inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.end();
Expand All @@ -58,6 +59,7 @@ function deflateResetDictionaryTest() {
let output = '';
const deflate = zlib.createDeflate({ dictionary: spdyDict });
const inflate = zlib.createInflate({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
if (doneReset)
Expand All @@ -72,10 +74,69 @@ function deflateResetDictionaryTest() {
inflate.end();
});

inflate.on('end', function() {
assert.equal(input, output);
inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.flush(function() {
deflate.reset();
doneReset = true;
deflate.write(input);
deflate.end();
});
}

function rawDictionaryTest() {
let output = '';
const deflate = zlib.createDeflateRaw({ dictionary: spdyDict });
const inflate = zlib.createInflateRaw({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
inflate.write(chunk);
});

inflate.on('data', function(chunk) {
output += chunk;
});

deflate.on('end', function() {
inflate.end();
});

inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.end();
}

function deflateRawResetDictionaryTest() {
let doneReset = false;
let output = '';
const deflate = zlib.createDeflateRaw({ dictionary: spdyDict });
const inflate = zlib.createInflateRaw({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
if (doneReset)
inflate.write(chunk);
});

inflate.on('data', function(chunk) {
output += chunk;
});

deflate.on('end', function() {
inflate.end();
});

inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.flush(function() {
deflate.reset();
Expand All @@ -87,3 +148,5 @@ function deflateResetDictionaryTest() {

basicDictionaryTest();
deflateResetDictionaryTest();
rawDictionaryTest();
deflateRawResetDictionaryTest();