Description
I was getting the following stack trace while unzipping a file:
buffer.js:620
throw new RangeError('index out of range');
^RangeError: index out of range
at checkOffset (buffer.js:620:11)
at Buffer.readUInt16LE (buffer.js:666:5)
at /usr/local/lib/node_modules/yauzl/index.js:286:41
at /usr/local/lib/node_modules/yauzl/index.js:474:5
at /usr/local/lib/node_modules/yauzl/node_modules/fd-slicer/index.js:32:7
at FSReqWrap.wrapper as oncomplete
I tracked it down to line 283:
while (i < extraFieldBuffer.length) {
which should instead be at least:
while (i+4 < extraFieldBuffer.length) {
to avoid attempting to read past the end of the buffer at line 284 and 285.
However, I think you should add another check before line 289 to ensure the extraFieldBuffer.copy does not fail due to an invalid size field in the zip file. That is, if it is invalid, you should throw a descriptive exception rather than letting it be handled by another RangeCheck error (which doesn't explain the problem very well to the casual user of a damaged zip file).
(Note that there appears to be no easy way to trap this exception since it occurs within FSReqWrap wrapper).