Skip to content

Commit 40f7beb

Browse files
committed
Fixed handling of CSRF logout error
1 parent 06ebbba commit 40f7beb

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Firewall/ExceptionListener.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
102102
}
103103

104104
if ($exception instanceof LogoutException) {
105-
$this->handleLogoutException($exception);
105+
$this->handleLogoutException($event, $exception);
106106

107107
return;
108108
}
@@ -172,10 +172,12 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
172172
}
173173
}
174174

175-
private function handleLogoutException(LogoutException $exception)
175+
private function handleLogoutException(GetResponseForExceptionEvent $event, LogoutException $exception)
176176
{
177+
$event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));
178+
177179
if (null !== $this->logger) {
178-
$this->logger->info('A LogoutException was thrown.', ['exception' => $exception]);
180+
$this->logger->info('A LogoutException was thrown; wrapping with AccessDeniedHttpException', ['exception' => $exception]);
179181
}
180182
}
181183

Tests/Firewall/ExceptionListenerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2222
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2323
use Symfony\Component\Security\Core\Exception\AuthenticationException;
24+
use Symfony\Component\Security\Core\Exception\LogoutException;
2425
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
2526
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
2627
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
@@ -160,6 +161,17 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
160161
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
161162
}
162163

164+
public function testLogoutException()
165+
{
166+
$event = $this->createEvent(new LogoutException('Invalid CSRF.'));
167+
168+
$listener = $this->createExceptionListener();
169+
$listener->onKernelException($event);
170+
171+
$this->assertEquals('Invalid CSRF.', $event->getException()->getMessage());
172+
$this->assertEquals(403, $event->getException()->getStatusCode());
173+
}
174+
163175
public function getAccessDeniedExceptionProvider()
164176
{
165177
return [

0 commit comments

Comments
 (0)