-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invalidate devise session cookies immediately on logout
The default devise behaviour does not invalidate session cookies after logout so if an attacker can retrieve a cookie from a session they can use it even if the user has logged out. We have used variations of this change successfully on a number of Rails apps in production. References * heartcombo/devise#3031 * http://maverickblogging.com/logout-is-broken-by-default-ruby-on-rails-web-applications/ * https://makandracards.com/makandra/53562-devise-invalidating-all-sessions-for-a-user Fixes #91
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
variants/devise/app/controllers/users/sessions_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Users | ||
class SessionsController < Devise::SessionsController | ||
## | ||
# We want to make sure that when a user logs out (i.e. destroys their | ||
# session) then the session cookie they had cannot be used again. We | ||
# achieve this by overriding the built-in devise implementation of | ||
# `Devise::SessionsController#destroy` action to invalidate all existing | ||
# user sessions and then call `super` to run the built-in devise | ||
# implementation of the method. | ||
# | ||
# References | ||
# * https://github.com/plataformatec/devise/issues/3031 | ||
# * http://maverickblogging.com/logout-is-broken-by-default-ruby-on-rails-web-applications/ | ||
# * https://makandracards.com/makandra/53562-devise-invalidating-all-sessions-for-a-user | ||
# | ||
def destroy | ||
current_user.invalidate_all_sessions! | ||
super | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
variants/devise/spec/requests/session_cookie_expiry_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "Session cookies are expired immediately after logout", type: :request do | ||
let(:password) { SecureRandom.hex(16) } | ||
|
||
let(:user) { FactoryBot.create(:user, password: password) } | ||
|
||
it "session cookies are invalidated by logging out" do | ||
# Sign in | ||
post new_user_session_path, params: { "user[email]" => user.email, "user[password]" => password } | ||
|
||
# When we view a page which requires a signed-in user ... | ||
get edit_user_registration_path | ||
|
||
# ... then it works. | ||
expect(response).to have_http_status(200) | ||
expect(response.body).to match(/Edit User/) | ||
|
||
# Save the signed-in user's session cookie | ||
session_cookie = response.headers["Set-Cookie"] | ||
|
||
# Sign out | ||
delete destroy_user_session_path | ||
|
||
# When we try to view the same page as before ... | ||
get edit_user_registration_path | ||
|
||
# ... then we are redirected to the sign-in page | ||
expect(response).to have_http_status(302) | ||
expect(response.headers["Location"]).to eq(new_user_session_url) | ||
|
||
# When we try to view the private page, this time passing the old session | ||
# cookie ... | ||
get edit_user_registration_path, headers: { "Cookie" => session_cookie } | ||
|
||
# ... then we are still redirected to the sign-in page (this demonstrates | ||
# that session cookies are properly invalidated when a user signs out) | ||
expect(response).to have_http_status(302) | ||
expect(response.headers["Location"]).to eq(new_user_session_url) | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters