loopback icon indicating copy to clipboard operation
loopback copied to clipboard

User.validatePassword throws uncaught exception in User.create

Open cleas opened this issue 11 years ago • 14 comments

related to #941

Creating user with empty password : [email protected] user created , [email protected] application crash due to uncaught exception

User.create({username: 'John', email: '[email protected]', password:''},function(err,user){
//invalid password error should be returned in callback
 });

overriding validatePassword to check for minimum length will also give uncaught exception

see also example https://github.com/cleas/loopback-sandbox https://github.com/cleas/loopback-sandbox/blob/master/server/boot/create-model-instances.js

cleas avatar May 05 '15 23:05 cleas

I tried running your example. I get:

Error: Invalid password:
  ...

Seems like this is fixed in the latest LoopBack. Can you confirm so I close this?

superkhau avatar Jul 07 '15 00:07 superkhau

I tried with loopback 2.18.0 => error is still there. looking at the source nothing changed to User.hashPassword and User.validatePassword. function User.validatePassword can throw exception , this exception is not handled in UserModel.setter.password

cleas avatar Jul 07 '15 11:07 cleas

I'm having this issue as well. I overrode validatePassword in my own User model subclass and used the default implementation as a reference. Now my instance crashes whenever there's an invalid password. Should I just return false instead of throwing an error?

rawrmaan avatar Jul 09 '15 19:07 rawrmaan

I also encountered this, and used the following as a quickfix in user.js

User.observe('before save', function checkEmptyPasswordString(ctx, next) {
  if (ctx.data && ctx.data.password === '') {
    next('Password is empty string');
  }
  next();
});

jlvdh avatar Sep 01 '15 14:09 jlvdh

@cleas Are you still running into issues? Seems to be fixed by #941 now as I can't reproduce the error in your loopback-sandbox for anymore.

superkhau avatar Sep 01 '15 16:09 superkhau

I'm still experiencing the error on Loopback 2.21.0 with the following request:

PUT /user

{"id":140, "password":""}

jlvdh avatar Sep 02 '15 07:09 jlvdh

The error in the loopback-sandbox example is now catched due to the changes in commit:

https://github.com/strongloop/loopback-datasource-juggler/commit/21c0067462f48210f156bb043cb4076e979ff94d "Report deferred exceptions via callback"

But still validatePassword exceptions are not caught in User.create function it self and returned in cb. For example if the create functions are wrapped in a dataSource.autoupdate function, the application crashes. see updated loopback-sandbox example. https://github.com/cleas/loopback-sandbox/commit/fd0fd3369d433ca79e4130fbbb139436d2e8c192

cleas avatar Sep 02 '15 10:09 cleas

@cleas: I believe this should have been resolved in newer versions of LoopBack. Could you please confirm?

gunjpan avatar Aug 16 '16 20:08 gunjpan

Getting this error. while creation of user, it catches if there is any error. but, if i use updateAttributes function, throwing an uncaught exception. Can someone help?

vijay22sai avatar Oct 19 '16 07:10 vijay22sai

When calling User.updateAttribute('password','someinvalidpassword',cb), User.validatePassword() exception is not passed to the callback, thus the application crashes. Using [email protected]

Allan1 avatar May 28 '17 15:05 Allan1

Marking as P1 since many community members are still seeing this issue.

rashmihunt avatar Jun 28 '17 21:06 rashmihunt

might be related to: https://github.com/strongloop/loopback/issues/251

dhmlau avatar Jul 05 '17 21:07 dhmlau

UPDATE: Nevermind, the problem still exists in updateAttribute.

I'm not sure if this is still an issue. Using [email protected], if i set the password to empty string, the error code is INVALID_PASSWORD.

Here is what I've tried: boot/test.js:

module.exports = function(app) {
  var user = app.models.User;
  user.create({email: '[email protected]', password: ''}, (err, result)=> {
    if (err) {
      console.log('err.code = ', err.code);
      console.log(err);
    }
    console.log('result = ', result);
  });
};

Output:

err.code =  INVALID_PASSWORD
{ [Error: Invalid password: ] code: 'INVALID_PASSWORD', statusCode: 422 }

dhmlau avatar Jul 20 '17 14:07 dhmlau

Issue is fixed for all Loopback 2.x.x versions.

loay avatar Jul 21 '17 20:07 loay