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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should auto_login resets and restores session? #321

Open
yuh-pen opened this issue Aug 25, 2022 · 0 comments
Open

Should auto_login resets and restores session? #321

yuh-pen opened this issue Aug 25, 2022 · 0 comments
Labels
question Asking a question about Sorcery

Comments

@yuh-pen
Copy link

yuh-pen commented Aug 25, 2022

Hi,

Looks like login method resets and restores session. But auto_login method doesn't.
I think auto_login method should too. Hou about? 馃憖

Ruby On Rails Security Guide

The most effective countermeasure is to issue a new session identifier and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well. Here is how to create a new session in Rails: reset_session

If you use the popular RestfulAuthentication plugin for user management, add reset_session to the SessionsController#create action. Note that this removes any value from the session, you have to transfer them to the new session.

def auto_login(user, _should_remember = false)
session[:user_id] = user.id.to_s
@current_user = user
end

def login(*credentials)
@current_user = nil
user_class.authenticate(*credentials) do |user, failure_reason|
if failure_reason
after_failed_login!(credentials)
yield(user, failure_reason) if block_given?
# FIXME: Does using `break` or `return nil` change functionality?
# rubocop:disable Lint/NonLocalExitFromIterator
return
# rubocop:enable Lint/NonLocalExitFromIterator
end
old_session = session.dup.to_hash
reset_sorcery_session
old_session.each_pair do |k, v|
session[k.to_sym] = v
end
form_authenticity_token
auto_login(user, credentials[2])
after_login!(user, credentials)
block_given? ? yield(current_user, nil) : current_user
end
end

@joshbuker joshbuker added the question Asking a question about Sorcery label Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking a question about Sorcery
Projects
None yet
Development

No branches or pull requests

2 participants