From 47bdd3d2dcf5c76d5f4d992b6aba0235a35680bf Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 25 Nov 2026 08:25:13 +0200 Subject: [PATCH] Set text/plain content type for plain-text upload error responses --- server/controllers/MiscController.js | 1 + .../server/controllers/MiscController.test.js | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) 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(