Skip to content

Commit d6cb5bb

Browse files
trsteel88nicolas-grekas
authored andcommitted
[HttpKernel] Use the existing session id if available.
1 parent 8a2ad2a commit d6cb5bb

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

EventListener/AbstractSessionListener.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ public function onKernelRequest(RequestEvent $event)
7272
$request->setSessionFactory(function () use (&$sess, $request) {
7373
if (!$sess) {
7474
$sess = $this->getSession();
75-
}
7675

77-
/*
78-
* For supporting sessions in php runtime with runners like roadrunner or swoole, the session
79-
* cookie needs to be read from the cookie bag and set on the session storage.
80-
*
81-
* Do not set it when a native php session is active.
82-
*/
83-
if ($sess && !$sess->isStarted() && \PHP_SESSION_ACTIVE !== session_status()) {
84-
$sessionId = $request->cookies->get($sess->getName(), '');
85-
$sess->setId($sessionId);
76+
/*
77+
* For supporting sessions in php runtime with runners like roadrunner or swoole, the session
78+
* cookie needs to be read from the cookie bag and set on the session storage.
79+
*
80+
* Do not set it when a native php session is active.
81+
*/
82+
if ($sess && !$sess->isStarted() && \PHP_SESSION_ACTIVE !== session_status()) {
83+
$sessionId = $sess->getId() ?: $request->cookies->get($sess->getName(), '');
84+
$sess->setId($sessionId);
85+
}
8686
}
8787

8888
return $sess;

Tests/EventListener/SessionListenerTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,40 @@ public function testSessionCookieNotWrittenCookieGiven()
202202
$this->assertCount(0, $cookies);
203203
}
204204

205+
/**
206+
* @runInSeparateProcess
207+
*/
208+
public function testNewSessionIdIsNotOverwritten()
209+
{
210+
$newSessionId = $this->createValidSessionId();
211+
212+
$this->assertNotEmpty($newSessionId);
213+
214+
$request = new Request();
215+
$request->cookies->set('PHPSESSID', 'OLD-SESSION-ID');
216+
217+
$listener = $this->createListener($request, new NativeSessionStorageFactory());
218+
219+
$kernel = $this->createMock(HttpKernelInterface::class);
220+
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
221+
222+
$session = $request->getSession();
223+
$this->assertSame($newSessionId, $session->getId());
224+
$session->set('hello', 'world');
225+
226+
$response = new Response();
227+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
228+
$this->assertSame($newSessionId, $session->getId());
229+
230+
$cookies = $response->headers->getCookies();
231+
232+
$this->assertCount(1, $cookies);
233+
$sessionCookie = $cookies[0];
234+
235+
$this->assertSame('PHPSESSID', $sessionCookie->getName());
236+
$this->assertSame($newSessionId, $sessionCookie->getValue());
237+
}
238+
205239
/**
206240
* @runInSeparateProcess
207241
*/
@@ -488,7 +522,7 @@ public function testUninitializedSessionWithoutInitializedSession()
488522
public function testSurrogateMainRequestIsPublic()
489523
{
490524
$session = $this->createMock(Session::class);
491-
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
525+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
492526
$session->expects($this->exactly(4))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1, 1, 1));
493527

494528
$container = new Container();
@@ -528,7 +562,7 @@ public function testSurrogateMainRequestIsPublic()
528562
public function testGetSessionIsCalledOnce()
529563
{
530564
$session = $this->createMock(Session::class);
531-
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
565+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
532566
$sessionStorage = $this->createMock(NativeSessionStorage::class);
533567
$kernel = $this->createMock(KernelInterface::class);
534568

0 commit comments

Comments
 (0)