Fixed handling empty zip file uploads

fix https://linear.app/tryghost/issue/SLO-102/end-of-central-directory-record-signature-not-found-an-unexpected

- previously, uploading an empty zip would result in a HTTP 500 error
  because yauzl would error and we'd bubble that up as an
  InternalServerError
- now, we catch the specific error message and return a more user
  friendly error
- also includes tests and sample zip file
This commit is contained in:
Daniel Lockyer 2024-05-08 11:05:21 +02:00 committed by Daniel Lockyer
parent 00f42855e3
commit e8e3447f15
3 changed files with 15 additions and 0 deletions

View File

@ -235,6 +235,11 @@ class ImportManager {
help: tpl(messages.invalidZipFileNameEncodingHelp),
code: 'INVALID_ZIP_FILE_NAME_ENCODING'
});
} else if (err.message.includes('end of central directory record signature not found')) { // This comes from Yauzl when the zip is invalid
throw new errors.UnsupportedMediaTypeError({
message: tpl(messages.invalidZipFileNameEncoding),
code: 'INVALID_ZIP_FILE'
});
}
throw err;
}

View File

@ -103,4 +103,14 @@ describe('DB API', function () {
}]
});
});
it('Handles invalid zip file uploads', async function () {
const res = await request.post(localUtils.API.getApiQuery('db/'))
.set('Origin', config.get('url'))
.attach('importfile', 'test/utils/fixtures/import/zips/empty.zip')
.expect('Content-Type', /json/)
.expect(415);
res.body.errors[0].message.should.eql('The uploaded zip could not be read');
});
});