Skip to content

Commit

Permalink
fix: #9670 return 4xx errors instead of 5xx on flag routes, when unau…
Browse files Browse the repository at this point in the history
…thenticated or not privileged [breaking]
  • Loading branch information
julianlam committed Jul 21, 2021
1 parent 6c47a06 commit d1959a2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
8 changes: 2 additions & 6 deletions src/controllers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ helpers.buildTerms = function (url, term, query) {
};

helpers.notAllowed = async function (req, res, error) {
const data = await plugins.hooks.fire('filter:helpers.notAllowed', {
req: req,
res: res,
error: error,
});
({ error } = await plugins.hooks.fire('filter:helpers.notAllowed', { req, res, error }));

if (req.loggedIn || req.uid === -1) {
if (res.locals.isAPI) {
Expand All @@ -132,7 +128,7 @@ helpers.notAllowed = async function (req, res, error) {
res.status(403).render('403', {
path: req.path,
loggedIn: req.loggedIn,
error: data.error,
error,
title: '[[global:403.title]]',
});
}
Expand Down
8 changes: 3 additions & 5 deletions src/controllers/mods.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ modsController.flags.list = async function (req, res, next) {
let [,, { filters }] = results;

if (!(isAdminOrGlobalMod || !!moderatedCids.length)) {
return next(new Error('[[error:no-privileges]]'));
return helpers.notAllowed(req, res);
}

if (!isAdminOrGlobalMod && moderatedCids.length) {
Expand Down Expand Up @@ -113,10 +113,8 @@ modsController.flags.detail = async function (req, res, next) {
});
results.privileges = { ...results.privileges[0], ...results.privileges[1] };

if (!results.flagData) {
return next(new Error('[[error:invalid-data]]'));
} else if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) {
return next(new Error('[[error:no-privileges]]'));
if (!results.flagData || (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length))) {
return next(); // 404
}

if (results.flagData.type === 'user') {
Expand Down

0 comments on commit d1959a2

Please sign in to comment.