Skip to content
Open
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
6 changes: 5 additions & 1 deletion node_stream_zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ const StreamZip = function (config) {
if (buffer.readUInt32LE(bufferPosition) === op.sig) {
op.lastBufferPosition = bufferPosition;
op.lastBytesRead = bytesRead;
op.complete();
try {
op.complete();
} catch (ex) {
that.emit('error', ex);
}
return;
}
}
Expand Down
3 changes: 3 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Release notes
-------------
##### v1.16.1 (2026-09-15)
`-` fix #112: emitting an error instead of throwing on a malformed zip64 record

##### v1.16.0 (2026-07-22)
`+` fixed reading compressed entry size to prevent exploitation reported by @DumDumSu
`*` updated dependencies
Expand Down
85 changes: 85 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,91 @@ module.exports.error['mismatched local header sizes'] = function (test) {
});
};

const endHeaderLength = 22;
const zip64LocatorLength = 20;
const zip64EndHeaderLength = 56;

function endHeaderWithZip64Sentinel() {
const endHeader = Buffer.alloc(endHeaderLength);
endHeader.writeUInt32LE(0x06054b50, 0);
endHeader.writeUInt32LE(0xffffffff, 12);
return endHeader;
}

function zip64Locator() {
const locator = Buffer.alloc(zip64LocatorLength);
locator.writeUInt32LE(0x07064b50, 0);
return locator;
}

module.exports.error['truncated zip64 record'] = function (test) {
test.expect(1);
const file = testPathTmp + 'truncated-zip64-record.zip';
const zip64EndHeader = Buffer.alloc(4);
zip64EndHeader.writeUInt32LE(0x06064b50, 0);

fs.writeFileSync(
file,
Buffer.concat([zip64EndHeader, zip64Locator(), endHeaderWithZip64Sentinel()])
);
const zip = new StreamZip({ file });
zip.on('ready', () => {
test.ok(false, 'Should throw an error');
});
zip.on('error', (err) => {
test.equal(err.message, 'Invalid central directory');
test.done();
});
};

module.exports.error['truncated zip64 locator'] = function (test) {
test.expect(1);
const file = testPathTmp + 'truncated-zip64-locator.zip';
const chunkSize = 1024;
// fewer than zip64LocatorLength bytes in front of the end header, so the locator has to be
// sought in a second, asynchronous window read
const endHeaderOffsetInWindow = 10;
const comment = Buffer.alloc(chunkSize - endHeaderLength - endHeaderOffsetInWindow);
const padding = Buffer.alloc(chunkSize * 2 - endHeaderLength - comment.length);
padding.writeUInt32LE(0x07064b50, chunkSize - 4);
const endHeader = endHeaderWithZip64Sentinel();
endHeader.writeUInt16LE(comment.length, 20);

fs.writeFileSync(file, Buffer.concat([padding, endHeader, comment]));
const zip = new StreamZip({ file, chunkSize });
zip.on('ready', () => {
test.ok(false, 'Should throw an error');
});
zip.on('error', (err) => {
test.equal(err.message, 'Invalid zip64 central directory locator');
test.done();
});
};

module.exports.error['out-of-range zip64 offset'] = function (test) {
test.expect(2);
const file = testPathTmp + 'out-of-range-zip64-offset.zip';
const zip64EndHeader = Buffer.alloc(zip64EndHeaderLength);
zip64EndHeader.writeUInt32LE(0x06064b50, 0);
zip64EndHeader.writeUInt32LE(1, 24);
zip64EndHeader.writeUInt32LE(1, 32);
zip64EndHeader.writeUInt32LE(0x100000, 48);

fs.writeFileSync(
file,
Buffer.concat([zip64EndHeader, zip64Locator(), endHeaderWithZip64Sentinel()])
);
const zip = new StreamZip({ file });
zip.on('ready', () => {
test.ok(false, 'Should throw an error');
});
zip.on('error', (err) => {
test.ok(err);
test.equal(zip.entriesCount, 1, 'the zip64 record was read before the failing entry read');
test.done();
});
};

module.exports.parallel = {};
module.exports.parallel['streaming 100 files'] = function (test) {
const num = 100;
Expand Down