From 219a3a960e2c2e2f5b4df0360ecbed2f761ebe60 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 14 May 2026 12:12:24 +0100 Subject: [PATCH] Add details to zip safety errors --- packages/zip/lib/extract.js | 16 +++++++++++++++- packages/zip/test/zip.test.js | 27 +++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/zip/lib/extract.js b/packages/zip/lib/extract.js index 2e64ccff5..4cc6d8b80 100644 --- a/packages/zip/lib/extract.js +++ b/packages/zip/lib/extract.js @@ -116,14 +116,28 @@ function throwOnSymlinks(entry) { if (symlink) { throw new errors.UnsupportedMediaTypeError({ message: 'Symlinks are not allowed in the zip folder.', + context: 'The zip contains a symlink entry.', + code: 'SYMLINK_NOT_ALLOWED', + errorDetails: { + entryName: entry.fileName, + }, }); } } function throwOnLargeFilenames(entry) { - if (Buffer.byteLength(entry.fileName, 'utf8') >= 254) { + const filenameBytes = Buffer.byteLength(entry.fileName, 'utf8'); + + if (filenameBytes >= 254) { throw new errors.UnsupportedMediaTypeError({ message: 'File names in the zip folder must be shorter than 254 characters.', + context: 'The zip contains a filename that is too long.', + code: 'FILENAME_TOO_LONG', + errorDetails: { + entryName: entry.fileName, + observedBytes: filenameBytes, + limitBytes: 254, + }, }); } } diff --git a/packages/zip/test/zip.test.js b/packages/zip/test/zip.test.js index 3fcb19a53..563e1974b 100644 --- a/packages/zip/test/zip.test.js +++ b/packages/zip/test/zip.test.js @@ -176,7 +176,21 @@ describe('Extract zip', function () { await compress(themeFolder, zipDestination); await assert.rejects( () => extract(zipDestination, unzipDestination), - /File names in the zip folder must be shorter than 254 characters\./, + (err) => { + assert.equal(err.errorType, 'UnsupportedMediaTypeError'); + assert.equal( + err.message, + 'File names in the zip folder must be shorter than 254 characters.', + ); + assert.equal(err.context, 'The zip contains a filename that is too long.'); + assert.equal(err.code, 'FILENAME_TOO_LONG'); + assert.deepEqual(err.errorDetails, { + entryName: longFileName, + observedBytes: 254, + limitBytes: 254, + }); + return true; + }, ); }); @@ -187,7 +201,16 @@ describe('Extract zip', function () { await assert.rejects( () => extract(zipDestination, unzipDestination), - /Symlinks are not allowed in the zip folder\./, + (err) => { + assert.equal(err.errorType, 'UnsupportedMediaTypeError'); + assert.equal(err.message, 'Symlinks are not allowed in the zip folder.'); + assert.equal(err.context, 'The zip contains a symlink entry.'); + assert.equal(err.code, 'SYMLINK_NOT_ALLOWED'); + assert.deepEqual(err.errorDetails, { + entryName: 'themes/test-target', + }); + return true; + }, ); });