Node.js based forum software built for the modern web
fix: return error instead of redirect for banned uid on token-authenticated requests, closes #12739 (#14639)
palmoni52c8379b7a657
diff --git a/src/middleware/user.js b/src/middleware/user.jsindex 99de735c29..ded06e6799 100644--- a/src/middleware/user.js+++ b/src/middleware/user.js@@ -70,8 +70,9 @@ module.exports = function (middleware) {} else if (user.hasOwnProperty('master') && user.master === true) {// If the token received was a master token, a _uid must also be present for all callsconst body = req.body || {};- if (body.hasOwnProperty('_uid') || req.query.hasOwnProperty('_uid')) {- user.uid = body._uid || req.query._uid;+ const query = req.query || {};+ if (body.hasOwnProperty('_uid') || query.hasOwnProperty('_uid')) {+ user.uid = body._uid || query._uid;delete user.master;return await finishLogin(req, user);}@@ -238,6 +239,10 @@ module.exports = function (middleware) {if (req.loggedIn) {const canLoginIfBanned = await user.bans.canLoginIfBanned(req.uid);if (!canLoginIfBanned) {+ // Token-authenticated requests get an error response instead of a redirect+ if (req.headers.hasOwnProperty('authorization')) {+ return controllers.helpers.notAllowed(req, res, '[[error:user-banned]]');+ }req.logout(() => {res.redirect('/');});diff --git a/test/authentication.js b/test/authentication.jsindex e620dd57ef..40c5aedb36 100644--- a/test/authentication.js+++ b/test/authentication.js@@ -682,5 +682,18 @@ describe('authentication', () => {assert.strictEqual(response.statusCode, 200);assert.strictEqual(body.username, 'apiUserTarget');});++ it('should return an error if master api token _uid points to a banned user', async () => {+ await user.bans.ban(newUid);+ const { response, body } = await helpers.request('get', `/api/categories?_uid=${newUid}`, {+ headers: {+ Authorization: `Bearer ${masterToken}`,+ },+ });++ assert.strictEqual(response.statusCode, 403);+ assert.strictEqual(body.error, '[[error:user-banned]]');+ await user.bans.unban(newUid);+ });});});