Skip to content

Node.js based forum software built for the modern web

fix: getEmail to verify email belongs to uid, closes #14512

Barış Soner Uşaklıe5cc6c2b0290
diff --git a/src/api/users.js b/src/api/users.js
index b92ff65970..6f180eafc4 100644
--- a/src/api/users.js
+++ b/src/api/users.js
@@ -475,13 +475,14 @@ usersAPI.listEmails = async (caller, { uid }) => {
};
usersAPI.getEmail = async (caller, { uid, email }) => {
- const [isPrivileged, { showemail }, exists] = await Promise.all([
+ const [isPrivileged, { showemail }, emailUid] = await Promise.all([
user.isPrivileged(caller.uid),
user.getSettings(uid),
- db.isSortedSetMember('email:uid', email.toLowerCase()),
+ db.sortedSetScore('email:uid', String(email).toLowerCase()),
]);
- const isSelf = caller.uid === parseInt(uid, 10);
+ const isSelf = caller.uid === parseInt(uid, 10);
+ const exists = parseInt(emailUid, 10) === parseInt(uid, 10);
return exists && (isSelf || isPrivileged || showemail);
};
diff --git a/test/user/emails.js b/test/user/emails.js
index ef7572e9a6..b1ab247dcd 100644
--- a/test/user/emails.js
+++ b/test/user/emails.js
@@ -182,6 +182,42 @@ describe('email confirmation (v3 api)', () => {
assert.deepStrictEqual(body, JSON.parse('{"status":{"code":"ok","message":"OK"},"response":{"emails":[]}}'));
});
+ it('should not report an email that belongs to another user', async () => {
+ const { body } = await helpers.registerUser({
+ username: 'emaillookupother',
+ password: 'abcdef',
+ email: '[email protected]',
+ gdpr_consent: true,
+ acceptTos: true,
+ });
+ const { uid } = body;
+ const { response } = await helpers.request('get', `/api/v3/users/${userObj.uid}/emails/${encodeURIComponent('[email protected]')}`, {
+ jar,
+ });
+ assert.strictEqual(response.statusCode, 404);
+
+ const { response: response1 } = await helpers.request('get', `/api/v3/users/${uid}/emails/${encodeURIComponent('[email protected]')}`, {
+ jar,
+ });
+ assert.strictEqual(response1.statusCode, 404);
+ });
+
+ it('should return 204 when checking the current user\'s own email', async function () {
+ const email = '[email protected]';
+ const uid = await user.create({
+ username: 'emaillookupself',
+ password: 'abcdef',
+ email: email,
+ }, {
+ emailVerification: 'verify',
+ });
+ const { jar } = await helpers.loginUser('emaillookupself', 'abcdef');
+ const { response } = await helpers.request('get', `/api/v3/users/${uid}/emails/${encodeURIComponent(email)}`, {
+ jar,
+ });
+ assert.strictEqual(response.statusCode, 204);
+ });
+
it('should not allow confirmation if they are not an admin', async function () {
const { response } = await helpers.request('post', `/api/v3/users/${userObj.uid}/emails/${encodeURIComponent(this.emailTestEmail)}/confirm`, {
jar,