8000 [Security] Do not try to clear CSRF on stateless request by Seb33300 · Pull Request #54742 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Do not try to clear CSRF on stateless request #54742

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function registerLogoutHandler(ContainerBuilder $container): void

$container->register('security.logout.listener.csrf_token_clearing', CsrfTokenClearingLogoutListener::class)
->addArgument(new Reference('security.csrf.token_storage'))
->addArgument(new Reference('security.firewall.map'))
->addTag('kernel.event_subscriber');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ security:
secret: secret
logout:
invalidate_session: false
stateless: true
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Symfony\Component\Security\Http\EventListener;

use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\Security\Http\FirewallMapInterface;

/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
Expand All @@ -24,15 +26,25 @@
class CsrfTokenClearingLogoutListener implements EventSubscriberInterface
{
private ClearableTokenStorageInterface $csrfTokenStorage;
private FirewallMapInterface $map;

public function __construct(ClearableTokenStorageInterface $csrfTokenStorage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding an argument is a BC break, let's make it nullable?

public function __construct(ClearableTokenStorageInterface $csrfTokenStorage, FirewallMapInterface $map)
{
$this->csrfTokenStorage = $csrfTokenStorage;
$this->map = $map;
}

public function onLogout(LogoutEvent $event): void
{
if ($this->csrfTokenStorage instanceof SessionTokenStorage && !$event->getRequest()->hasPreviousSession()) {
$request = $event->getRequest();

if (
$this->csrfTokenStorage instanceof SessionTokenStorage
&& (
($this->map instanceof FirewallMap && $this->map->getFirewallConfig($request)->isStateless())
|| !$request->hasPreviousSession()
)
) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,66 @@
namespace Symfony\Component\Security\Http\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\Security\Http\EventListener\CsrfTokenClearingLogoutListener;

class CsrfTokenClearingLogoutListenerTest extends TestCase
{
public function testSkipsClearingSessionTokenStorageOnStatelessRequest()
public function testSkipsClearingSessionTokenStorageOnRequestWithoutSession()
{
$map = $this->createMock(FirewallMap::class);
$map
->expects($this->once())
->method('getFirewallConfig')
->willReturn(new FirewallConfig('firewall', 'user_checker'))
;

try {
(new CsrfTokenClearingLogoutListener(
new SessionTokenStorage(new RequestStack())
new SessionTokenStorage(new RequestStack()),
$map
))->onLogout(new LogoutEvent(new Request(), null));
} catch (SessionNotFoundException) {
$this->fail('clear() must not be called if the request is not associated with a session instance');
}

$this->addToAssertionCount(1);
}

public function testSkipsClearingSessionTokenStorageOnStatelessRequest()
{
$session = new Session();

// Create a stateless request with a previous session
$request = new Request();
$request->setSession($session);
$request->cookies->set($session->getName(), 'previous_session');
$request->attributes->set('_stateless', true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this since it's not related anymore, isn't it?


$map = $this->createMock(FirewallMap::class);
$map
->expects($this->once())
->method('getFirewallConfig')
->with($this->equalTo($request))
->willReturn(new FirewallConfig('stateless_firewall', 'user_checker', stateless: true))
;

try {
(new CsrfTokenClearingLogoutListener(
new SessionTokenStorage(new RequestStack()),
$map
))->onLogout(new LogoutEvent($request, null));
} catch (SessionNotFoundException) {
$this->fail('clear() must not be called if the request is stateless');
}

$this->addToAssertionCount(1);
}
}
Loading
0