diff --git a/ghost/core/core/frontend/web/middleware/handle-image-sizes.js b/ghost/core/core/frontend/web/middleware/handle-image-sizes.js index a887cc4342..f565b87e80 100644 --- a/ghost/core/core/frontend/web/middleware/handle-image-sizes.js +++ b/ghost/core/core/frontend/web/middleware/handle-image-sizes.js @@ -139,6 +139,11 @@ module.exports = function handleImageSizes(req, res, next) { if (err.code === 'SHARP_INSTALLATION' || err.code === 'IMAGE_PROCESSING' || err.errorType === 'NoContentError') { return redirectToOriginal(); } + + if (err.errorType === 'NotFoundError') { + return next(); + } + next(err); }); }; diff --git a/ghost/core/test/e2e-frontend/static-files.test.js b/ghost/core/test/e2e-frontend/static-files.test.js new file mode 100644 index 0000000000..1072806b9c --- /dev/null +++ b/ghost/core/test/e2e-frontend/static-files.test.js @@ -0,0 +1,25 @@ +const assert = require('assert/strict'); +const {agentProvider} = require('../utils/e2e-framework'); + +describe('Static files', function () { + let frontendAgent; + let ghostServer; + + before(async function () { + const agents = await agentProvider.getAgentsWithFrontend(); + frontendAgent = agents.frontendAgent; + ghostServer = agents.ghostServer; + }); + + after(async function () { + await ghostServer.stop(); + }); + + it('serves unstyled 404 for non-existing resized + original files', async function () { + const response = await frontendAgent + .get('/content/images/size/w2000/1995/12/daniel.jpg') + .expect(404); + + assert.ok(response.text.includes('NotFoundError: Image not found')); + }); +}); diff --git a/ghost/core/test/unit/frontend/web/middleware/handle-image-sizes.test.js b/ghost/core/test/unit/frontend/web/middleware/handle-image-sizes.test.js index 80777ddd20..348a105af0 100644 --- a/ghost/core/test/unit/frontend/web/middleware/handle-image-sizes.test.js +++ b/ghost/core/test/unit/frontend/web/middleware/handle-image-sizes.test.js @@ -3,6 +3,7 @@ const sinon = require('sinon'); const storage = require('../../../../../core/server/adapters/storage'); const activeTheme = require('../../../../../core/frontend/services/theme-engine/active'); const handleImageSizes = require('../../../../../core/frontend/web/middleware/handle-image-sizes.js'); +const errors = require('@tryghost/errors'); const imageTransform = require('@tryghost/image-transform'); const fakeResBase = { @@ -673,5 +674,36 @@ describe('handleImageSizes middleware', function () { done(); }); }); + + it('goes to next middleware with no error if source and resized image 404', function (done) { + dummyStorage.exists = async function () { + return false; + }; + dummyStorage.read = async function () { + throw new errors.NotFoundError({ + message: 'File not found' + }); + }; + + const fakeReq = { + url: '/size/w1000/2020/02/test.png', + originalUrl: '/2020/02/test.png' + }; + + const fakeRes = { + redirect() { + done(new Error('Should not have called redirect')); + }, + setHeader() {}, + type: function () {} + }; + + handleImageSizes(fakeReq, fakeRes, function next(err) { + if (err) { + return done(err); + } + done(); + }); + }); }); });