Skip to content

Commit

Permalink
Merge pull request #43284 from mibradev/password-digest-nil
Browse files Browse the repository at this point in the history
Prevent error when authenticating user with a blank password digest
  • Loading branch information
rafaelfranca committed Nov 25, 2021
2 parents 395ea07 + d1d4a54 commit 902e829
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/secure_password.rb
Expand Up @@ -119,7 +119,7 @@ def initialize(attribute)
# user.authenticate_password('mUc3m00RsqyRe') # => user
define_method("authenticate_#{attribute}") do |unencrypted_password|
attribute_digest = public_send("#{attribute}_digest")
BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
attribute_digest.present? && BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
end

alias_method :authenticate, :authenticate_password if attribute == :password
Expand Down
5 changes: 5 additions & 0 deletions activemodel/test/cases/secure_password_test.rb
Expand Up @@ -218,6 +218,11 @@ class SecurePasswordTest < ActiveModel::TestCase
assert_equal @user, @user.authenticate_recovery_password("42password")
end

test "authenticate should return false and not raise when password digest is blank" do
@user.password_digest = " "
assert_equal false, @user.authenticate(" ")
end

test "Password digest cost defaults to bcrypt default cost when min_cost is false" do
ActiveModel::SecurePassword.min_cost = false

Expand Down

0 comments on commit 902e829

Please sign in to comment.