Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpKernel] Use the existing session id if available. #45394

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

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpKernel] Use the existing session id if available.
  • Loading branch information
trsteel88 authored and nicolas-grekas committed Apr 12, 2022
commit 8e98edcddea5e5b8e70834cf7174837276d839d3
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ public function onKernelRequest(RequestEvent $event)
$request->setSessionFactory(function () use (&$sess, $request) {
if (!$sess) {
$sess = $this->getSession();
}

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

return $sess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,40 @@ public function testSessionCookieNotWrittenCookieGiven()
$this->assertCount(0, $cookies);
}

/**
* @runInSeparateProcess
*/
public function testNewSessionIdIsNotOverwritten()
{
$newSessionId = $this->createValidSessionId();

$this->assertNotEmpty($newSessionId);

$request = new Request();
$request->cookies->set('PHPSESSID', 'OLD-SESSION-ID');

$listener = $this->createListener($request, new NativeSessionStorageFactory());

$kernel = $this->createMock(HttpKernelInterface::class);
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));

$session = $request->getSession();
$this->assertSame($newSessionId, $session->getId());
$session->set('hello', 'world');

$response = new Response();
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
$this->assertSame($newSessionId, $session->getId());

$cookies = $response->headers->getCookies();

$this->assertCount(1, $cookies);
$sessionCookie = $cookies[0];

$this->assertSame('PHPSESSID', $sessionCookie->getName());
$this->assertSame($newSessionId, $sessionCookie->getValue());
}

/**
* @runInSeparateProcess
*/
Expand Down Expand Up @@ -488,7 +522,7 @@ public function testUninitializedSessionWithoutInitializedSession()
public function testSurrogateMainRequestIsPublic()
{
$session = $this->createMock(Session::class);
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
$session->expects($this->exactly(4))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1, 1, 1));

$container = new Container();
Expand Down Expand Up @@ -528,7 +562,7 @@ public function testSurrogateMainRequestIsPublic()
public function testGetSessionIsCalledOnce()
{
$session = $this->createMock(Session::class);
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
$sessionStorage = $this->createMock(NativeSessionStorage::class);
$kernel = $this->createMock(KernelInterface::class);

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.