Node.js based forum software built for the modern web
fix(auth): ignore client-supplied uid in registration flow
Julian Lam376d14abb7ff
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.jsindex 051b6cb997..a0997b726b 100644--- a/src/controllers/authentication.js+++ b/src/controllers/authentication.js@@ -21,6 +21,13 @@ const sockets = require('../socket.io');const authenticationController = module.exports;async function registerAndLoginUser(req, res, userData) {+ // Never trust a client-supplied uid; only the session's own uid is valid+ if (req.uid) {+ userData.uid = req.uid;+ } else {+ delete userData.uid;+ }+if (!userData.hasOwnProperty('email')) {userData.updateEmail = true;}diff --git a/src/user/interstitials.js b/src/user/interstitials.jsindex e10d5099f6..ea750e2894 100644--- a/src/user/interstitials.js+++ b/src/user/interstitials.js@@ -119,9 +119,15 @@ Interstitials.email = async (data) => {throw new Error('[[error:invalid-email]]');}- if (current.length && (!hasPassword || (hasPassword && isPasswordCorrect))) {- // User explicitly clearing their email- await user.email.remove(userData.uid, isSelf ? data.req.session.id : null);+ if (current.length) {+ if (!isSelf) {+ throw new Error('[[error:no-privileges]]');+ }++ if (!hasPassword || isPasswordCorrect) {+ // User explicitly clearing their email+ await user.email.remove(userData.uid, data.req.session.id);+ }}}} else {diff --git a/test/authentication.js b/test/authentication.jsindex e73504597c..e46957a278 100644--- a/test/authentication.js+++ b/test/authentication.js@@ -702,6 +702,48 @@ describe('authentication', () => {assert.strictEqual(redirect.searchParams.get('register'), '[[register:invite.error-invite-only]]');assert.strictEqual(await user.getUidByUsername(username), null);});++ it('should ignore a client-supplied uid in the registration body', async () => {+ meta.config.requireEmailAddress = 0;++ // Victim: an account with a confirmed email and no password (e.g. SSO-created)+ const suffix = utils.generateUUID().slice(0, 8);+ const victimUid = await user.create({+ username: `victim${suffix}`,+ email: `victim${suffix}@example.org`,+ }, { emailVerification: 'verify' });++ const username = `mallory${suffix}`;+ const jar = request.jar();+ const { response, body } = await helpers.request('post', '/register', {+ jar,+ body: {+ username,+ password: 'pending-registration-password',+ 'password-confirm': 'pending-registration-password',+ uid: victimUid,+ },+ });+ assert.strictEqual(response.status, 200);+ assert.strictEqual(body.next, `${nconf.get('relative_path')}/register/complete`);++ const formData = new FormData();+ formData.append('email', '');+ formData.append('gdpr_agree_data', 'on');+ formData.append('gdpr_agree_email', 'on');+ const { response: res2 } = await helpers.request('post', '/register/complete', {+ jar,+ body: formData,+ redirect: 'manual',+ });+ assert.strictEqual(res2.status, 302);++ const victimData = await user.getUserFields(victimUid, ['email', 'email:confirmed']);+ assert.strictEqual(victimData.email, `victim${suffix}@example.org`);+ assert.strictEqual(victimData['email:confirmed'], 1);++ meta.config.requireEmailAddress = 1;+ });});it('should fail to register if username is falsy or too short', async () => {diff --git a/test/controllers.js b/test/controllers.jsindex 6308627c41..ead5404d5d 100644--- a/test/controllers.js+++ b/test/controllers.js@@ -465,6 +465,31 @@ describe('Controllers', () => {meta.config.requireEmailAddress = 1;});+ it('should not remove the email of a user other than the caller', async () => {+ meta.config.requireEmailAddress = 0;++ // Passwordless account with a confirmed email (e.g. SSO-created)+ const uid = await user.create({+ username: 'interstiuser-guest-uid', email: '[email protected]',+ }, { emailVerification: 'verify' });++ const result = await user.interstitials.email({+ userData: { uid: uid, updateEmail: true },+ req: { uid: 0, session: { id: 0 } },+ interstitials: [],+ });++ await assert.rejects(result.interstitials[0].callback({ uid }, {+ email: '',+ }), { message: '[[error:no-privileges]]' });++ const userData = await user.getUserData(uid);+ assert.strictEqual(userData.email, '[email protected]');+ assert.strictEqual(userData['email:confirmed'], 1);++ meta.config.requireEmailAddress = 1;+ });+it('should require a password (if one is set) for email change', async () => {try {const [username, password] = [utils.generateUUID().slice(0, 10), utils.generateUUID()];