Node.js based forum software built for the modern web
refactor: check detected extension if it's different than file extension
Barış Soner Uşaklı98b3bb7bee30
diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.jsonindex 0e23a05cb9..80397b92bf 100644--- a/public/language/en-GB/error.json+++ b/public/language/en-GB/error.json@@ -148,9 +148,9 @@"already-deleting": "Already deleting","invalid-image": "Invalid image",- "invalid-image-type": "Invalid image type. Allowed types are: %1",+ "invalid-image-type": "Invalid image type: %1. Allowed types are: %2","invalid-image-extension": "Invalid image extension",- "invalid-file-type": "Invalid file type. Allowed types are: %1",+ "invalid-file-type": "Invalid file type %1. Allowed types are: %2","invalid-mime-type": "Invalid MIME type","blocked-file-type": "File type not allowed. The extension %1 is explicitly blocked by the administrator.","invalid-image-dimensions": "Image dimensions are too big",diff --git a/src/controllers/admin/uploads.js b/src/controllers/admin/uploads.jsindex 21be9c441b..abec2e1c79 100644--- a/src/controllers/admin/uploads.js+++ b/src/controllers/admin/uploads.js@@ -274,7 +274,7 @@ async function upload(name, req, res, next) {async function validateUpload(uploadedFile, allowedTypes) {if (!allowedTypes.includes(uploadedFile.type)) {file.delete(uploadedFile.path);- throw new Error(`[[error:invalid-image-type, ${allowedTypes.join(', ')}]]`);+ throw new Error(`[[error:invalid-image-type, ${uploadedFile.type}, ${allowedTypes.join(', ')}]]`);}}diff --git a/src/controllers/uploads.js b/src/controllers/uploads.jsindex 0a65b40000..7a8fb4202f 100644--- a/src/controllers/uploads.js+++ b/src/controllers/uploads.js@@ -2,6 +2,7 @@const path = require('path');const { fileTypeFromFile } = require('file-type');+const mime = require('mime').default;const nconf = require('nconf');const user = require('../user');@@ -205,17 +206,23 @@ async function saveFileToLocal(uid, folder, uploadedFile) {}async function validateFileExtension(uploadedFile) {- const detected = await fileTypeFromFile(uploadedFile.path);const allowed = file.allowedExtensions();const blocked = file.blockedExtensions();-- // if we cant detect with magic bytes fall back to file extension- const extension = detected ? `.${detected.ext}` : path.extname(uploadedFile.name).toLowerCase();- if (allowed.length > 0 && (!extension || extension === '.' || !allowed.includes(extension))) {- throw new Error(`[[error:invalid-file-type, ${allowed.join(', ')}]]`);+ function validateExtension(extension) {+ if (allowed.length > 0 && (!extension || extension === '.' || !allowed.includes(extension))) {+ throw new Error(`[[error:invalid-file-type, ${mime.getType(extension)}, ${allowed.join(', ')}]]`);+ }+ if (blocked.length > 0 && (!extension || extension === '.' || blocked.includes(extension))) {+ throw new Error(`[[error:blocked-file-type, ${mime.getType(extension) || 'unknown'}]]`);+ }}- if (blocked.length > 0 && (!extension || extension === '.' || blocked.includes(extension))) {- throw new Error(`[[error:blocked-file-type, ${extension || 'unknown'}]]`);+ const extension = path.extname(uploadedFile.name).toLowerCase();+ validateExtension(extension);++ const detected = await fileTypeFromFile(uploadedFile.path);+ const detectedExtension = detected ? `.${detected.ext}` : '';+ if (detectedExtension && detectedExtension !== extension) {+ validateExtension(detectedExtension);}}diff --git a/test/uploads.js b/test/uploads.jsindex 02da8c3627..da384bd489 100644--- a/test/uploads.js+++ b/test/uploads.js@@ -298,7 +298,7 @@ describe('Upload Controllers', () => {const body = await response.json();assert.strictEqual(response.status, 500);- assert.strictEqual(body?.status?.message, 'Invalid file type. Allowed types are: .png, .jpg, .bmp, .jpeg');+ assert.strictEqual(body?.status?.message, 'Invalid file type text/html. Allowed types are: .png, .jpg, .bmp, .jpeg');} finally {meta.config.allowedFileExtensions = oldValue;}@@ -436,7 +436,7 @@ describe('Upload Controllers', () => {csrf_token);assert.strictEqual(response.statusCode, 500);- assert.equal(body.error, '[[error:invalid-image-type, image/png, image/jpeg, image/pjpeg, image/jpg, image/gif, image/svg+xml]]');+ assert.equal(body.error, '[[error:invalid-image-type, text/html, image/png, image/jpeg, image/pjpeg, image/jpg, image/gif, image/svg+xml]]');});it('should fail to upload category image with invalid json params', async () => {