Skip to content

Commit

Permalink
Do not confirm users when confirmation_token is blank
Browse files Browse the repository at this point in the history
As reported in #5071, if
for some reason, a user in the database had the `confirmation_token`
column as a blank string, Devise would confirm that user after receiving
a request with a blank `confirmation_token` parameter.
After this commit, a request sending a blank `confirmation_token`
parameter will receive a validation error.
For applications that have users with a blank `confirmation_token` in
the database, it's recommended to manually regenerate or to nullify
them.
  • Loading branch information
tegon committed Aug 12, 2019
1 parent ad58923 commit f66c996
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/devise/models/confirmable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ def send_confirmation_instructions(attributes={})
# Options must have the confirmation_token
def confirm_by_token(confirmation_token)
confirmable = find_first_by_auth_conditions(confirmation_token: confirmation_token)

if confirmable && confirmation_token.blank?
confirmable.errors.add(:confirmation_token, :blank)
return confirmable
end

unless confirmable
confirmation_digest = Devise.token_generator.digest(self, :confirmation_token, confirmation_token)
confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_digest)
Expand Down
32 changes: 32 additions & 0 deletions test/integration/confirmable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ def resend_confirmation
assert_current_url '/users/sign_in'
end

test "should not be able to confirm an email with a blank confirmation token" do
visit_user_confirmation_with_token("")

assert_contain "Confirmation token can't be blank"
end

test "should not be able to confirm an email with a nil confirmation token" do
visit_user_confirmation_with_token(nil)

assert_contain "Confirmation token can't be blank"
end

test "should not confirm user with blank confirmation token" do
user = create_user(confirm: false)
user.update_attribute(:confirmation_token, "")

visit_user_confirmation_with_token("")

assert_contain "Confirmation token can't be blank"
refute user.reload.confirmed?
end

test "should not confirm user with nil confirmation token" do
user = create_user(confirm: false)
user.update_attribute(:confirmation_token, nil)

visit_user_confirmation_with_token(nil)

assert_contain "Confirmation token can't be blank"
refute user.reload.confirmed?
end

test 'error message is configurable by resource name' do
store_translations :en, devise: {
failure: { user: { unconfirmed: "Not confirmed user" } }
Expand Down

0 comments on commit f66c996

Please sign in to comment.