Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport CVE-2019-5421 fix to 3.x #5043

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/devise/models/lockable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def valid_for_authentication?
if super && !access_locked?
true
else
self.failed_attempts ||= 0
self.failed_attempts += 1
self.class.increment_counter(:failed_attempts, id)
reload
if attempts_exceeded?
lock_access! unless access_locked?
else
Expand Down
11 changes: 11 additions & 0 deletions test/models/lockable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ def setup
end
end

test "should read failed_attempts from database when incrementing" do
user = create_user
initial_failed_attempts = user.failed_attempts
same_user = User.find(user.id)

user.valid_for_authentication?{ false }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, what's the benefit of calling #valid_for_authentication? instead of #user.increment_failed_attempts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afaik increment_failed_attempts is not available in the 3-stable branch.
The incrementation is actually done in the valid_for_authentication? method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's correct. That's my bad, sorry 😞

same_user.valid_for_authentication?{ false }

assert_equal initial_failed_attempts + 2, user.reload.failed_attempts
end

test 'should be valid for authentication with a unlocked user' do
user = create_user
user.lock_access!
Expand Down