Skip to content

Node.js based forum software built for the modern web

4.8 KB
Raw Blame
'use strict';

/**
 * The middlewares here strictly act to "assert" validity of the incoming
 * payload and throw an error otherwise.
 */

const path = require('path');
const nconf = require('nconf');

const file = require('../file');
const user = require('../user');
const groups = require('../groups');
const categories = require('../categories');
const topics = require('../topics');
const posts = require('../posts');
const messaging = require('../messaging');
const flags = require('../flags');
const slugify = require('../slugify');
const utils = require('../utils');
const activitypub = require('../activitypub');

const helpers = require('./helpers');
const controllerHelpers = require('../controllers/helpers');

const Assert = module.exports;

Assert.user = helpers.try(async (req, res, next) => {
	const uid = req.params.uid || res.locals.uid;

	if (
		uid !== -2 && // exposeUid middleware was in chain (means route is local user only) and resolved to fediverse user
		(((utils.isNumber(uid) || activitypub.helpers.isUri(uid)) && await user.exists(uid)) ||
		(uid.indexOf('@') !== -1 && await user.existsBySlug(uid)))
	) {
		return next();
	}

	await controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-user]]'));
});

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.category = helpers.try(async (req, res, next) => {
	if (!await categories.exists(req.params.cid)) {
		return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-category]]'));
	}

	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)) {
		return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-post]]'));
	}

	next();
});

Assert.flag = helpers.try(async (req, res, next) => {
	const canView = await flags.canView(req.params.flagId, req.uid);
	if (!canView) {
		return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-flag]]'));
	}

	next();
});

Assert.path = helpers.try(async (req, res, next) => {
	// Get path from either query or body
	let _path = req.body.path;
	if (!_path && req.query.path) {
		_path = req.query.path;
	}

	// file: URL support
	if (_path.startsWith('file:///')) {
		_path = new URL(_path).pathname;
	}

	// Strip upload_url if found
	if (_path.startsWith(nconf.get('upload_url'))) {
		_path = _path.slice(nconf.get('upload_url').length);
	}

	const pathToFile = path.join(nconf.get('upload_path'), _path);
	res.locals.cleanedPath = pathToFile;

	// Guard against path traversal
	if (!file.isPathInside(nconf.get('upload_path'), pathToFile)) {
		return controllerHelpers.formatApiResponse(403, res, new Error('[[error:invalid-path]]'));
	}

	if (!await file.exists(pathToFile)) {
		return controllerHelpers.formatApiResponse(404, res, new Error('[[error:invalid-path]]'));
	}

	next();
});

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();
});

Assert.room = helpers.try(async (req, res, next) => {
	if (!isFinite(req.params.roomId)) {
		return controllerHelpers.formatApiResponse(400, res, new Error('[[error:invalid-data]]'));
	}

	const [exists, inRoom] = await Promise.all([
		messaging.roomExists(req.params.roomId),
		messaging.isUserInRoom(req.uid, req.params.roomId),
	]);

	if (!exists) {
		return controllerHelpers.formatApiResponse(404, res, new Error('[[error:chat-room-does-not-exist]]'));
	}

	if (!inRoom && !await user.isAdministrator(req.uid)) {
		return controllerHelpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
	}

	next();
});

Assert.message = helpers.try(async (req, res, next) => {
	let roomId;
	if (!req.params.roomId) {
		roomId = await messaging.getMessageField(req.params.mid, 'roomId');
	}

	if (
		!(await messaging.messageExists(req.params.mid)) ||
        !(await messaging.canViewMessage(req.params.mid, roomId || req.params.roomId, req.uid))
	) {
		return controllerHelpers.formatApiResponse(400, res, new Error('[[error:invalid-mid]]'));
	}

	next();
});