Skip to content

Node.js based forum software built for the modern web

change isPasswordCorrect to return false if user does not have password

Barış Soner Uşaklı25fed0aa8d37
diff --git a/src/socket.io/user.js b/src/socket.io/user.js
index 2df4cfa612..ece95c875e 100644
--- a/src/socket.io/user.js
+++ b/src/socket.io/user.js
@@ -37,6 +37,12 @@ SocketUser.deleteAccount = function (socket, data, callback) {
async.waterfall([
function (next) {
+ user.hasPassword(socket.uid, next);
+ },
+ function (hasPassword, next) {
+ if (!hasPassword) {
+ return next();
+ }
user.isPasswordCorrect(socket.uid, data.password, socket.ip, function (err, ok) {
next(err || (!ok ? new Error('[[error:invalid-password]]') : undefined));
});
diff --git a/src/user/password.js b/src/user/password.js
index 78dc61cec4..bfb65f9cb3 100644
--- a/src/user/password.js
+++ b/src/user/password.js
@@ -24,9 +24,7 @@ module.exports = function (User) {
},
function (_hashedPassword, next) {
hashedPassword = _hashedPassword;
- if (uid && !hashedPassword) {
- return callback(null, true);
- } else if (!hashedPassword) {
+ if (!hashedPassword) {
// Non-existant user, submit fake hash for comparison
hashedPassword = '';
}
@@ -37,17 +35,13 @@ module.exports = function (User) {
function (next) {
Password.compare(password, hashedPassword, next);
},
- ], function (err, ok) {
- if (err) {
- return callback(err);
- }
-
- if (ok) {
- User.auth.clearLoginAttempts(uid);
- }
-
- callback(null, ok);
- });
+ function (ok, next) {
+ if (ok) {
+ User.auth.clearLoginAttempts(uid);
+ }
+ next(null, ok);
+ },
+ ], callback);
};
User.hasPassword = function (uid, callback) {
diff --git a/test/authentication.js b/test/authentication.js
index b90d585971..650fcac74b 100644
--- a/test/authentication.js
+++ b/test/authentication.js
@@ -303,6 +303,19 @@ describe('authentication', function () {
});
});
+ it('should fail to login if user does not have password field in db', function (done) {
+ user.create({ username: 'hasnopassword', email: '[email protected]' }, function (err, uid) {
+ assert.ifError(err);
+ loginUser('hasnopassword', 'doesntmatter', function (err, response, body) {
+ assert.ifError(err);
+ console.log(response.statusCode, body);
+ assert.equal(response.statusCode, 403);
+ assert.equal(body, '[[error:invalid-login-credentials]]');
+ done();
+ });
+ });
+ });
+
it('should fail to login if password is longer than 4096', function (done) {
var longPassword;
for (var i = 0; i < 5000; i++) {