Node.js based forum software built for the modern web
fix: compare mimetype
Barış Soner Uşaklıe8b52591e5d1
diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.jsonindex afe5f6cb9c..65ef3580ac 100644--- a/public/language/en-GB/error.json+++ b/public/language/en-GB/error.json@@ -151,6 +151,7 @@"invalid-image-type": "Invalid image type. Allowed types are: %1","invalid-image-extension": "Invalid image extension","invalid-file-type": "Invalid file type. Allowed types are: %1",+ "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/uploads.js b/src/controllers/uploads.jsindex 9ba41ac3d2..0f95552e70 100644--- a/src/controllers/uploads.js+++ b/src/controllers/uploads.js@@ -1,6 +1,7 @@'use strict';const path = require('path');+const mime = require('mime').default;const nconf = require('nconf');const validator = require('validator');@@ -47,6 +48,7 @@ uploadsController.upload = async function (req, res, filesIterator) {uploadsController.uploadPost = async function (req, res) {await uploadsController.upload(req, res, async (uploadedFile) => {+ validateUploadedFileMime(uploadedFile);const isImage = uploadedFile.type.match(/image./);if (isImage) {return await uploadAsImage(req, uploadedFile);@@ -132,6 +134,7 @@ uploadsController.uploadThumb = async function (req, res) {if (!uploadedFile.type.match(/image./)) {throw new Error('[[error:invalid-file]]');}+ validateUploadedFileMime(uploadedFile);await image.isFileTypeAllowed(uploadedFile.path);const dimensions = await image.checkDimensions(uploadedFile.path);@@ -211,6 +214,13 @@ async function saveFileToLocal(uid, folder, uploadedFile) {return data.storedFile;}+function validateUploadedFileMime(uploadedFile) {+ const detectedMimeType = mime.getType(uploadedFile.name) || mime.getType(uploadedFile.path);+ if (detectedMimeType && uploadedFile.type && detectedMimeType !== uploadedFile.type) {+ throw new Error('[[error:invalid-mime-type]]');+ }+}+function deleteTempFiles(files) {if (Array.isArray(files)) {files.forEach(fileObj => file.delete(fileObj.path));diff --git a/test/uploads.js b/test/uploads.jsindex a30ead73d3..f8d080a4c9 100644--- a/test/uploads.js+++ b/test/uploads.js@@ -276,6 +276,35 @@ describe('Upload Controllers', () => {assert.strictEqual(body.status.message, 'pngload_buffer: end of stream');});+ it('should fail to upload file if extension and mime type do not match', async () => {+ const oldValue = meta.config.allowedFileExtensions;+ meta.config.allowedFileExtensions = 'png,jpg,bmp,html';++ try {+ const uploadEndPoint = `${nconf.get('url')}/api/post/upload`;+ const form = new FormData();+ const uploadedFile = await fs.readFile(path.join(__dirname, '../test/files/503.html'));+ const blob = new Blob([uploadedFile], { type: 'image/png' });++ form.append('files[]', blob, '503.html');++ const response = await fetch(uploadEndPoint, {+ method: 'post',+ body: form,+ headers: {+ 'x-csrf-token': csrf_token,+ cookie: await jar.getCookieString(uploadEndPoint),+ },+ });+ const body = await response.json();++ assert.strictEqual(response.status, 500);+ assert.strictEqual(body?.status?.message, 'Invalid MIME type');+ } finally {+ meta.config.allowedFileExtensions = oldValue;+ }+ });+it('should fail if file is not an image', (done) => {image.isFileTypeAllowed(path.join(__dirname, '../test/files/notanimage.png'), (err) => {assert.strictEqual(err.message, 'Input file contains unsupported image format');