Skip to content

Commit 2b971fd

Browse files
committed
Merge pull request #55232 from callmesangio/fix-gh-55225
`has_secure_password`: fix password validation.
1 parent d09ccae commit 2b971fd

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

activemodel/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Fix `has_secure_password` to validate the password confirmation field even when blank.
2+
3+
The password confirmation field being blank suggest the confirmation field was displayed
4+
and submitted.
5+
6+
The validation is now only skipped if the field is `nil`.
7+
8+
*Fabio Sangiovanni*
9+
110
## Rails 8.0.2 (March 12, 2025) ##
211

312
* No changes.

activemodel/lib/active_model/secure_password.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def has_secure_password(attribute = :password, validations: true, reset_token: t
155155
end
156156
end
157157

158-
validates_confirmation_of attribute, allow_blank: true
158+
validates_confirmation_of attribute, allow_nil: true
159159
end
160160

161161
# Only generate tokens for records that are capable of doing so (Active Records, not vanilla Active Models)

activemodel/test/cases/secure_password_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ class SecurePasswordTest < ActiveModel::TestCase
104104
assert_equal ["doesn't match Password"], @user.errors[:password_confirmation]
105105
end
106106

107+
test "create a new user with validation, a spaces only password, and an incorrect password confirmation" do
108+
@user.password = " "
109+
@user.password_confirmation = "something else"
110+
assert_not @user.valid?(:create), "user should be invalid"
111+
assert_equal 1, @user.errors.count
112+
assert_equal ["doesn't match Password"], @user.errors[:password_confirmation]
113+
end
114+
107115
test "resetting password to nil clears the password cache" do
108116
@user.password = "password"
109117
@user.password = nil
@@ -179,6 +187,14 @@ class SecurePasswordTest < ActiveModel::TestCase
179187
assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation]
180188
end
181189

190+
test "updating an existing user with validation, a spaces only password, and an incorrect password confirmation" do
191+
@existing_user.password = " "
192+
@existing_user.password_confirmation = "something else"
193+
assert_not @existing_user.valid?(:update), "user should be invalid"
194+
assert_equal 1, @existing_user.errors.count
195+
assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation]
196+
end
197+
182198
test "updating an existing user with validation and a correct password challenge" do
183199
@existing_user.password = "new password"
184200
@existing_user.password_challenge = "password"

0 commit comments

Comments
 (0)