Node.js based forum software built for the modern web
fix(security): harden login timing, user status, and groupTitle
Julian Lamd271e20cc6c0
diff --git a/public/openapi/write/users/uid.yaml b/public/openapi/write/users/uid.yamlindex c8cbcb61b7..30d3033ce0 100644--- a/public/openapi/write/users/uid.yaml+++ b/public/openapi/write/users/uid.yaml@@ -93,7 +93,7 @@ put:example: 'Toronto, Canada'groupTitle:type: string- example: '["administrators","Staff"]'+ example: '["administrators"]'birthday:type: stringdescription: A birthdate given in an ISO format parseable by the Date objectdiff --git a/src/password.js b/src/password.jsindex 6f9617f6ab..8f95de15cc 100644--- a/src/password.js+++ b/src/password.js@@ -2,6 +2,7 @@const path = require('path');const crypto = require('crypto');+const nconf = require('nconf');const workerpool = require('workerpool');const pool = workerpool.pool(@@ -29,9 +30,7 @@ async function getFakeHash() {if (fakeHashCache) {return fakeHashCache;}- const length = 18;- fakeHashCache = crypto.randomBytes(Math.ceil(length / 2))- .toString('hex').slice(0, length);+ fakeHashCache = await exports.hash(nconf.get('bcrypt_rounds') || 12, crypto.randomBytes(16).toString('hex'));return fakeHashCache;}diff --git a/src/routes/write/users.js b/src/routes/write/users.jsindex 6bb7b3b4dc..02ad00fb6a 100644--- a/src/routes/write/users.js+++ b/src/routes/write/users.js@@ -42,8 +42,8 @@ function authenticatedRoutes() {setupApiRoute(router, 'delete', '/:uid/content', [...middlewares, middleware.assert.user], controllers.write.users.deleteContent);setupApiRoute(router, 'delete', '/:uid/account', [...middlewares, middleware.assert.user], controllers.write.users.deleteAccount);- setupApiRoute(router, 'get', '/:uid/status', [], controllers.write.users.getStatus);- setupApiRoute(router, 'head', '/:uid/status/:status', [], controllers.write.users.checkStatus);+ setupApiRoute(router, 'get', '/:uid/status', [middleware.assert.user, middleware.canViewUsers], controllers.write.users.getStatus);+ setupApiRoute(router, 'head', '/:uid/status/:status', [middleware.assert.user, middleware.canViewUsers], controllers.write.users.checkStatus);setupApiRoute(router, 'get', '/:uid/chat', [...middlewares], controllers.write.users.getPrivateRoomId);diff --git a/src/user/profile.js b/src/user/profile.jsindex f9ebd85f7e..58a84ccefc 100644--- a/src/user/profile.js+++ b/src/user/profile.js@@ -84,7 +84,7 @@ module.exports = function (User) {await isSignatureValid(callerUid, data);isFullnameValid(data);isBirthdayValid(data);- isGroupTitleValid(data);+ await isGroupTitleValid(data);await validateCustomFields(data);}@@ -255,7 +255,7 @@ module.exports = function (User) {}}- function isGroupTitleValid(data) {+ async function isGroupTitleValid(data) {function checkTitle(title) {if (title === 'registered-users' || groups.isPrivilegeGroup(title)) {throw new Error('[[error:invalid-group-title]]');@@ -277,6 +277,12 @@ module.exports = function (User) {}if (!meta.config.allowMultipleBadges && groupTitles.length > 1) {data.groupTitle = JSON.stringify(groupTitles[0]);+ groupTitles = [groupTitles[0]];+ }+ // Ensure the user is actually a member of each selected group (prevents badge spoofing via the API)+ const memberships = await groups.isMemberOfGroups(data.uid, groupTitles);+ if (memberships.includes(false)) {+ throw new Error('[[error:invalid-group-title]]');}}diff --git a/test/user.js b/test/user.jsindex a1e63378aa..25fb9ff3c8 100644--- a/test/user.js+++ b/test/user.js@@ -738,6 +738,7 @@ describe('User', () => {describe('.updateProfile()', () => {let uid;+ let group;it('should update a user\'s profile', async () => {uid = await User.create({@@ -746,12 +747,15 @@ describe('User', () => {emailVerification: 'verify',});+ group = await groups.create({ name: 'updateProfileGroup' });+ await groups.join(group.name, uid);+const data = {uid: uid,username: 'updatedUserName',email: '[email protected]',fullname: 'updatedFullname',- groupTitle: 'testGroup',+ groupTitle: group.name,birthday: '01/01/1980',signature: 'nodebb is good',password: '123456',@@ -802,6 +806,19 @@ describe('User', () => {const confirmSent = await User.email.isValidationPending(uid, '[email protected]');assert.strictEqual(confirmSent, true);});++ it('should reject a groupTitle for a group the user is not a member of', async () => {+ const otherGroup = await groups.create({ name: 'notAMemberGroup' });+ await assert.rejects(+ apiUser.update({ uid: uid }, { uid: uid, groupTitle: otherGroup.name }),+ { message: '[[error:invalid-group-title]]' },+ );+ await groups.destroy(otherGroup.name);+ });++ after(async () => {+ await groups.destroy(group.name);+ });});it('should change a user\'s password', async () => {