Skip to content

Commit 54a5287

Browse files
addaleaxbnoordhuis
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. This change also modifies behaviour for trailing garbage: If there is trailing garbage which happens to start with the gzip magic bytes, it is no longer discarded but rather throws an error, since we cannot reliably tell random garbage from a valid gzip member anyway and have to try and decompress it. (Null byte padding is not affected, since it has been pointed out at various occasions that such padding is normal and discarded by `gzip(1)`, too.) 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). PR-URL: #5883 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 33c27f8 commit 54a5287

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/node_zlib.cc

Lines changed: 7 additions & 9 deletions
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

@@ -257,17 +256,16 @@ class ZCtx : public AsyncWrap {
257256
ctx->err_ = Z_NEED_DICT;
258257
}
259258
}
260-
while (ctx->strm_.avail_in >= GZIP_MIN_HEADER_SIZE &&
259+
260+
while (ctx->strm_.avail_in > 0 &&
261261
ctx->mode_ == GUNZIP &&
262-
ctx->err_ == Z_STREAM_END) {
262+
ctx->err_ == Z_STREAM_END &&
263+
ctx->strm_.next_in[0] != 0x00) {
263264
// Bytes remain in input buffer. Perhaps this is another compressed
264265
// 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-
}
266+
// Trailing zero bytes are okay, though, since they are frequently
267+
// used for padding.
268+
271269
Reset(ctx);
272270
ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
273271
}

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

Lines changed: 28 additions & 2 deletions
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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ data = Buffer.concat([
4242
Buffer([0x1f, 0x8b, 0xff, 0xff])
4343
]);
4444

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

4747
zlib.gunzip(data, common.mustCall((err, result) => {
48-
assert.ifError(err);
49-
assert.equal(result, 'abcdef', 'result should match original string');
48+
assert(err);
5049
}));

0 commit comments

Comments
 (0)