From 4c412c048a4cb1e09d90f4a6a3568b0737491e58 Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Tue, 15 Sep 2026 12:46:37 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Stop=20a=20malformed=20zip64=20r?= =?UTF-8?q?ecord=20bypassing=20the=20`error`=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/antelle/node-stream-zip/issues/112 At the moment, a crafted archive can make the central-directory reader throw synchronously from inside an `fs.read` callback, which surfaces as an `uncaughtException` rather than an `error` event. A caller with correct error handling still has its process taken down by a malformed file. `op.complete` is assigned three completion handlers, and only one of them — `readCentralDirectoryComplete` — guards itself. Since all three are reached through the single `op.complete()` call in `readUntilFoundCallback`, guarding that one statement covers every path, and any handler added later. That leaves the try/catch in `readCentralDirectoryComplete` redundant, but removing it would mean re-indenting the whole function, so it stays. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- node_stream_zip.js | 6 +++- release-notes.md | 3 ++ test/tests.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/node_stream_zip.js b/node_stream_zip.js index ce5667e..bfab557 100644 --- a/node_stream_zip.js +++ b/node_stream_zip.js @@ -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; } } diff --git a/release-notes.md b/release-notes.md index 9bb3b10..68c7d86 100644 --- a/release-notes.md +++ b/release-notes.md @@ -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 diff --git a/test/tests.js b/test/tests.js index a814b2e..ec9e3ce 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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;