Skip to content

Node.js based forum software built for the modern web

feat: add blocked file extensions (blacklist) to complement the existing allowlist (#14229)

Click&Go41675ecf328f
diff --git a/install/data/defaults.json b/install/data/defaults.json
index 9217f8ac07..3e0b845c7f 100644
--- a/install/data/defaults.json
+++ b/install/data/defaults.json
@@ -44,6 +44,7 @@
"allowAccountDelete": 1,
"privateUploads": 0,
"allowedFileExtensions": "png,jpg,bmp,txt,webp,webm,mp4,gif",
+ "blockedFileExtensions": "",
"uploadRateLimitThreshold": 10,
"uploadRateLimitCooldown": 60,
"allowUserHomePage": 1,
diff --git a/public/language/en-GB/admin/settings/uploads.json b/public/language/en-GB/admin/settings/uploads.json
index 9cd7b7d8f2..84e30f535a 100644
--- a/public/language/en-GB/admin/settings/uploads.json
+++ b/public/language/en-GB/admin/settings/uploads.json
@@ -29,8 +29,10 @@
"allow-topic-thumbnails": "Allow users to upload topic thumbnails",
"show-post-uploads-as-thumbnails": "Show post uploads as thumbnails",
"topic-thumb-size": "Topic Thumb Size",
- "allowed-file-extensions": "Allowed File Extensions",
- "allowed-file-extensions-help": "Enter comma-separated list of file extensions here (e.g. <code>pdf,xls,doc</code>). An empty list means all extensions are allowed.",
+ "allowed-file-extensions": "Allowed File Extensions (Whitelist)",
+ "allowed-file-extensions-help": "Comma-separated list of extensions permitted for upload (e.g. <code>pdf,xls,doc</code>). Leave empty to allow all extensions. Note: the blocked list below takes priority — an extension present in both lists will be rejected.",
+ "blocked-file-extensions": "Blocked File Extensions (Blacklist)",
+ "blocked-file-extensions-help": "Comma-separated list of extensions that are always rejected (e.g. <code>html,svg,php</code>). The blocked list takes priority over the allowed list — if an extension appears in both, it will be rejected.",
"upload-limit-threshold": "Rate limit user uploads to:",
"upload-limit-threshold-per-minute": "Per %1 Minute",
"upload-limit-threshold-per-minutes": "Per %1 Minutes",
diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.json
index 5dda7f0e68..95c4931165 100644
--- a/public/language/en-GB/error.json
+++ b/public/language/en-GB/error.json
@@ -150,6 +150,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",
+ "blocked-file-type": "File type not allowed. The extension %1 is explicitly blocked by the administrator.",
"invalid-image-dimensions": "Image dimensions are too big",
"group-name-too-short": "Group name too short",
diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js
index 6452535977..9ba41ac3d2 100644
--- a/src/controllers/uploads.js
+++ b/src/controllers/uploads.js
@@ -176,11 +176,15 @@ uploadsController.uploadFile = async function (uid, uploadedFile) {
}
const allowed = file.allowedExtensions();
+ const blocked = file.blockedExtensions();
const extension = path.extname(uploadedFile.name).toLowerCase();
if (allowed.length > 0 && (!extension || extension === '.' || !allowed.includes(extension))) {
throw new Error(`[[error:invalid-file-type, ${allowed.join('&#44; ')}]]`);
}
+ if (blocked.length > 0 && (!extension || extension === '.' || blocked.includes(extension))) {
+ throw new Error(`[[error:blocked-file-type, ${extension || 'unknown'}]]`);
+ }
return await saveFileToLocal(uid, 'files', uploadedFile);
};
diff --git a/src/file.js b/src/file.js
index 7bfc957a94..bdb1581e44 100644
--- a/src/file.js
+++ b/src/file.js
@@ -59,13 +59,20 @@ file.appendToFileName = function (filename, string) {
};
file.allowedExtensions = function () {
- const meta = require('./meta');
- let allowedExtensions = (meta.config.allowedFileExtensions || '').trim();
- if (!allowedExtensions) {
+ return parseExtensions(require('./meta').config.allowedFileExtensions);
+};
+
+file.blockedExtensions = function () {
+ return parseExtensions(require('./meta').config.blockedFileExtensions);
+};
+
+function parseExtensions(input) {
+ let extensions = (input || '').trim();
+ if (!extensions) {
return [];
}
- allowedExtensions = allowedExtensions.split(',');
- allowedExtensions = allowedExtensions.filter(Boolean).map((extension) => {
+ extensions = extensions.split(',');
+ extensions = extensions.filter(Boolean).map((extension) => {
extension = extension.trim();
if (!extension.startsWith('.')) {
extension = `.${extension}`;
@@ -73,12 +80,12 @@ file.allowedExtensions = function () {
return extension.toLowerCase();
});
- if (allowedExtensions.includes('.jpg') && !allowedExtensions.includes('.jpeg')) {
- allowedExtensions.push('.jpeg');
+ if (extensions.includes('.jpg') && !extensions.includes('.jpeg')) {
+ extensions.push('.jpeg');
}
- return allowedExtensions;
-};
+ return extensions;
+}
file.exists = async function (path) {
try {
diff --git a/src/views/admin/settings/uploads.tpl b/src/views/admin/settings/uploads.tpl
index d2bb25076c..fccb51eb98 100644
--- a/src/views/admin/settings/uploads.tpl
+++ b/src/views/admin/settings/uploads.tpl
@@ -116,6 +116,14 @@
</p>
</div>
+ <div class="mb-3">
+ <label class="form-label" for="blockedFileExtensions">[[admin/settings/uploads:blocked-file-extensions]]</label>
+ <input id="blockedFileExtensions" type="text" class="form-control" value="" data-field="blockedFileExtensions" data-field-type="tagsinput" />
+ <p class="form-text">
+ [[admin/settings/uploads:blocked-file-extensions-help]]
+ </p>
+ </div>
+
<div class="mb-3">
<label class="form-label" for="uploadRateLimitThreshold">[[admin/settings/uploads:upload-limit-threshold]]</label>
<div id="uploadRateLimitThreshold" class="row">