Skip to content

Commit a60785c

Browse files
committed
Update password validation and error messages
- Simplify password validation to only check byte size for BCrypt limit (72 bytes) - Replace specific error messages with a single "is too long" message - Update test cases to reflect new error message Co-authored-by: ChatGPT
1 parent 63f0914 commit a60785c

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

activemodel/lib/active_model/locale/en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ en:
1818
too_long:
1919
one: "is too long (maximum is 1 character)"
2020
other: "is too long (maximum is %{count} characters)"
21-
too_long_in_bytes: "is too long (maximum is %{count} bytes)"
21+
password_too_long: "is too long"
2222
too_short:
2323
one: "is too short (minimum is 1 character)"
2424
other: "is too short (minimum is %{count} characters)"

activemodel/lib/active_model/secure_password.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,11 @@ def has_secure_password(attribute = :password, validations: true)
132132
end
133133
end
134134

135-
# Validates that the password does not exceed the maximum allowed characters (72 characters) and
136-
# the maximum allowed bytes (72 bytes) for BCrypt. The character length validation is checked first
137-
# to provide a more user-friendly error message. However, the byte size validation is still necessary
138-
# due to BCrypt's inherent limitation of 72 bytes.
135+
# Validates that the password does not exceed the maximum allowed bytes for BCrypt (72 bytes).
139136
validate do |record|
140137
password_value = record.public_send(attribute)
141-
if password_value.present?
142-
if password_value.length > ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED
143-
record.errors.add(attribute, :too_long, count: ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED)
144-
elsif password_value.bytesize > ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED
145-
record.errors.add(attribute, :too_long_in_bytes, count: ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED)
146-
end
138+
if password_value.present? && password_value.bytesize > ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED
139+
record.errors.add(attribute, :password_too_long)
147140
end
148141
end
149142

activemodel/test/cases/secure_password_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SecurePasswordTest < ActiveModel::TestCase
6767
@user.password_confirmation = "a" * 73
6868
assert_not @user.valid?(:create), "user should be invalid"
6969
assert_equal 1, @user.errors.count
70-
assert_equal ["is too long (maximum is 72 characters)"], @user.errors[:password]
70+
assert_equal ["is too long"], @user.errors[:password]
7171
end
7272

7373
test "create a new user with validation and password byte size greater than 72 bytes" do
@@ -77,7 +77,7 @@ class SecurePasswordTest < ActiveModel::TestCase
7777
@user.password_confirmation = "あ" * 24 + "a"
7878
assert_not @user.valid?(:create), "user should be invalid"
7979
assert_equal 1, @user.errors.count
80-
assert_equal ["is too long (maximum is 72 bytes)"], @user.errors[:password]
80+
assert_equal ["is too long"], @user.errors[:password]
8181
end
8282

8383
test "create a new user with validation and a blank password confirmation" do
@@ -152,7 +152,7 @@ class SecurePasswordTest < ActiveModel::TestCase
152152
@existing_user.password_confirmation = "a" * 73
153153
assert_not @existing_user.valid?(:update), "user should be invalid"
154154
assert_equal 1, @existing_user.errors.count
155-
assert_equal ["is too long (maximum is 72 characters)"], @existing_user.errors[:password]
155+
assert_equal ["is too long"], @existing_user.errors[:password]
156156
end
157157

158158
test "updating an existing user with validation and a blank password confirmation" do

0 commit comments

Comments
 (0)