Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9554589

Browse files
authored
Send new session cookie from AbstractTestSessionListener after session invalidation
When we call `\Symfony\Component\HttpFoundation\Session\Session::invalidate` the session will be emptied and given a new ID, however, since it is empty this `AbstractTestSessionListener` will not send a new cookie to the user, so the user is not caught up to the latest session ID and will re-generate a session with the old session ID on a new visit. Thus, we the sessionID has changed during a request we must always send a new cookie with the new sessionID, even though the session is empty. This behaviour is also what is shown in production (non-test) mode.
1 parent 4ef0b3e commit 9554589

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030
abstract class AbstractTestSessionListener implements EventSubscriberInterface
3131
{
32+
private $initialSessionId;
33+
3234
public function onKernelRequest(GetResponseEvent $event)
3335
{
3436
if (!$event->isMasterRequest()) {
@@ -45,6 +47,7 @@ public function onKernelRequest(GetResponseEvent $event)
4547

4648
if ($cookies->has($session->getName())) {
4749
$session->setId($cookies->get($session->getName()));
50+
$this->initialSessionId = $cookies->get($session->getName());
4851
}
4952
}
5053

@@ -66,7 +69,7 @@ public function onKernelResponse(FilterResponseEvent $event)
6669
$session->save();
6770
}
6871

69-
if ($session instanceof Session ? !$session->isEmpty() : $wasStarted) {
72+
if ($session instanceof Session ? !$session->isEmpty() || $session->getId() !== $this->initialSessionId : $wasStarted) {
7073
$params = session_get_cookie_params();
7174
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
7275
}

0 commit comments

Comments
 (0)