2020-10-01 14:11:59 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The middlewares here strictly act to "assert" validity of the incoming
|
|
|
|
|
* payload and throw an error otherwise.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-08 12:00:06 -04:00
|
|
|
const path = require('path');
|
|
|
|
|
const nconf = require('nconf');
|
|
|
|
|
|
2021-07-16 13:44:42 -04:00
|
|
|
const db = require('../database');
|
2020-12-03 07:41:14 -05:00
|
|
|
const file = require('../file');
|
2020-10-01 19:37:13 -04:00
|
|
|
const user = require('../user');
|
2020-10-01 14:11:59 -04:00
|
|
|
const groups = require('../groups');
|
2020-10-01 14:26:34 -04:00
|
|
|
const topics = require('../topics');
|
2020-10-06 14:12:02 -04:00
|
|
|
const posts = require('../posts');
|
2021-12-10 17:16:54 -05:00
|
|
|
const messaging = require('../messaging');
|
2021-08-31 16:27:00 +03:00
|
|
|
const slugify = require('../slugify');
|
2020-10-01 14:26:34 -04:00
|
|
|
|
2020-10-08 12:00:06 -04:00
|
|
|
const helpers = require('./helpers');
|
|
|
|
|
const controllerHelpers = require('../controllers/helpers');
|
2020-10-01 14:11:59 -04:00
|
|
|
|
2020-10-08 13:56:50 -04:00
|
|
|
const Assert = module.exports;
|
|
|
|
|
|
|
|
|
|
Assert.user = helpers.try(async (req, res, next) => {
|
|
|
|
|
if (!await user.exists(req.params.uid)) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-user]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.group = helpers.try(async (req, res, next) => {
|
|
|
|
|
const name = await groups.getGroupNameByGroupSlug(req.params.slug);
|
|
|
|
|
if (!name || !await groups.exists(name)) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-group]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.topic = helpers.try(async (req, res, next) => {
|
|
|
|
|
if (!await topics.exists(req.params.tid)) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-topic]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.post = helpers.try(async (req, res, next) => {
|
|
|
|
|
if (!await posts.exists(req.params.pid)) {
|
2021-01-18 15:31:14 -05:00
|
|
|
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-post]]'));
|
2020-10-08 13:56:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-16 13:44:42 -04:00
|
|
|
Assert.flag = helpers.try(async (req, res, next) => {
|
|
|
|
|
if (!await db.isSortedSetMember('flags:datetime', req.params.flagId)) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-flag]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-08 13:56:50 -04:00
|
|
|
Assert.path = helpers.try(async (req, res, next) => {
|
|
|
|
|
// file: URL support
|
|
|
|
|
if (req.body.path.startsWith('file:///')) {
|
|
|
|
|
req.body.path = new URL(req.body.path).pathname;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 15:04:23 -05:00
|
|
|
// Strip upload_url if found
|
|
|
|
|
if (req.body.path.startsWith(nconf.get('upload_url'))) {
|
|
|
|
|
req.body.path = req.body.path.slice(nconf.get('upload_url').length);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 13:56:50 -04:00
|
|
|
const pathToFile = path.join(nconf.get('upload_path'), req.body.path);
|
|
|
|
|
res.locals.cleanedPath = pathToFile;
|
|
|
|
|
|
2020-12-03 07:41:14 -05:00
|
|
|
// Guard against path traversal
|
2020-10-08 13:56:50 -04:00
|
|
|
if (!pathToFile.startsWith(nconf.get('upload_path'))) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(403, res, new Error('[[error:invalid-path]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 07:41:14 -05:00
|
|
|
if (!await file.exists(pathToFile)) {
|
2020-10-08 13:56:50 -04:00
|
|
|
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:invalid-path]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
2021-08-31 16:27:00 +03:00
|
|
|
|
|
|
|
|
Assert.folderName = helpers.try(async (req, res, next) => {
|
|
|
|
|
const folderName = slugify(path.basename(req.body.folderName.trim()));
|
|
|
|
|
const folderPath = path.join(res.locals.cleanedPath, folderName);
|
|
|
|
|
|
|
|
|
|
// slugify removes invalid characters, folderName may become empty
|
|
|
|
|
if (!folderName) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(403, res, new Error('[[error:invalid-path]]'));
|
|
|
|
|
}
|
|
|
|
|
if (await file.exists(folderPath)) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(403, res, new Error('[[error:folder-exists]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.locals.folderPath = folderPath;
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
2021-12-10 17:16:54 -05:00
|
|
|
|
|
|
|
|
Assert.room = helpers.try(async (req, res, next) => {
|
2021-12-16 10:46:58 -05:00
|
|
|
if (!isFinite(req.params.roomId)) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(400, res, new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 17:16:54 -05:00
|
|
|
const [exists, inRoom] = await Promise.all([
|
|
|
|
|
await messaging.roomExists(req.params.roomId),
|
|
|
|
|
await messaging.isUserInRoom(req.uid, req.params.roomId),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:chat-room-does-not-exist]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!inRoom) {
|
|
|
|
|
return controllerHelpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
2021-12-20 14:32:45 -05:00
|
|
|
|
|
|
|
|
Assert.message = helpers.try(async (req, res, next) => {
|
2021-12-22 14:58:42 -05:00
|
|
|
if (
|
|
|
|
|
!isFinite(req.params.mid) ||
|
|
|
|
|
!(await messaging.messageExists(req.params.mid)) ||
|
|
|
|
|
!(await messaging.canViewMessage(req.params.mid, req.params.roomId, req.uid))
|
|
|
|
|
) {
|
2021-12-20 14:32:45 -05:00
|
|
|
return controllerHelpers.formatApiResponse(400, res, new Error('[[error:invalid-mid]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|