Skip to content

Commit 61167c3

Browse files
addaleaxjasnell
authored andcommitted
zlib: fix gzip member head/buffer boundary issue
Make sure that, even if an `inflate()` call only sees the first few bytes of a following gzip member, all members are decompressed and part of the full output. Adds tests for the special case that the first `inflate()` call receives only the first few bytes of a second gzip member but not the whole header (or even just the magic bytes). This is a backport of #5883 and contains additional changes to make sure that the behaviour on encountering trailing garbage remains the same (namely to silently discard it if one full member has already been decompressed). PR-URL: #5973 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 20bb92f commit 61167c3

3 files changed

+50
-14
lines changed

src/node_zlib.cc

+19-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ enum node_zlib_mode {
4343

4444
#define GZIP_HEADER_ID1 0x1f
4545
#define GZIP_HEADER_ID2 0x8b
46-
#define GZIP_MIN_HEADER_SIZE 10
4746

4847
void InitZlib(v8::Local<v8::Object> target);
4948

@@ -69,7 +68,8 @@ class ZCtx : public AsyncWrap {
6968
windowBits_(0),
7069
write_in_progress_(false),
7170
pending_close_(false),
72-
refs_(0) {
71+
refs_(0),
72+
first_member_ended_(false) {
7373
MakeWeak<ZCtx>(this);
7474
}
7575

@@ -257,17 +257,20 @@ class ZCtx : public AsyncWrap {
257257
ctx->err_ = Z_NEED_DICT;
258258
}
259259
}
260-
while (ctx->strm_.avail_in >= GZIP_MIN_HEADER_SIZE &&
260+
261+
if (ctx->err_ == Z_STREAM_END) {
262+
ctx->first_member_ended_ = true;
263+
}
264+
265+
while (ctx->strm_.avail_in > 0 &&
261266
ctx->mode_ == GUNZIP &&
262-
ctx->err_ == Z_STREAM_END) {
267+
ctx->err_ == Z_STREAM_END &&
268+
ctx->strm_.next_in[0] != 0x00) {
263269
// Bytes remain in input buffer. Perhaps this is another compressed
264270
// member in the same archive, or just trailing garbage.
265-
// Check the header to find out.
266-
if (ctx->strm_.next_in[0] != GZIP_HEADER_ID1 ||
267-
ctx->strm_.next_in[1] != GZIP_HEADER_ID2) {
268-
// Not a valid gzip member
269-
break;
270-
}
271+
// Trailing zero bytes are okay, though, since they are frequently
272+
// used for padding.
273+
271274
Reset(ctx);
272275
ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
273276
}
@@ -302,6 +305,11 @@ class ZCtx : public AsyncWrap {
302305
else
303306
ZCtx::Error(ctx, "Bad dictionary");
304307
return false;
308+
case Z_DATA_ERROR:
309+
if (ctx->first_member_ended_) {
310+
// Silently discard trailing garbage after fully decompressed member.
311+
break;
312+
}
305313
default:
306314
// something else.
307315
ZCtx::Error(ctx, "Zlib error");
@@ -593,6 +601,7 @@ class ZCtx : public AsyncWrap {
593601
bool write_in_progress_;
594602
bool pending_close_;
595603
unsigned int refs_;
604+
bool first_member_ended_;
596605
};
597606

598607

test/parallel/test-zlib-from-concatenated-gzip.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const zlib = require('zlib');
77
const path = require('path');
88
const fs = require('fs');
99

10+
const abcEncoded = zlib.gzipSync('abc');
11+
const defEncoded = zlib.gzipSync('def');
12+
1013
const data = Buffer.concat([
11-
zlib.gzipSync('abc'),
12-
zlib.gzipSync('def')
14+
abcEncoded,
15+
defEncoded
1316
]);
1417

1518
assert.equal(zlib.gunzipSync(data).toString(), 'abcdef');
@@ -38,3 +41,26 @@ fs.createReadStream(pmmFileGz)
3841
assert.deepStrictEqual(Buffer.concat(pmmResultBuffers), pmmExpected,
3942
'result should match original random garbage');
4043
}));
44+
45+
// test that the next gzip member can wrap around the input buffer boundary
46+
[0, 1, 2, 3, 4, defEncoded.length].forEach((offset) => {
47+
const resultBuffers = [];
48+
49+
const unzip = zlib.createGunzip()
50+
.on('error', (err) => {
51+
assert.ifError(err);
52+
})
53+
.on('data', (data) => resultBuffers.push(data))
54+
.on('finish', common.mustCall(() => {
55+
assert.strictEqual(Buffer.concat(resultBuffers).toString(), 'abcdef',
56+
`result should match original input (offset = ${offset})`);
57+
}));
58+
59+
// first write: write "abc" + the first bytes of "def"
60+
unzip.write(Buffer.concat([
61+
abcEncoded, defEncoded.slice(0, offset)
62+
]));
63+
64+
// write remaining bytes of "def"
65+
unzip.end(defEncoded.slice(offset));
66+
});

test/parallel/test-zlib-from-gzip-with-trailing-garbage.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ data = Buffer.concat([
2828
Buffer(10).fill(0)
2929
]);
3030

31-
assert.throws(() => zlib.gunzipSync(data));
31+
assert.equal(zlib.gunzipSync(data).toString(), 'abcdef');
3232

3333
zlib.gunzip(data, common.mustCall((err, result) => {
34-
assert(err);
34+
assert.ifError(err);
35+
assert.equal(result, 'abcdef', 'result should match original string');
3536
}));
3637

3738
// In this case the trailing junk is too short to be a gzip segment

0 commit comments

Comments
 (0)