Skip to content

Commit e9779ec

Browse files
committed
bug symfony#44618 [HttpKernel] Fix SessionListener without session in request (shyim)
This PR was merged into the 5.3 branch. Discussion ---------- [HttpKernel] Fix SessionListener without session in request | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | We have in our project a Listener for `kernel.request` which sets a Response object if it's a `OPTIONS` call. A `setResponse` in the event does stopping all other listeners also the session listener. As the session listener is not triggered in the kernel.request the followup kernel.response event will let the session listener crash. https://github.com/symfony/symfony/blob/5.3/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php#L80 This line tries to get the session from the container, if its missing it calls `getSession` this throws then a error Session is not set. It looks like this issue has been fixed already in Symfony 6. See https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php#L99-L101 Commits ------- 7abddd0 Fix SessionListener without session in request
2 parents ab7b2d9 + 7abddd0 commit e9779ec

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function onKernelRequest(RequestEvent $event)
6868

6969
public function onKernelResponse(ResponseEvent $event)
7070
{
71-
if (!$event->isMainRequest()) {
71+
if (!$event->isMainRequest() || (!$this->container->has('initialized_session') && !$event->getRequest()->hasSession())) {
7272
return;
7373
}
7474

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ public function testUninitializedSession()
142142
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
143143
}
144144

145+
public function testUninitializedSessionWithoutInitializedSession()
146+
{
147+
$kernel = $this->createMock(HttpKernelInterface::class);
148+
$response = new Response();
149+
$response->setSharedMaxAge(60);
150+
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
151+
152+
$container = new ServiceLocator([]);
153+
154+
$listener = new SessionListener($container);
155+
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $response));
156+
$this->assertFalse($response->headers->has('Expires'));
157+
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
158+
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
159+
$this->assertFalse($response->headers->hasCacheControlDirective('must-revalidate'));
160+
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
161+
}
162+
145163
public function testSurrogateMainRequestIsPublic()
146164
{
147165
$session = $this->createMock(Session::class);

0 commit comments

Comments
 (0)