Skip to content

Node.js based forum software built for the modern web

fix: enforce email:disableEdit in the email change interstitial (#14620)

palmoni5d7e6f921752f
diff --git a/src/controllers/accounts/edit.js b/src/controllers/accounts/edit.js
index 57dc0e035c..06ac495cf7 100644
--- a/src/controllers/accounts/edit.js
+++ b/src/controllers/accounts/edit.js
@@ -94,6 +94,11 @@ editController.email = async function (req, res, next) {
return next();
}
+ const isAdmin = await privileges.admin.can('admin:users', req.uid);
+ if (meta.config['email:disableEdit'] && !isAdmin) {
+ return helpers.notAllowed(req, res);
+ }
+
req.session.returnTo = `/uid/${targetUid}`;
req.session.registration = req.session.registration || {};
req.session.registration.updateEmail = true;
diff --git a/src/user/interstitials.js b/src/user/interstitials.js
index 10ebee2be1..e10d5099f6 100644
--- a/src/user/interstitials.js
+++ b/src/user/interstitials.js
@@ -56,9 +56,10 @@ Interstitials.email = async (data) => {
// Validate and send email confirmation
if (userData.uid) {
const isSelf = parseInt(userData.uid, 10) === parseInt(data.req.uid, 10);
- const [isPasswordCorrect, canEdit, { email: current, 'email:confirmed': confirmed }, { allowed, error }] = await Promise.all([
+ const [isPasswordCorrect, canEdit, isAdmin, { email: current, 'email:confirmed': confirmed }, { allowed, error }] = await Promise.all([
user.isPasswordCorrect(userData.uid, formData.password, data.req.ip),
privileges.users.canEdit(data.req.uid, userData.uid),
+ privileges.admin.can('admin:users', data.req.uid),
user.getUserFields(userData.uid, ['email', 'email:confirmed']),
plugins.hooks.fire('filter:user.saveEmail', {
uid: userData.uid,
@@ -73,6 +74,13 @@ Interstitials.email = async (data) => {
await sleep(2000);
}
+ // Changing or removing an existing email is not allowed if email edits are disabled.
+ // Setting an initial email is still allowed, otherwise users caught by
+ // `requireEmailAddress` would have no way out of the interstitial.
+ if (meta.config['email:disableEdit'] && !isAdmin && current.length && formData.email !== current) {
+ throw new Error('[[error:no-privileges]]');
+ }
+
if (formData.email && formData.email.length) {
if (!allowed || !utils.isEmailValid(formData.email)) {
throw new Error(error);
diff --git a/test/controllers.js b/test/controllers.js
index 60a7a42948..aba0699b6a 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -516,6 +516,93 @@ describe('Controllers', () => {
assert.strictEqual(userData['email:confirmed'], 1);
});
+ describe('when email:disableEdit is enabled', () => {
+ afterEach(() => {
+ meta.config['email:disableEdit'] = 0;
+ });
+
+ async function getCallback(uid) {
+ const result = await user.interstitials.email({
+ userData: { uid: uid, updateEmail: true },
+ req: { uid: uid, session: { id: 0 } },
+ interstitials: [],
+ });
+ return result.interstitials[0].callback;
+ }
+
+ it('should not allow a regular user to change their email', async () => {
+ const username = utils.generateUUID().slice(0, 10);
+ const uid = await user.create({ username, email: `${username}@nodebb.org` }, {
+ emailVerification: 'verify',
+ });
+ const callback = await getCallback(uid);
+ meta.config['email:disableEdit'] = 1;
+
+ await assert.rejects(callback({ uid }, {
+ email: `${username}@nodebb.com`,
+ }), { message: '[[error:no-privileges]]' });
+
+ const userData = await user.getUserData(uid);
+ assert.strictEqual(userData.email, `${username}@nodebb.org`);
+ });
+
+ it('should not allow a regular user to clear their email', async () => {
+ meta.config.requireEmailAddress = 0;
+
+ const username = utils.generateUUID().slice(0, 10);
+ const uid = await user.create({ username, email: `${username}@nodebb.org` }, {
+ emailVerification: 'verify',
+ });
+ const callback = await getCallback(uid);
+ meta.config['email:disableEdit'] = 1;
+
+ await assert.rejects(callback({ uid }, {
+ email: '',
+ }), { message: '[[error:no-privileges]]' });
+
+ const userData = await user.getUserData(uid);
+ assert.strictEqual(userData.email, `${username}@nodebb.org`);
+
+ meta.config.requireEmailAddress = 1;
+ });
+
+ it('should still allow a user without an email to set one', async () => {
+ const username = utils.generateUUID().slice(0, 10);
+ const uid = await user.create({ username });
+ const callback = await getCallback(uid);
+ meta.config['email:disableEdit'] = 1;
+
+ await callback({ uid }, { email: `${username}@nodebb.org` });
+
+ const pending = await user.email.isValidationPending(uid, `${username}@nodebb.org`);
+ assert.strictEqual(pending, true);
+ });
+
+ it('should still allow an admin to change a user\'s email', async () => {
+ const username = utils.generateUUID().slice(0, 10);
+ const uid = await user.create({ username, email: `${username}@nodebb.org` }, {
+ emailVerification: 'verify',
+ });
+ const adminUid = await user.create({ username: utils.generateUUID().slice(0, 10) });
+ await groups.join('administrators', adminUid);
+ meta.config['email:disableEdit'] = 1;
+
+ const result = await user.interstitials.email({
+ userData: { uid: uid, updateEmail: true },
+ req: { uid: adminUid, session: { id: 0 } },
+ interstitials: [],
+ });
+ await result.interstitials[0].callback({ uid }, {
+ email: `${username}@nodebb.com`,
+ });
+
+ const pending = await user.email.isValidationPending(uid, `${username}@nodebb.com`);
+ assert.strictEqual(pending, true);
+
+ await groups.leave('administrators', adminUid);
+ });
+ });
+
describe('blocking access for unconfirmed emails', () => {
let jar;
let token;
@@ -1401,6 +1488,40 @@ describe('Controllers', () => {
const { response, body } = await request.get(`${nconf.get('url')}/api/user/foo/edit/username`, { jar });
assert.equal(response.statusCode, 200);
});
+
+ it('should not render edit/email if email:disableEdit is enabled', async () => {
+ // The route sits behind requirePageReAuth, so the controller is called directly here
+ const editController = require('../src/controllers/accounts/edit');
+ meta.config['email:disableEdit'] = 1;
+
+ let statusCode;
+ const req = {
+ uid: fooUid,
+ loggedIn: true,
+ path: '/user/foo/edit/email',
+ originalUrl: '/api/user/foo/edit/email',
+ params: { userslug: 'foo' },
+ session: {},
+ };
+ const res = {
+ locals: { isAPI: true },
+ status: function (code) {
+ statusCode = code;
+ return this;
+ },
+ json: function () {
+ return this;
+ },
+ };
+ await editController.email(req, res, () => {
+ assert(false, 'next() should not have been called');
+ });
+
+ assert.strictEqual(statusCode, 403);
+ assert.strictEqual(req.session.registration, undefined);
+
+ meta.config['email:disableEdit'] = 0;
+ });
});
describe('account follow page', () => {