8000 [Security] Handle non-callable implementations of `FirewallListenerInterface` by MatTheCat · Pull Request #60785 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Handle non-callable implementations of FirewallListenerInterface #60785

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/Security/Http/Firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public static function getSubscribedEvents()
protected function callListeners(RequestEvent $event, iterable $listeners)
{
foreach ($listeners as $listener) {
$listener($event);
if (!$listener instanceof FirewallListenerInterface) {
$listener($event);
} elseif (false !== $listener->supports($event->getRequest())) {
$listener->authenticate($event);
}

if ($event->hasResponse()) {
break;
Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Component/Security/Http/Tests/FirewallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Http\Firewall;
use Symfony\Component\Security\Http\Firewall\AbstractListener;
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
use Symfony\Component\Security\Http\FirewallMapInterface;

class FirewallTest extends TestCase
Expand Down Expand Up @@ -97,4 +99,59 @@ public function testOnKernelRequestWithSubRequest()

$this->assertFalse($event->hasResponse());
}

public function testListenersAreCalled()
{
$calledListeners = [];

$callableListener = static function() use(&$calledListeners) { $calledListeners[] = 'callableListener'; };
$firewallListener = new class($calledListeners) implements FirewallListenerInterface {
public function __construct(private array &$calledListeners) {}

public function supports(Request $request): ?bool
{
return true;
}

public function authenticate(RequestEvent $event): void
{
$this->calledListeners[] = 'firewallListener';
}

public static function getPriority(): int
{
return 0;
}
};
$callableFirewall 7D6C Listener = new class($calledListeners) extends AbstractListener {
public function __construct(private array &$calledListeners) {}

public function supports(Request $request): ?bool
{
return true;
}

public function authenticate(RequestEvent $event): void
{
$this->calledListeners[] = 'callableFirewallListener';
}
};

$request = $this->createMock(Request::class);

$map = $this->createMock(FirewallMapInterface::class);
$map
->expects($this->once())
->method('getListeners')
->with($this->equalTo($request))
->willReturn([[$callableListener, $firewallListener, $callableFirewallListener], null, null])
;

$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST);

$firewall = new Firewall($map, $this->createMock(EventDispatcherInterface::class));
$firewall->onKernelRequest($event);

$this->assertSame(['callableListener', 'firewallListener', 'callableFirewallListener'], $calledListeners);
}
}
0