You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Password validators operate on hashed password, making length validators useless #251
Modifying the User model in the following way does not trigger a validation error when attempting to create a user with an invalid password:
varUser=app.models.User;User.validatesLengthOf('password',{min: 6,message: {min: 'Password is too short'}});varu=newUser({email: "foo@bar.com",password: "short"});assert(u.isValid())// Error
Tracing it with node-inspector shows that the length is being compared to the hashed password, which is obviously always quite long.
Thanks for the bug report. We probably have to implement a custom validator for the password field. The length is lost as soon as the value is set. I'm thinking of moving the validation to the setter to prevent ever setting an invalid password. The issue is that the validator doesn't support this.
I'm not too familiar with the internals of loopback yet, but for this bug, I'm wondering if this will work for a fix in user.js
I tried to put some code in the UserMode.beforeRemote('create',...) method to do the hashing but that didn't work.
UserModel.setter.password=function(plain){//var salt = bcrypt.genSaltSync(this.constructor.settings.saltWorkFactor || SALT_WORK_FACTOR);//this.$password = bcrypt.hashSync(plain, salt);this.$password=plain;};UserModel.beforeCreate=function(next,user){varpassword=user.password;console.log("user.password before hash = ",password);varsalt=bcrypt.genSaltSync(this.constructor.settings.saltWorkFactor||SALT_WORK_FACTOR);user.password=bcrypt.hashSync(user.password,salt);console.log("user.password after hash = ",password);next();};// Make sure emailVerified is not set by creationUserModel.beforeRemote('create',function(ctx,user,next){varbody=ctx.req.body;if(body&&body.emailVerified){body.emailVerified=false;}next();});
Actually, what I posted isn't going to work because if I call UserModel.beforeCreate here, and then I call beforeCreate in a custom Model that extends LoopBack's built-in User model and user.js file, then the createModel defined in Loopback's user.js file won't ever get called.
So for a workaround, I just did the following:
UserModel.setter.password=function(plain){//var salt = bcrypt.genSaltSync(this.constructor.settings.saltWorkFactor || SALT_WORK_FACTOR);//this.$password = bcrypt.hashSync(plain, salt);this.$password=plain;};
And then in my file that extends the built-in User model, I did this:
UserModel.beforeCreate=function(next,user){varpassword=user.password;console.log("user.password before hash = ",password);varsalt=bcrypt.genSaltSync(this.constructor.settings.saltWorkFactor||SALT_WORK_FACTOR);user.password=bcrypt.hashSync(user.password,salt);console.log("user.password after hash = ",password);next();};
Works for a temporary workaround solution for now...
@akapaul's suggestion works for me, except I need access to the model's other data inside this method to make sure they aren't using something like their name for their password. Suggestions for accessing this data? this looks to be set to the server instance, not the model.
@STRML As far as I can understand following above discussion, per @bajtos
comments above #251 (comment), there are couple of possible workarounds - override validatePassword(), use 'persist' hook.
"
I see we landed #941 which allows users to implement a workaround. Is that good enough?
…….
Sounds to me like now that persist is in, then there just needs to be a fix implemented that knows if you're trying to validate 'password', uses the right hook, then this can be closed. But it shouldn't be closed just with a workaround.
In general, we need to have a better way to handle calculated/transformed properties such as password upon 'read/write`. I guess we should open an issue for the broader feature.
IMO, we should keep this issue open. As I understand it, #3468 will lay groundwork necessary to fix password validators. With this foundation in place, we will still need to rework the way how password are handled in LoopBack.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
Activity
ritch commentedon May 8, 2014
Thanks for the bug report. We probably have to implement a custom validator for the password field. The length is lost as soon as the value is set. I'm thinking of moving the validation to the setter to prevent ever setting an invalid password. The issue is that the validator doesn't support this.
bajtos commentedon May 28, 2014
Mailing-list discussion: https://groups.google.com/forum/#!topic/loopbackjs/SdWN5n3hMzw
jonathanxie commentedon Dec 20, 2014
I'm not too familiar with the internals of loopback yet, but for this bug, I'm wondering if this will work for a fix in
user.jsI tried to put some code in the UserMode.beforeRemote('create',...) method to do the hashing but that didn't work.
jonathanxie commentedon Dec 20, 2014
Actually, what I posted isn't going to work because if I call UserModel.beforeCreate here, and then I call beforeCreate in a custom Model that extends LoopBack's built-in User model and user.js file, then the createModel defined in Loopback's user.js file won't ever get called.
So for a workaround, I just did the following:
And then in my file that extends the built-in User model, I did this:
Works for a temporary workaround solution for now...
Allow User.hashPassword/validatePassword to be overridden
Change the password from setter to beforeSave
42 remaining items
kooliokey commentedon May 18, 2017
@akapaul's suggestion works for me, except I need access to the model's other data inside this method to make sure they aren't using something like their name for their password. Suggestions for accessing this data?
thislooks to be set to the server instance, not the model.rashmihunt commentedon Jun 28, 2017
@STRML As per the above conversation, looks like there is solution for originally reported problem. We will be closing this soon. Let us know.
STRML commentedon Jun 29, 2017
So your suggestion is to override
User.validatePassword?rashmihunt commentedon Jun 29, 2017
@STRML As far as I can understand following above discussion, per @bajtos
comments above #251 (comment), there are couple of possible workarounds - override validatePassword(), use 'persist' hook.
"
I see we landed #941 which allows users to implement a workaround. Is that good enough?
…….
Then there is #941 (comment) (comment) mentioning this issue should be easy to fix/rework once we have the "persist" hook in place, which has happened quite some time ago - see loopbackio/loopback-datasource-juggler#559.
Perhaps that's the way to go? To keep the password plaintext all the way until the "persist" hook that is called after any validations were passed.
"
STRML commentedon Jun 29, 2017
Sounds to me like now that
persistis in, then there just needs to be a fix implemented that knows if you're trying to validate'password', uses the right hook, then this can be closed. But it shouldn't be closed just with a workaround.raymondfeng commentedon Jun 29, 2017
In general, we need to have a better way to handle
calculated/transformedproperties such aspasswordupon 'read/write`. I guess we should open an issue for the broader feature.rashmihunt commentedon Jun 30, 2017
@raymondfeng opened feature #3468
Will close this bug as couple of workaround available and broader feature is tracked through #3468
jannyHou commentedon Jul 6, 2017
Hi @rashmihunt, would you mind if I close this issue and track the feature progress in your new issue #3468?
bajtos commentedon Aug 9, 2017
IMO, we should keep this issue open. As I understand it, #3468 will lay groundwork necessary to fix password validators. With this foundation in place, we will still need to rework the way how password are handled in LoopBack.
stale commentedon Oct 8, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
stale commentedon Oct 22, 2017
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the
CODEOWNERSfile at the top-level of this repository.