10000 [Security] Do not use First Class Callable Syntax for listeners by javer · Pull Request #46260 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Do not use First Class Callable Syntax for listeners #46260

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
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
[Security] Do not use First Class Callable Syntax for listeners
  • Loading branch information
javer committed May 5, 2022
commit d65da3c785be0360e4054c177ca5329dc467dc87
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function supports(Request $request): ?bool
public function authenticate(RequestEvent $event)
{
if (!$this->registered && null !== $this->dispatcher && $event->isMainRequest()) {
$this->dispatcher->addListener(KernelEvents::RESPONSE, $this->onKernelResponse(...));
$this->dispatcher->addListener(KernelEvents::RESPONSE, [$this, 'onKernelResponse']);
$this->registered = true;
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public function onKernelResponse(ResponseEvent $event)
return;
}

$this->dispatcher?->removeListener(KernelEvents::RESPONSE, $this->onKernelResponse(...));
$this->dispatcher?->removeListener(KernelEvents::RESPONSE, [$this, 'onKernelResponse']);
$this->registered = false;
$session = $request->getSession();
$sessionId = $session->getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public function __construct(TokenStorageInterface $tokenStorage, AuthenticationT
*/
public function register(EventDispatcherInterface $dispatcher)
{
$dispatcher->addListener(KernelEvents::EXCEPTION, $this->onKernelException(...), 1);
$dispatcher->addListener(KernelEvents::EXCEPTION, [$this, 'onKernelException'], 1);
}

/**
* Unregisters the dispatcher.
*/
public function unregister(EventDispatcherInterface $dispatcher)
{
$dispatcher->removeListener(KernelEvents::EXCEPTION, $this->onKernelException(...));
$dispatcher->removeListener(KernelEvents::EXCEPTION, [$this, 'onKernelException']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function testHandleAddsKernelResponseListener()

$dispatcher->expects($this->once())
->method('addListener')
->with(KernelEvents::RESPONSE, $listener->onKernelResponse(...));
->with(KernelEvents::RESPONSE, [$listener, 'onKernelResponse']);

$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MAIN_REQUEST));
}
Expand All @@ -197,7 +197,7 @@ public function testOnKernelResponseListenerRemovesItself()

$dispatcher->expects($this->once())
->method('removeListener')
->with(KernelEvents::RESPONSE, $listener->onKernelResponse(...));
->with(KernelEvents::RESPONSE, [$listener, 'onKernelResponse']);

$listener->onKernelResponse($event);
}
Expand Down Expand Up @@ -322,6 +322,30 @@ public function testSessionIsNotReported()
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST));
}

public function testOnKernelResponseRemoveListener()
{
$tokenStorage = new TokenStorage();
$tokenStorage->setToken(new UsernamePasswordToken(new InMemoryUser('test1', 'pass1'), 'phpunit', ['ROLE_USER']));

$request = new Request();
$request->attributes->set('_security_firewall_run', '_security_session');

$session = new Session(new MockArraySessionStorage());
$request->setSession($session);

$dispatcher = new EventDispatcher();
$httpKernel = $this->createMock(HttpKernelInterface::class);

$listener = new ContextListener($tokenStorage, [], 'session', null, $dispatcher, null, $tokenStorage->getToken(...));
$this->assertEmpty($dispatcher->getListeners());

$listener(new RequestEvent($httpKernel, $request, HttpKernelInterface::MAIN_REQUEST));
$this->assertNotEmpty($dispatcher->getListeners());

$listener->onKernelResponse(new ResponseEvent($httpKernel, $request, HttpKernelInterface::MAIN_REQUEST, new Response()));
$this->assertEmpty($dispatcher->getListeners());
}

protected function runSessionOnKernelResponse($newToken, $original = null)
{
$session = new Session(new MockArraySessionStorage());
Expand Down
41C3
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Http\Tests\Firewall;

use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
Expand Down Expand Up @@ -179,6 +180,18 @@ public function testLogoutException()
$this->assertEquals(403, $event->getThrowable()->getStatusCode());
}

public function testUnregister()
{
$listener = $this->createExceptionListener();
$dispatcher = new EventDispatcher();

$listener->register($dispatcher);
$this->assertNotEmpty($dispatcher->getListeners());

$listener->unregister($dispatcher);
$this->assertEmpty($dispatcher->getListeners());
}

public function getAccessDeniedExceptionProvider()
{
return [
Expand Down
0