Today I noticed something disturbing while inspecting session files in storage/framework/sessions
folder created by Laravel 5.
Here is what happened:
- I logged in as user A
- I navigated to a page which stores variable X in the Session
- I logged out, but did not close the browser.
- Session file in
storage/framework/sessions
was still there, and browser cookie was alive. - I logged in as user B.
- The old session file in
storage/framework/sessions
got deleted and a new session file was there. - I looked into the new session file - surprise! variable X has survived log-out and is still there, accessible for user B!
It leads to security concerns because now user B has access to the data of user A.
While debugging through Laravel source code, I found out that Session Store is never being cleared up during logout/login process. Only login credentials are being removed in Illuminate\Auth\Guard::clearUserDataFromStorage()
method, but all the session Store attributes are still there, and then later when $kernel->terminate($request, $response);
is called, which in turn leads to Illuminate\Session\Middleware\StartSession::terminate()
calling Store::save()
, which blindly saves $this->attributes
to the new session, ignoring the fact that it now belongs to another user.
From one hand, it seems logical - Laravel has no assumptions about my data and whether I want it to expire together with authentication or not. But it would be great to have it documented somewhere with a solution to attach some sensitive data to authentication object and expire together with it.
This means that I as a programmer am responsible for completely clearing away all the sensitive data from current session when a new (or the same) user is logging in.
Clearing on logging out would not be reliable, because user might never click Logout link but wait for the session to "expire", which for Laravel still does not clear up the session.
One more thing to keep in mind: I should not clear the session too early - there is AntiForgery token which must be present, or else login form will always fail.
I have found a forum topic which also tries to solve somewhat similar problem:
http://laravel.io/forum/04-27-2014-how-to-expire-session-data
I got confused by this:
I had another go at it today and realised what the problem was: Session::flush() does not delete session data that the app creates, such as the shopping cart details
If this is true, then the only way to get completely rid of session would be to use PHP native session_unset()
and session_destroy()
but I wouldn't want to go that way - I would prefer to find a cleaner, Laravel-ish solution, if possible.
How do I tell Laravel that I want my old session data to be removed together with user authentication data when authentication expires or user logs out?