Node.js based forum software built for the modern web
fix(auth): prevent user account enumeration (via email) on login
Julian Lamce1c358defc7
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.jsindex a0997b726b..749abb47cb 100644--- a/src/controllers/authentication.js+++ b/src/controllers/authentication.js@@ -256,11 +256,8 @@ authenticationController.login = async (req, res, next) => {const isUsernameLogin = loginWith.includes('username') && !validator.isEmail(req.body.username);if (isEmailLogin) {const username = await user.getUsernameByEmail(req.body.username);- if (username !== '[[global:guest]]') {- req.body.username = username;- } else {- return errorHandler(req, res, '[[error:invalid-email]]', 400);- }+ // if no username, pass in dummy username for login purposes, otherwise oracle 🔮 if returning early here+ req.body.username = (username && username !== '[[global:guest]]') ? username : '__dummy__';}if (isEmailLogin || isUsernameLogin) {continueLogin(strategy, req, res, next);diff --git a/test/authentication.js b/test/authentication.jsindex e46957a278..ebce1709d4 100644--- a/test/authentication.js+++ b/test/authentication.js@@ -275,11 +275,36 @@ describe('authentication', () => {meta.config.allowLoginWith = 'username-email';const uid = await user.create({ username: '2nduser', password: '2ndpassword', email: '[email protected]' });const { response, body } = await helpers.loginUser('[email protected]', '2ndpassword');- assert.strictEqual(response.statusCode, 400);- assert.strictEqual(body, '[[error:invalid-email]]');+ // Unconfirmed emails are not in the email:uid index, so they fall through the+ // same non-existent-user path as any other bad credential (403, not 400).+ assert.strictEqual(response.statusCode, 403);+ assert.strictEqual(body, '[[error:invalid-login-credentials]]');meta.config.allowLoginWith = oldValue;});+ it('should not leak whether an email is registered (email enumeration)', async () => {+ const oldValue = meta.config.allowLoginWith;+ meta.config.allowLoginWith = 'username-email';+ try {+ // Unregistered email+ const { response: emailRes, body: emailBody } = await helpers.loginUser('[email protected]', 'SomePassword123');+ // Non-existent username+ const { response: userRes, body: userBody } = await helpers.loginUser('unregistered-enum-user', 'SomePassword123');+ // Registered email with a wrong password+ const { response: regRes, body: regBody } = await helpers.loginUser('[email protected]', 'WrongPassword123');++ // All three must be indistinguishable: same status, same message+ assert.strictEqual(emailRes.statusCode, 403);+ assert.strictEqual(emailBody, '[[error:invalid-login-credentials]]');+ assert.strictEqual(userRes.statusCode, 403);+ assert.strictEqual(userBody, '[[error:invalid-login-credentials]]');+ assert.strictEqual(regRes.statusCode, 403);+ assert.strictEqual(regBody, '[[error:invalid-login-credentials]]');+ } finally {+ meta.config.allowLoginWith = oldValue;+ }+ });+it('should fail to create user if username is too short', async () => {const { response, body } = await helpers.registerUser({username: 'a',