8000 bug #45394 [HttpKernel] Use the existing session id if available. (tr… · symfony/symfony@1f15392 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f15392

Browse files
bug #45394 [HttpKernel] Use the existing session id if available. (trsteel88)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [HttpKernel] Use the existing session id if available. | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Session id is being overwritten by listener even if it is already set. I have an application that is overriding the session id when the session factory creates the session. However, when this listener runs, it's overriding the session id that has already been set. Commits ------- 8e98edc [HttpKernel] Use the existing session id if available.
2 parents 4ac2f66 + 8e98edc commit 1f15392

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/Symfony/Component/HttpKernel/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;

src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php

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

217+
/**
218+
* @runInSeparateProcess
219+
*/
220+
public function testNewSessionIdIsNotOverwritten()
221+
{
222+
$newSessionId = $this->createValidSessionId();
223+
224+
$this->assertNotEmpty($newSessionId);
225+
226+
$request = new Request();
227+
$request->cookies->set('PHPSESSID', 'OLD-SESSION-ID');
228+
229+
$listener = $this->createListener($request, new NativeSessionStorageFactory());
230+
231+
$kernel = $this->createMock(HttpKernelInterface::class);
232+
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
233+
234+
$session = $request->getSession();
235+
$this->assertSame($newSessionId, $session->getId());
236+
$session->set('hello', 'world');
237+
238+
$response = new Response();
239+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
240+
$this->assertSame($newSessionId, $session->getId());
241+
242+
$cookies = $response->headers->getCookies();
243+
244+
$this->assertCount(1, $cookies);
245+
$sessionCookie = $cookies[0];
246+
247+
$this->assertSame('PHPSESSID', $sessionCookie->getName());
248+
$this->assertSame($newSessionId, $sessionCookie->getValue());
249+
}
250+
217251
/**
218252
* @runInSeparateProcess
219253
*/
@@ -500,7 +534,7 @@ public function testUninitializedSessionWithoutInitializedSession()
500534
public function testSurrogateMainRequestIsPublic()
501535
{
502536
$session = $this->createMock(Session::class);
503-
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
537+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
504538
$session->expects($this->exactly(4))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1, 1, 1));
505539

506540
$container = new Container();
@@ -540,7 +574,7 @@ public function testSurrogateMainRequestIsPublic()
540574
public function testGetSessionIsCalledOnce()
541575
{
542576
$session = $this->createMock(Session::class);
543-
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
577+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
544578
$sessionStorage = $this->createMock(NativeSessionStorage::class);
545579
$kernel = $this->createMock(KernelInterface::class);
546580

0 commit comments

Comments
 (0)
0