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

Commit c009e60

Browse filesBrowse files
bug #31121 [HttpKernel] Fix get session when the request stack is empty (yceruto)
This PR was merged into the 4.2 branch. Discussion ---------- [HttpKernel] Fix get session when the request stack is empty | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT This bug happen behind an exception on a kernel response event, when one collector (e.g. `RequestDataCollector`) is trying to get the request session and the request stack is currently empty. **Reproducer** https://github.com/yceruto/get-session-bug (`GET /`) See logs on terminal: ```bash Apr 15 20:29:03 |ERROR| PHP 2019-04-15T20:29:03-04:00 Call to a member function isSecure() on null Apr 15 20:29:03 |ERROR| PHP PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function isSecure() on null in /home/yceruto/demos/getsession/vendor/symfony/http-kernel/EventListener/SessionListener.php:43 Apr 15 20:29:03 |DEBUG| PHP Stack trace: Apr 15 20:29:03 |DEBUG| PHP #0 /home/yceruto/demos/getsession/vendor/symfony/http-kernel/EventListener/AbstractSessionListener.php(59): Symfony\Component\HttpKernel\EventListener\SessionListener->getSession() Apr 15 20:29:03 |DEBUG| PHP #1 /home/yceruto/demos/getsession/vendor/symfony/http-foundation/Request.php(707): Symfony\Component\HttpKernel\EventListener\AbstractSessionListener->Symfony\Component\HttpKernel\EventListener\{closure}() Apr 15 20:29:03 |DEBUG| PHP #2 /home/yceruto/demos/getsession/vendor/symfony/http-kernel/DataCollector/RequestDataCollector.php(65): Symfony\Component\HttpFoundation\Request->getSession() Apr 15 20:29:03 |DEBUG| PHP #3 /home/yceruto/demos/getsession/vendor/symfony/http-kernel/Profiler/Profiler.php(167): Symfony\Component\HttpKernel\DataCollector\RequestDataCollector->collect(Object(Symfony\Component\HttpFoundation\Request), Object(Symfony\Component\HttpFoundation\Respo in /home/yceruto/demos/getsession/vendor/symfony/http-kernel/EventListener/SessionListener.php on line 43 ``` Friendly ping @nicolas-grekas as author of the previous PR #28244 Commits ------- d62ca37 Fix get session when the request stack is empty
2 parents 8419873 + d62ca37 commit c009e60
Copy full SHA for c009e60

File tree

2 files changed

+12
-1
lines changed
Filter options

2 files changed

+12
-1
lines changed

‎src/Symfony/Component/HttpKernel/EventListener/SessionListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/SessionListener.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ protected function getSession()
4040

4141
if ($this->container->has('session_storage')
4242
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
43-
&& $this->container->get('request_stack')->getMasterRequest()->isSecure()
43+
&& ($masterRequest = $this->container->get('request_stack')->getMasterRequest())
44+
&& $masterRequest->isSecure()
4445
) {
4546
$storage->setOptions(['cookie_secure' => true]);
4647
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Symfony\Component\DependencyInjection\Container;
1616
use Symfony\Component\DependencyInjection\ServiceLocator;
1717
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1819
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\HttpFoundation\Session\Session;
21+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
2022
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
2123
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
2224
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
@@ -41,8 +43,16 @@ public function testSessionIsSet()
4143
{
4244
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
4345

46+
$requestStack = $this->getMockBuilder(RequestStack::class)->getMock();
47+
$requestStack->expects($this->once())->method('getMasterRequest')->willReturn(null);
48+
49+
$sessionStorage = $this->getMockBuilder(NativeSessionStorage::class)->getMock();
50+
$sessionStorage->expects($this->never())->method('setOptions')->with(['cookie_secure' => true]);
51+
4452
$container = new Container();
4553
$container->set('session', $session);
54+
$container->set('request_stack', $requestStack);
55+
$container->set('session_storage', $sessionStorage);
4656

4757
$request = new Request();
4858
$listener = new SessionListener($container);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.