Skip to content

Commit

Permalink
[HttpKernel] Configure session.cookie_secure earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
tamcy authored and nicolas-grekas committed Feb 25, 2021
1 parent a3050e0 commit 6dc9343
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 13 additions & 3 deletions EventListener/SessionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
* Sets the session in the request.
Expand All @@ -33,10 +34,12 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

protected function getSession(): ?SessionInterface
public function onKernelRequest(GetResponseEvent $event)
{
if (!$this->container->has('session')) {
return null;
parent::onKernelRequest($event);

if (!$event->isMasterRequest() || !$this->container->has('session')) {
return;
}

if ($this->container->has('session_storage')
Expand All @@ -46,6 +49,13 @@ protected function getSession(): ?SessionInterface
) {
$storage->setOptions(['cookie_secure' => true]);
}
}

protected function getSession(): ?SessionInterface
{
if (!$this->container->has('session')) {
return null;
}

return $this->container->get('session');
}
Expand Down
8 changes: 6 additions & 2 deletions Tests/EventListener/SessionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testSessionIsSet()
$listener = new SessionListener($container);

$event = $this->createMock(RequestEvent::class);
$event->expects($this->once())->method('isMasterRequest')->willReturn(true);
$event->expects($this->exactly(2))->method('isMasterRequest')->willReturn(true);
$event->expects($this->once())->method('getRequest')->willReturn($request);

$listener->onKernelRequest($event);
Expand Down Expand Up @@ -203,12 +203,16 @@ public function testGetSessionIsCalledOnce()
$listener = new SessionListener($container);
$listener->onKernelRequest($event);

// storage->setOptions() should have been called already
$container->set('session_storage', null);
$sessionStorage = null;

$subRequest = $masterRequest->duplicate();
// at this point both master and subrequest have a closure to build the session

$masterRequest->getSession();

// calling the factory on the subRequest should not trigger a second call to storage->sesOptions()
// calling the factory on the subRequest should not trigger a second call to storage->setOptions()
$subRequest->getSession();
}
}

0 comments on commit 6dc9343

Please sign in to comment.