Skip to content

Commit d1d4a54

Browse files
mibradevp8
andcommitted
Prevent error when authenticating user with a blank password digest
Co-authored-by: Petrik de Heus <[email protected]>
1 parent 581aac7 commit d1d4a54

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

activemodel/lib/active_model/secure_password.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def initialize(attribute)
118118
# user.authenticate_password('mUc3m00RsqyRe') # => user
119119
define_method("authenticate_#{attribute}") do |unencrypted_password|
120120
attribute_digest = public_send("#{attribute}_digest")
121-
BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
121+
attribute_digest.present? && BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
122122
end
123123

124124
alias_method :authenticate, :authenticate_password if attribute == :password

activemodel/test/cases/secure_password_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ class SecurePasswordTest < ActiveModel::TestCase
212212
assert_equal @user, @user.authenticate_recovery_password("42password")
213213
end
214214

215+
test "authenticate should return false and not raise when password digest is blank" do
216+
@user.password_digest = " "
217+
assert_equal false, @user.authenticate(" ")
218+
end
219+
215220
test "Password digest cost defaults to bcrypt default cost when min_cost is false" do
216221
ActiveModel::SecurePassword.min_cost = false
217222

0 commit comments

Comments
 (0)