-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Session] cleanup behavior #12325
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Let me add the following item (currently implemented in Drupal): If the session is empty when attempting to write it at the end of the request, destroy it and remove the session cookie. This optimization is important for sites behind reverse proxies. |
Related issue I just experienced: If a user is logged in and you close the session early (in the controller in my case) the Symfony\Component\Security\Http\Firewall\ContextListener comes in kernel.response and tries to write the security context to the session, therefore resulting in an exception / 500 instead of the response being sent. Maybe that should be taken into account by this refactoring, it seems way too hard to even do the right thing at the moment :/ |
@Seldaek What exception is thrown? And how is this related to this ticket? |
@Tobion I got: |
Great research and suggestion, @Tobion! |
I started getting the exact same error as @Seldaek after upgrading to 2.5.7 - should I open another issue, seems like BC break to me.. |
If it's a regrression then yes please open an issue. I just tried doing it
|
…s-grekas) This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] Make sessions secure and lazy | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | not yet | Fixed tickets | #6388, #6036, #12375, #12325 | License | MIT | Doc PR | - The `SessionUpdateTimestampHandlerInterface` (new to PHP 7.0) is mostly undocumented, and just not implemented anywhere. Yet, it's required to implement session fixation preventions and lazy write in userland session handlers (there is https://wiki.php.net/rfc/session-read_only-lazy_write which describes the behavior.) By implementing it, we would make Symfony session handling much better and stronger. Meanwhile, doing some cookie headers management, this also gives the opportunity to fix the "don't start if session is only read issue". So, here we are for the general idea. Now needs more (and green) tests, and review of course. Commits ------- 347939c [HttpFoundation] Make sessions secure and lazy
So are dozens of Session bug reports across the Symfony and Silex and other related libraries. Most of them are because of wrong usage or misunderstanding. This ticket sums up how I see sessions should behave after researching many, many tickets.
Session State
Session Usage
Session methods should throw exceptions when misused and when this misuse would cause wrong expectations. If the "misuse" does not cause side-effects or unexpected behavior, it should not throw exceptions.
SessionStorage::regenerate
should throw exception if no session is active because otherwise it has no effect. This fixes Session invalidate does not work (session not started automatically) silexphp/Silex#746 and https://bugs.php.net/bug.php?id=49462SessionStorage::start
should NOT throw an exception because starting a session twice makes no sense but has no downsides.$_SESSION
data into the symfony session data, in case you need access to the third party session data from within the symfony session.SessionStorage::save
should throw an exception if session is not active because there is something wrong in program logic. You cannot save something that does not exist.SessionStorage::setId
should throw an exception if session is active (partly as-is). But the question is why there is a setter at all? There should not be a case where you need to explicitly set an id. It's exactly the point that the id is random and not guessable.Session Lifecycle
Always save session before sending response / doing a redirect. This will fix many common problems:
Session::regenerate
login, the session data is actually available on the next request. Fixes NativeSessionStorage::regenerate bug #7380 and Symfony 2.2.1 - session lost after success login rediraction #7885 and https://bugs.php.net/bug.php?id=61470. Locking won't help in this case because the ID is different, which is the whole point. [HttpFoundation] fixed issue with session_regenerate_id (closes #7380) #8270 should be reverted.Auto-start behavior when reading/writing
See also #6036
{% if app.session.isstarted() %}{% for flashMessage in app.session.flashbag.get('notice') %}
is wrong. If the session is closed/saved this won't even work and not show the messages. So actually one would need to checkRequest::hasPreviousSession
each time one reads session data at the moment. But the session part does not even have a method for that. So when injecting the session alone (without request) you don't have access to this important method. So in reality, forcing users to check the session state everywhere where they read data is not viable.save()
and theget()
. But this is highly unlikely and rather strange case anway. And devs could just callstart()
themselves if they want that sync.The text was updated successfully, but these errors were encountered: