Skip to content

Commit 902e829

Browse files
authored
Merge pull request rails#43284 from mibradev/password-digest-nil
Prevent error when authenticating user with a blank password digest
2 parents 395ea07 + d1d4a54 commit 902e829

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
@@ -119,7 +119,7 @@ def initialize(attribute)
119119
# user.authenticate_password('mUc3m00RsqyRe') # => user
120120
define_method("authenticate_#{attribute}") do |unencrypted_password|
121121
attribute_digest = public_send("#{attribute}_digest")
122-
BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
122+
attribute_digest.present? && BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
123123
end
124124

125125
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
@@ -218,6 +218,11 @@ class SecurePasswordTest < ActiveModel::TestCase
218218
assert_equal @user, @user.authenticate_recovery_password("42password")
219219
end
220220

221+
test "authenticate should return false and not raise when password digest is blank" do
222+
@user.password_digest = " "
223+
assert_equal false, @user.authenticate(" ")
224+
end
225+
221226
test "Password digest cost defaults to bcrypt default cost when min_cost is false" do
222227
ActiveModel::SecurePassword.min_cost = false
223228

0 commit comments

Comments
 (0)