Skip to content

Password validators operate on hashed password, making length validators useless #251

Closed

Description

@STRML

Modifying the User model in the following way does not trigger a validation error when attempting to create a user with an invalid password:

var User = app.models.User;
User.validatesLengthOf('password', {min: 6, message: {min: 'Password is too short'}});
var u = new User({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.

Activity

  1. ritch commented on May 8, 2014

    @ritch
    Member

    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.

  2. bajtos commented on May 28, 2014

    @bajtos
    Member
  3. ghost assigned on Dec 17, 2014
  4. jonathanxie commented on Dec 20, 2014

    @jonathanxie

    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) {
          var password = user.password;
          console.log("user.password before hash = ", password);
          var salt = 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 creation
        UserModel.beforeRemote('create', function(ctx, user, next) {
          var body = ctx.req.body;
          if (body && body.emailVerified) {
            body.emailVerified = false;
          }
          next();
        });
  5. jonathanxie commented on Dec 20, 2014

    @jonathanxie

    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) {
          var password = user.password;
          console.log("user.password before hash = ", password);
          var salt = 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...

  6. 42 remaining items

  7. kooliokey commented on May 18, 2017

    @kooliokey

    @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.

  8. rashmihunt commented on Jun 28, 2017

    @rashmihunt
    Contributor

    @STRML As per the above conversation, looks like there is solution for originally reported problem. We will be closing this soon. Let us know.

  9. STRML commented on Jun 29, 2017

    @STRML
    MemberAuthor

    So your suggestion is to override User.validatePassword?

  10. rashmihunt commented on Jun 29, 2017

    @rashmihunt
    Contributor

    @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.

    "

  11. STRML commented on Jun 29, 2017

    @STRML
    MemberAuthor

    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.

  12. raymondfeng commented on Jun 29, 2017

    @raymondfeng
    Member

    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.

  13. rashmihunt commented on Jun 30, 2017

    @rashmihunt
    Contributor

    @raymondfeng opened feature #3468
    Will close this bug as couple of workaround available and broader feature is tracked through #3468

  14. jannyHou commented on Jul 6, 2017

    @jannyHou
    Contributor

    Hi @rashmihunt, would you mind if I close this issue and track the feature progress in your new issue #3468?

  15. bajtos commented on Aug 9, 2017

    @bajtos
    Member

    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.

  16. stale commented on Oct 8, 2017

    @stale

    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.

  17. stale commented on Oct 22, 2017

    @stale

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions