heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
24.01k stars 5.55k forks source link

Absolute timeout of the session (session not extended by the last activity) #5715

Open Mathiou04 opened 2 months ago

Mathiou04 commented 2 months ago

Issue description

Currently the session cookie created by devise seems to be re-usable indefinitely. It looks like a security issue, as if for any reason a user has his session cookie leaked, then this cookie allows anyone to impersonate the user for as long as they want.

Suggestion

I am not sure if there is a way currently to invalidate previous session cookies (changing password?), but for example the timeout module which prevents this issue to a certain extent can be circumvented: a malicious user can extend the validity of the cookie by performing legitimate HTTP requests with the still valid cookie.

A way to ensure the cookie is not re-usable indefinitely would be to provide an expiry date that can not be extended (not dependent on user activity) as already suggested here a few years back.

Would you be open to support such a feature?

himanshu123456789000 commented 1 month ago

You can set a hard expiry for session cookies by configuring the expire_after option in Rails' session store. This will ensure that a session cookie cannot be reused after a specific time, no matter how active the user has been

Here's how you can set it in config/initializers/session_store.rb:

Rails.application.config.session_store :cookie_store, key: '_your_app_session', expire_after: 30.minutes

Mathiou04 commented 1 month ago

Thanks for the reply @himanshu123456789000

I tried this option but:

The consequence is that this cookie won't expire when the browser is closed, it will be stored on the user's device, which is not a good security practice. In our case we would like to keep a session cookie (that disappears after the browser is closed) but also add an expiry mechanism in case the browser stays open for too long, re-using the same session cookie.