From 4d548801136259101d6480cb92f5aa60752fae96 Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Wed, 18 Jan 2023 13:28:36 +0100 Subject: [PATCH] Added error handling for ENAMETOOLONG import error (#16054) fixes https://github.com/TryGhost/Team/issues/2200 When zipping a folder that contains files with UTF-8 characters in the filename, using the MacOS Archive Utility, the resulting zip will be missing some UTF-8 configuration bit. This breaks the unzipper, causing it to decode the filenames using the wrong encodign. When the file names are long, and become longer than the length allowed by the OS, an ENAMETOOLONG error is thrown. This error is not handled by the importer, and causes the import to fail. This adds a specific check for this error so we can show a clear error message to the user, that helps them to resolve the issue. We are currently unable to fix the issue on our side, because of a lack of well supported zip libraries for node. --- .../app/components/modal-import-content.js | 5 +++- ghost/admin/app/services/ajax.js | 11 ++++++++ .../server/data/importer/import-manager.js | 26 +++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ghost/admin/app/components/modal-import-content.js b/ghost/admin/app/components/modal-import-content.js index b1ce28f838a..f4d33873441 100644 --- a/ghost/admin/app/components/modal-import-content.js +++ b/ghost/admin/app/components/modal-import-content.js @@ -2,6 +2,7 @@ import ModalComponent from 'ghost-admin/components/modal-base'; import ghostPaths from 'ghost-admin/utils/ghost-paths'; import {GENERIC_ERROR_MESSAGE} from 'ghost-admin/services/notifications'; import {computed} from '@ember/object'; +import {getErrorCode} from '../services/ajax'; import {inject} from 'ghost-admin/decorators/inject'; import { isRequestEntityTooLargeError, @@ -90,7 +91,9 @@ export default ModalComponent.extend({ this.notifications.showAPIError(error); } - if (isUnsupportedMediaTypeError(error)) { + if (getErrorCode(error) === 'INVALID_ZIP_FILE_NAME_ENCODING') { + message = 'The uploaded zip could not be read due to a long or invalid file name. Please remove any special characters from the file name, or alternatively try another archiving tool if using MacOS Archive Utility.'; + } else if (isUnsupportedMediaTypeError(error)) { message = 'The file type you uploaded is not supported.'; } else if (isRequestEntityTooLargeError(error)) { message = 'The file you uploaded was larger than the maximum file size your server allows.'; diff --git a/ghost/admin/app/services/ajax.js b/ghost/admin/app/services/ajax.js index 9065c36b2eb..058565fde4d 100644 --- a/ghost/admin/app/services/ajax.js +++ b/ghost/admin/app/services/ajax.js @@ -101,6 +101,17 @@ export function isUnsupportedMediaTypeError(errorOrStatus) { } } +/** + * Returns the code (from the payload) from an error object. + * @returns {string|null} error code + */ +export function getErrorCode(errorOrStatus) { + if (isAjaxError(errorOrStatus) && errorOrStatus.payload && errorOrStatus.payload.errors && Array.isArray(errorOrStatus.payload.errors) && errorOrStatus.payload.errors.length > 0) { + return errorOrStatus.payload.errors[0].code || null; + } + return null; +} + /* Maintenance error */ export class MaintenanceError extends AjaxError { diff --git a/ghost/core/core/server/data/importer/import-manager.js b/ghost/core/core/server/data/importer/import-manager.js index a25ab9a47d6..e9934e55b5a 100644 --- a/ghost/core/core/server/data/importer/import-manager.js +++ b/ghost/core/core/server/data/importer/import-manager.js @@ -32,7 +32,10 @@ const messages = { noContentToImport: 'Zip did not include any content to import.', invalidZipStructure: 'Invalid zip file structure.', invalidZipFileBaseDirectory: 'Invalid zip file: base directory read failed', - zipContainsMultipleDataFormats: 'Zip file contains multiple data formats. Please split up and import separately.' + zipContainsMultipleDataFormats: 'Zip file contains multiple data formats. Please split up and import separately.', + invalidZipFileNameEncoding: 'The uploaded zip could not be read', + invalidZipFileNameEncodingContext: 'The filename was too long or contained invalid characters', + invalidZipFileNameEncodingHelp: 'Remove any special characters from the file name, or alternatively try another archiving tool if using MacOS Archive Utility' }; // Glob levels @@ -170,13 +173,26 @@ class ImportManager { * @param {string} filePath * @returns {Promise} full path to the extracted folder */ - extractZip(filePath) { + async extractZip(filePath) { const tmpDir = path.join(os.tmpdir(), uuid.v4()); this.fileToDelete = tmpDir; - return extract(filePath, tmpDir).then(function () { - return tmpDir; - }); + try { + await extract(filePath, tmpDir); + } catch (err) { + if (err.message.startsWith('ENAMETOOLONG:')) { + // The file was probably zipped with MacOS zip utility. Which doesn't correctly set UTF-8 encoding flag. + // This causes ENAMETOOLONG error on Linux, because the resulting filename length is too long when decoded using the default string encoder. + throw new errors.UnsupportedMediaTypeError({ + message: tpl(messages.invalidZipFileNameEncoding), + context: tpl(messages.invalidZipFileNameEncodingContext), + help: tpl(messages.invalidZipFileNameEncodingHelp), + code: 'INVALID_ZIP_FILE_NAME_ENCODING' + }); + } + throw err; + } + return tmpDir; } /**