From 175ec70d9f0455dd143ce8e0b9c8a49a6a80273e Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:16:20 +0530 Subject: [PATCH] fix: reject zip.compressDir when a file cannot be read yazl emits file errors on the ZipFile, which ZipStream did not listen to, so an unreadable or changing file crashed the process with an unhandled error event instead of rejecting. --- lib/zip/stream.js | 2 ++ test/zip/index.test.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/zip/stream.js b/lib/zip/stream.js index fbbd2c2..c69e254 100644 --- a/lib/zip/stream.js +++ b/lib/zip/stream.js @@ -7,6 +7,8 @@ const TarStream = require('../tar/stream'); class ZipStream extends TarStream { _init() { const zipfile = this._zipfile = new yazl.ZipFile(); + // yazl emits errors reading the files (stat, open, size change) on the ZipFile itself + zipfile.on('error', err => this.emit('error', err)); const stream = zipfile.outputStream; stream.on('end', () => this.push(null)); stream.on('data', chunk => this.push(chunk)); diff --git a/test/zip/index.test.js b/test/zip/index.test.js index 8434870..efa26b4 100644 --- a/test/zip/index.test.js +++ b/test/zip/index.test.js @@ -111,6 +111,20 @@ describe('test/zip/index.test.js', () => { assert(fs.existsSync(destFile)); }); + it('zip.compressDir(dir, destFile) should reject when a file cannot be read', async () => { + if (isWindows || process.getuid() === 0) return; + const sourceDir = path.join(os.tmpdir(), randomUUID()); + fs.mkdirSync(sourceDir); + fs.writeFileSync(path.join(sourceDir, 'locked.txt'), 'locked'); + fs.chmodSync(path.join(sourceDir, 'locked.txt'), 0o000); + destDir = path.join(os.tmpdir(), randomUUID()); + fs.mkdirSync(destDir, { recursive: true }); + await assert.rejects( + compressing.zip.compressDir(sourceDir, path.join(destDir, 'out.zip')), + { code: 'EACCES' } + ); + }); + it('zip.compressDir(dir, destStream, { ignoreBase: true })', async () => { const sourceDir = path.join(__dirname, '..', 'fixtures'); destDir = path.join(os.tmpdir(), randomUUID());