Skip to content

Commit

Permalink
Revoke all authorized applications on password reset (mastodon#21325)
Browse files Browse the repository at this point in the history
* Clear sessions on password change

* Rename User::clear_sessions to revoke_access for a clearer meaning

* Add reset paassword controller test

* Use User.find instead of User.find_for_authentication for reset password test

* Use redirect and render for better test meaning in reset password

Co-authored-by: Effy Elden <[email protected]>
  • Loading branch information
2 people authored and neatchee committed Dec 16, 2022
1 parent 4cb598a commit 11d38d8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/controllers/auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def update
super do |resource|
if resource.errors.empty?
resource.session_activations.destroy_all

resource.revoke_access!
end
end
end
Expand Down
16 changes: 10 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@ def reset_password(new_password, new_password_confirmation)
super
end

def revoke_access!
Doorkeeper::AccessGrant.by_resource_owner(self).update_all(revoked_at: Time.now.utc)

Doorkeeper::AccessToken.by_resource_owner(self).in_batches do |batch|
batch.update_all(revoked_at: Time.now.utc)
Web::PushSubscription.where(access_token_id: batch).delete_all
end
end

def reset_password!
# First, change password to something random and deactivate all sessions
transaction do
Expand All @@ -394,12 +403,7 @@ def reset_password!
end

# Then, remove all authorized applications and connected push subscriptions
Doorkeeper::AccessGrant.by_resource_owner(self).in_batches.update_all(revoked_at: Time.now.utc)

Doorkeeper::AccessToken.by_resource_owner(self).in_batches do |batch|
batch.update_all(revoked_at: Time.now.utc)
Web::PushSubscription.where(access_token_id: batch).delete_all
end
revoke_access!

# Finally, send a reset password prompt to the user
send_reset_password_instructions
Expand Down
61 changes: 61 additions & 0 deletions spec/controllers/auth/passwords_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,65 @@
end
end
end

describe 'POST #update' do
let(:user) { Fabricate(:user) }

before do
@password = 'reset0password'
request.env['devise.mapping'] = Devise.mappings[:user]
end

context 'with valid reset_password_token' do
let!(:session_activation) { Fabricate(:session_activation, user: user) }
let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) }
let!(:web_push_subscription) { Fabricate(:web_push_subscription, access_token: access_token) }

before do
@token = user.send_reset_password_instructions

post :update, params: { user: { password: @password, password_confirmation: @password, reset_password_token: @token } }
end

it 'redirect to sign in' do
expect(response).to redirect_to '/auth/sign_in'
end

it 'changes password' do
this_user = User.find(user.id)

expect(this_user).to_not be_nil
expect(this_user.valid_password?(@password)).to be true
end

it 'deactivates all sessions' do
expect(user.session_activations.count).to eq 0
end

it 'revokes all access tokens' do
expect(Doorkeeper::AccessToken.active_for(user).count).to eq 0
end

it 'removes push subscriptions' do
expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count).to eq 0
end
end

context 'with invalid reset_password_token' do
before do
post :update, params: { user: { password: @password, password_confirmation: @password, reset_password_token: 'some_invalid_value' } }
end

it 'renders reset password' do
expect(response).to render_template(:new)
end

it 'retains password' do
this_user = User.find(user.id)

expect(this_user).to_not be_nil
expect(this_user.external_or_valid_password?(user.password)).to be true
end
end
end
end

0 comments on commit 11d38d8

Please sign in to comment.