Skip to content

Node.js based forum software built for the modern web

fix: prevent uploading thumbs if user does not have upload:image privilege

Barış Soner Uşaklı72defdcf95bb
diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js
index 0f95552e70..e96ee22940 100644
--- a/src/controllers/uploads.js
+++ b/src/controllers/uploads.js
@@ -131,6 +131,10 @@ uploadsController.uploadThumb = async function (req, res) {
}
return await uploadsController.upload(req, res, async (uploadedFile) => {
+ const canUpload = await privileges.global.can('upload:post:image', req.uid);
+ if (!canUpload) {
+ throw new Error('[[error:no-privileges]]');
+ }
if (!uploadedFile.type.match(/image./)) {
throw new Error('[[error:invalid-file]]');
}
diff --git a/src/controllers/write/topics.js b/src/controllers/write/topics.js
index 9399903bb0..cd382668a4 100644
--- a/src/controllers/write/topics.js
+++ b/src/controllers/write/topics.js
@@ -135,7 +135,7 @@ Topics.getThumbs = async (req, res) => {
Topics.addThumb = async (req, res) => {
// todo: move controller logic to src/api/topics.js
- await api.topics._checkThumbPrivileges({ tid: req.params.tid, uid: req.user.uid });
+ await api.topics._checkThumbPrivileges({ tid: req.params.tid, uid: req.uid });
const files = await uploadsController.uploadThumb(req, res); // response is handled here
diff --git a/src/routes/write/topics.js b/src/routes/write/topics.js
index 9fdbca9081..d19fa1cb9c 100644
--- a/src/routes/write/topics.js
+++ b/src/routes/write/topics.js
@@ -38,7 +38,6 @@ module.exports = function () {
setupApiRoute(router, 'post', '/:tid/thumbs', [
middleware.validateFiles,
middleware.uploads.ratelimit,
- ...middlewares,
], controllers.write.topics.addThumb);
diff --git a/test/topics/thumbs.js b/test/topics/thumbs.js
index d4364cd386..124228f254 100644
--- a/test/topics/thumbs.js
+++ b/test/topics/thumbs.js
@@ -18,6 +18,7 @@ const file = require('../../src/file');
const utils = require('../../src/utils');
const helpers = require('../helpers');
+const request = require('../../src/request');
describe('Topic thumbs', () => {
let topicObj;
@@ -315,6 +316,25 @@ describe('Topic thumbs', () => {
assert.strictEqual(response.statusCode, 403);
});
+ it('should fail if user does not have upload:image privilege', async function () {
+ const jar = request.jar();
+ const csrf = await helpers.getCsrfToken(jar);
+ const filePath = path.join(__dirname, '../files/test.png');
+ const { response, body } = await helpers.uploadFile(
+ `${nconf.get('url')}/api/v3/topics/${this.tid}/thumbs`, filePath, {}, jar, csrf
+ );
+ assert.strictEqual(response.statusCode, 403);
+ assert(body && body.status);
+ assert.strictEqual(body.status.message, 'You do not have enough privileges for this action.');
+
+ const {response: response1, body: body1 } = await helpers.uploadFile(
+ `${nconf.get('url')}/api/topic/thumb/upload`, filePath, {}, jar, csrf
+ );
+ assert.strictEqual(response1.statusCode, 403);
+ assert(body1 && body1.status);
+ assert.strictEqual(body1.status.message, 'You do not have enough privileges for this action.');
+ });
+
it('should fail if thumbnails are not enabled', async function () {
meta.config.allowTopicsThumbnail = 0;