Skip to content

Commit 4971111

Browse files
author
Yuta Imaya
committed
fix issues#45: check code length in decode huffman code table
1 parent deca970 commit 4971111

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

‎src/rawinflate.js

+4
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ Zlib.RawInflate.prototype.readCodeByTable = function(table) {
333333
codeWithLength = codeTable[bitsbuf & ((1 << maxCodeLength) - 1)];
334334
codeLength = codeWithLength >>> 16;
335335

336+
if (codeLength > bitsbuflen) {
337+
throw new ('invalid code length: ' + codeLength);
338+
}
339+
336340
this.bitsbuf = bitsbuf >> codeLength;
337341
this.bitsbuflen = bitsbuflen - codeLength;
338342
this.ip = ip;

‎test/nodejs/node-test.js

+19
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,25 @@ describe("node inflate and deflate", function() {
182182

183183
inflateOnlyTest(compressed, plain);
184184
});
185+
186+
it('issue#45 Infinite loop when decoding invalid zip file', function() {
187+
var data = new Buffer([
188+
0x08, 0x1D, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x61,
189+
0x2f, 0x62, 0x6c, 0x61, 0x68, 0x2e, 0x6a, 0x73,
190+
0x55, 0x58, 0x0c, 0x00, 0x14, 0x2c, 0xdb, 0x55,
191+
0xa9, 0x98, 0x85, 0x55, 0xf5, 0x01, 0x14, 0x00,
192+
0x2b, 0x4b, 0x2c, 0x52, 0x28, 0x4e, 0x2d, 0x2a,
193+
0x4b, 0x2d, 0x52, 0xb0, 0x55, 0xc8, 0x28, 0x29,
194+
0x29, 0xd0, 0x4b,
195+
]);
196+
197+
assert.throws(
198+
() => {
199+
Zlib.inflateSync(data);
200+
},
201+
Error
202+
);
203+
})
185204
});
186205

187206
// inflate test

0 commit comments

Comments
 (0)