diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index 35cce7f6b..ae3c01c39 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -63,6 +63,7 @@ class MiscController { if (error instanceof uploadPath.UploadError) { Logger.warn(`[MiscController] User "${req.user.username}" upload failed (${error.statusCode}): ${error.message}`) if (error.body) return res.status(error.statusCode).json(error.body) + res.type('text/plain') return res.status(error.statusCode).send(error.message) } return res.sendStatus(500) diff --git a/test/server/controllers/MiscController.test.js b/test/server/controllers/MiscController.test.js index ccd558406..d5c0831df 100644 --- a/test/server/controllers/MiscController.test.js +++ b/test/server/controllers/MiscController.test.js @@ -51,7 +51,12 @@ describe('upload path collisions', () => { function mockRes() { return { statusCode: 200, + contentType: undefined, body: undefined, + type(contentType) { + this.contentType = contentType + return this + }, status(code) { this.statusCode = code return this @@ -534,6 +539,23 @@ describe('upload path collisions', () => { expect(await pathExists(Path.join(folder.path, 'NewAuthor'))).to.equal(false) }) + it('refuses a file name that sanitizes to empty as plain text', async () => { + const res = mockRes() + await MiscController.handleUpload( + uploadRequest({ + files: { + 0: fakeUploadFile('...', 'new-audio') + } + }), + res + ) + + expect(res.statusCode).to.equal(400) + expect(res.contentType).to.equal('text/plain') + expect(res.body).to.equal('Invalid file name') + expect(await pathExists(Path.join(folder.path, 'Author A'))).to.equal(false) + }) + it('drops an author name that sanitizes to empty', async () => { const res = mockRes() await MiscController.handleUpload(