server side protect passwords which are too long

This commit is contained in:
Henry Oswald 2016-09-23 16:51:46 +01:00
parent 0d0f0e8604
commit 8a2b7d0461
2 changed files with 49 additions and 23 deletions

View file

@ -30,7 +30,7 @@ module.exports = AuthenticationManager =
setUserPassword: (user_id, password, callback = (error) ->) ->
if Settings.passwordStrengthOptions?.length?.max? and Settings.passwordStrengthOptions?.length?.max < password.length
return error("password is too long")
return callback("password is too long")
bcrypt.genSalt BCRYPT_ROUNDS, (error, salt) ->
return callback(error) if error?

View file

@ -9,6 +9,7 @@ ObjectId = require("mongojs").ObjectId
describe "AuthenticationManager", ->
beforeEach ->
@settings = { security: { bcryptRounds: 12 } }
@AuthenticationManager = SandboxedModule.require modulePath, requires:
"../../models/User": User: @User = {}
"../../infrastructure/mongojs":
@ -16,7 +17,7 @@ describe "AuthenticationManager", ->
users: {}
ObjectId: ObjectId
"bcrypt": @bcrypt = {}
"settings-sharelatex": { security: { bcryptRounds: 12 } }
"settings-sharelatex": @settings
@callback = sinon.stub()
describe "authenticate", ->
@ -102,27 +103,52 @@ describe "AuthenticationManager", ->
@bcrypt.genSalt = sinon.stub().callsArgWith(1, null, @salt)
@bcrypt.hash = sinon.stub().callsArgWith(2, null, @hashedPassword)
@db.users.update = sinon.stub().callsArg(2)
@AuthenticationManager.setUserPassword(@user_id, @password, @callback)
it "should update the user's password in the database", ->
@db.users.update
.calledWith({
_id: ObjectId(@user_id.toString())
}, {
$set: {
"hashedPassword": @hashedPassword
}
$unset: password: true
})
.should.equal true
describe "too long", ->
beforeEach ->
@settings.passwordStrengthOptions =
length:
max:10
@password = "dsdsadsadsadsadsadkjsadjsadjsadljs"
it "should return and error", (done)->
@AuthenticationManager.setUserPassword @user_id, @password, (err)->
expect(err).to.exist
done()
it "should not start the bcrypt process", (done)->
@AuthenticationManager.setUserPassword @user_id, @password, (err)=>
@bcrypt.genSalt.called.should.equal false
@bcrypt.hash.called.should.equal false
done()
describe "successful set", ->
beforeEach ->
@AuthenticationManager.setUserPassword(@user_id, @password, @callback)
it "should update the user's password in the database", ->
@db.users.update
.calledWith({
_id: ObjectId(@user_id.toString())
}, {
$set: {
"hashedPassword": @hashedPassword
}
$unset: password: true
})
.should.equal true
it "should hash the password", ->
@bcrypt.genSalt
.calledWith(12)
.should.equal true
@bcrypt.hash
.calledWith(@password, @salt)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
it "should hash the password", ->
@bcrypt.genSalt
.calledWith(12)
.should.equal true
@bcrypt.hash
.calledWith(@password, @salt)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true