Skip to content
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

[Security] fix Check if it has session before getSession() #42259

Merged
merged 1 commit into from
Jul 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class SessionLogoutListener implements EventSubscriberInterface
{
public function onLogout(LogoutEvent $event): void
{
$event->getRequest()->getSession()->invalidate();
if ($event->getRequest()->hasSession()) {
$event->getRequest()->getSession()->invalidate();
}
}

public static function getSubscribedEvents(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Http\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\Security\Http\EventListener\SessionLogoutListener;

class SessionLogoutListenerTest extends TestCase
mousezheng marked this conversation as resolved.
Show resolved Hide resolved
{
public function testOnLogoutIfHasNoSession()
{
$request = $this->createMock(Request::class);
$request->method('hasSession')->willReturn(false);
$request->expects($this->never())->method('getSession');

$sessionLogoutListener = new SessionLogoutListener();
$sessionLogoutListener->onLogout(new LogoutEvent($request, null));
}

public function testOnLogoutIfHasSession()
{
$session = $this->createMock(Session::class);
$session->expects($this->once())->method('invalidate');

$request = $this->createMock(Request::class);
$request->method('getSession')->willReturn($session);
$request->method('hasSession')->willReturn(true);

$sessionLogoutListener = new SessionLogoutListener();
$sessionLogoutListener->onLogout(new LogoutEvent($request, null));
}
}