From de5f80e0ff2f3d4c96e60b3c2036d1e01945d517 Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 25 Nov 2026 12:53:21 +0200 Subject: [PATCH] Avoid passing caught exception text directly to res.send on upload errors --- server/controllers/MiscController.js | 4 +++- server/utils/uploadPath.js | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index ae3c01c39..50097e44e 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -63,8 +63,10 @@ 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) + // Send only a known plain-text literal rather than arbitrary exception text. + const message = uploadPath.PLAIN_TEXT_UPLOAD_ERRORS.find((known) => known === error.message) res.type('text/plain') - return res.status(error.statusCode).send(error.message) + return message ? res.status(error.statusCode).send(message) : res.sendStatus(error.statusCode) } return res.sendStatus(500) } diff --git a/server/utils/uploadPath.js b/server/utils/uploadPath.js index 674de942b..1497fda4a 100644 --- a/server/utils/uploadPath.js +++ b/server/utils/uploadPath.js @@ -19,12 +19,16 @@ const UPLOAD_PATH_EXISTS_MESSAGE = 'File already exists' const UPLOAD_LIBRARY_ITEM_EXISTS_PREFIX = 'Library item exists: ' const UPLOAD_DUPLICATE_FILENAME_MESSAGE = 'Duplicate file name in upload' const UPLOAD_FOLDER_NOT_WRITABLE_MESSAGE = 'Cannot write to this library folder' +const UPLOAD_INVALID_PATH_MESSAGE = 'Invalid upload path' +const UPLOAD_INVALID_FILENAME_MESSAGE = 'Invalid file name' +const PLAIN_TEXT_UPLOAD_ERRORS = [UPLOAD_INVALID_PATH_MESSAGE, UPLOAD_INVALID_FILENAME_MESSAGE] module.exports.UPLOAD_ERROR_CODE = UPLOAD_ERROR_CODE module.exports.UPLOAD_PATH_EXISTS_MESSAGE = UPLOAD_PATH_EXISTS_MESSAGE module.exports.UPLOAD_LIBRARY_ITEM_EXISTS_PREFIX = UPLOAD_LIBRARY_ITEM_EXISTS_PREFIX module.exports.UPLOAD_DUPLICATE_FILENAME_MESSAGE = UPLOAD_DUPLICATE_FILENAME_MESSAGE module.exports.UPLOAD_FOLDER_NOT_WRITABLE_MESSAGE = UPLOAD_FOLDER_NOT_WRITABLE_MESSAGE +module.exports.PLAIN_TEXT_UPLOAD_ERRORS = PLAIN_TEXT_UPLOAD_ERRORS /** * Paths of library items this upload would be nested inside (issue #4146). @@ -105,7 +109,7 @@ module.exports.UploadError = UploadError */ async function lookupUploadDirectory(folderPath, outputDirectory) { if (!isSameOrSubPath(folderPath, outputDirectory)) { - throw new UploadError(400, 'Invalid upload path') + throw new UploadError(400, UPLOAD_INVALID_PATH_MESSAGE) } if (await fs.pathExists(outputDirectory)) { return { exists: true } @@ -130,7 +134,7 @@ async function lookupUploadDirectory(folderPath, outputDirectory) { async function assertUploadDestination(folderPath, outputDirectory, filenames) { // The library folder itself is not a valid item directory, even when every name sanitized away. if (!isStrictSubPath(folderPath, outputDirectory)) { - throw new UploadError(400, 'Invalid upload path') + throw new UploadError(400, UPLOAD_INVALID_PATH_MESSAGE) } const existing = await lookupUploadDirectory(folderPath, outputDirectory) if ('libraryItemTitle' in existing) { @@ -143,7 +147,7 @@ async function assertUploadDestination(folderPath, outputDirectory, filenames) { const seen = new Set() for (const name of filenames) { - if (!name) throw new UploadError(400, 'Invalid file name') + if (!name) throw new UploadError(400, UPLOAD_INVALID_FILENAME_MESSAGE) const key = name.toLowerCase() if (seen.has(key)) throw new UploadError(409, UPLOAD_DUPLICATE_FILENAME_MESSAGE, UPLOAD_ERROR_CODE.duplicateFileName) seen.add(key)