Skip to content

Commit f0e6aae

Browse files
committed
[Security] Do not try to clear CSRF on stateless request
1 parent 93e8814 commit f0e6aae

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Symfony/Component/Security/Http/EventListener/CsrfTokenClearingLogoutListener.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Component\Security\Http\EventListener;
1313

14+
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
1415
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1516
use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface;
1617
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
1718
use Symfony\Component\Security\Http\Event\LogoutEvent;
19+
use Symfony\Component\Security\Http\FirewallMapInterface;
1820

1921
/**
2022
* @author Christian Flothmann <[email protected]>
@@ -24,15 +26,25 @@
2426
class CsrfTokenClearingLogoutListener implements EventSubscriberInterface
2527
{
2628
private ClearableTokenStorageInterface $csrfTokenStorage;
29+
private FirewallMapInterface $map;
2730

28-
public function __construct(ClearableTokenStorageInterface $csrfTokenStorage)
31+
public function __construct(ClearableTokenStorageInterface $csrfTokenStorage, FirewallMapInterface $map)
2932
{
3033
$this->csrfTokenStorage = $csrfTokenStorage;
34+
$this->map = $map;
3135
}
3236

3337
public function onLogout(LogoutEvent $event): void
3438
{
35-
if ($this->csrfTokenStorage instanceof SessionTokenStorage && !$event->getRequest()->hasPreviousSession()) {
39+
$request = $event->getRequest();
40+
41+
if (
42+
$this->csrfTokenStorage instanceof SessionTokenStorage
43+
&& (
44+
($this->map instanceof FirewallMap && $this->map->getFirewallConfig($request)->isStateless())
45+
|| !$request->hasPreviousSession()
46+
)
47+
) {
3648
return;
3749
}
3850

0 commit comments

Comments
 (0)