Skip to content

Commit

Permalink
Added error handling for ENAMETOOLONG import error (#16054)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SimonBackx authored Jan 18, 2023
1 parent 6c2af07 commit 4d54880
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 4 additions & 1 deletion ghost/admin/app/components/modal-import-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.';
Expand Down
11 changes: 11 additions & 0 deletions ghost/admin/app/services/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 21 additions & 5 deletions ghost/core/core/server/data/importer/import-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,13 +173,26 @@ class ImportManager {
* @param {string} filePath
* @returns {Promise<string>} 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;
}

/**
Expand Down

0 comments on commit 4d54880

Please sign in to comment.