Skip to content

Commit 5ef19ef

Browse files
[Security] always check the token on non-lazy firewalls
1 parent ddc39f7 commit 5ef19ef

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Firewall/AccessListener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1919
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
2020
use Symfony\Component\Security\Http\AccessMapInterface;
21+
use Symfony\Component\Security\Http\Event\LazyResponseEvent;
2122

2223
/**
2324
* AccessListener enforces access control rules.
@@ -51,6 +52,10 @@ public function __construct(TokenStorageInterface $tokenStorage, AccessDecisionM
5152
*/
5253
public function __invoke(RequestEvent $event)
5354
{
55+
if (!$event instanceof LazyResponseEvent && null === $token = $this->tokenStorage->getToken()) {
56+
throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
57+
}
58+
5459
$request = $event->getRequest();
5560

5661
list($attributes) = $this->map->getPatterns($request);
@@ -59,7 +64,7 @@ public function __invoke(RequestEvent $event)
5964
return;
6065
}
6166

62-
if (null === $token = $this->tokenStorage->getToken()) {
67+
if ($event instanceof LazyResponseEvent && null === $token = $this->tokenStorage->getToken()) {
6368
throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
6469
}
6570

Tests/Firewall/AccessListenerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1919
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
2020
use Symfony\Component\Security\Http\AccessMapInterface;
21+
use Symfony\Component\Security\Http\Event\LazyResponseEvent;
2122
use Symfony\Component\Security\Http\Firewall\AccessListener;
2223

2324
class AccessListenerTest extends TestCase
@@ -219,7 +220,7 @@ public function testHandleWhenAccessMapReturnsEmptyAttributes()
219220
->willReturn($request)
220221
;
221222

222-
$listener($event);
223+
$listener(new LazyResponseEvent($event));
223224
}
224225

225226
public function testHandleWhenTheSecurityTokenStorageHasNoToken()

0 commit comments

Comments
 (0)