Skip to content

[Security] Fix event propagation for globally registered security events #39621

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 1 commit into from
Dec 23, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\Event\LoginFailureEvent;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\Security\Http\SecurityEvents;

/**
* Makes sure all event listeners on the global dispatcher are also listening
* to events on the firewall-specific dipatchers.
* to events on the firewall-specific dispatchers.
*
* This compiler pass must be run after RegisterListenersPass of the
* EventDispatcher component.
Expand All @@ -31,7 +35,18 @@
*/
class RegisterGlobalSecurityEventListenersPass implements CompilerPassInterface
{
private static $eventBubblingEvents = [CheckPassportEvent::class, LoginFailureEvent::class, LoginSuccessEvent::class, LogoutEvent::class];
private static $eventBubblingEvents = [
CheckPassportEvent::class,
LoginFailureEvent::class,
LoginSuccessEvent::class,
LogoutEvent::class,
AuthenticationSuccessEvent::class,
InteractiveLoginEvent::class,

// When events are registered by their name
AuthenticationEvents::AUTHENTICATION_SUCCESS,
SecurityEvents::INTERACTIVE_LOGIN,
];

/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
use Symfony\Component\Security\Http\Event\LogoutEvent;
Expand Down Expand Up @@ -53,13 +54,15 @@ public function testRegisterCustomListener()

$this->container->register('app.security_listener', \stdClass::class)
->addTag('kernel.event_listener', ['method' => 'onLogout', 'event' => LogoutEvent::class])
->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20]);
->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20])
->addTag('kernel.event_listener', ['method' => 'onAuthenticationSuccess', 'event' => AuthenticationEvents::AUTHENTICATION_SUCCESS]);

$this->container->compile();

$this->assertListeners([
[LogoutEvent::class, ['app.security_listener', 'onLogout'], 0],
[LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20],
[AuthenticationEvents::AUTHENTICATION_SUCCESS, ['app.security_listener', 'onAuthenticationSuccess'], 0],
]);
}

Expand All @@ -79,6 +82,7 @@ public function testRegisterCustomSubscriber()
[LogoutEvent::class, [TestSubscriber::class, 'onLogout'], -200],
[CheckPassportEvent::class, [TestSubscriber::class, 'onCheckPassport'], 120],
[LoginSuccessEvent::class, [TestSubscriber::class, 'onLoginSuccess'], 0],
[AuthenticationEvents::AUTHENTICATION_SUCCESS, [TestSubscriber::class, 'onAuthenticationSuccess'], 0],
]);
}

Expand All @@ -95,17 +99,20 @@ public function testMultipleFirewalls()

$this->container->register('app.security_listener', \stdClass::class)
->addTag('kernel.event_listener', ['method' => 'onLogout', 'event' => LogoutEvent::class])
->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20]);
->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20])
->addTag('kernel.event_listener', ['method' => 'onAuthenticationSuccess', 'event' => AuthenticationEvents::AUTHENTICATION_SUCCESS]);

$this->container->compile();

$this->assertListeners([
[LogoutEvent::class, ['app.security_listener', 'onLogout'], 0],
[LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20],
[AuthenticationEvents::AUTHENTICATION_SUCCESS, ['app.security_listener', 'onAuthenticationSuccess'], 0],
], 'security.event_dispatcher.main');
$this->assertListeners([
[LogoutEvent::class, ['app.security_listener', 'onLogout'], 0],
[LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20],
[AuthenticationEvents::AUTHENTICATION_SUCCESS, ['app.security_listener', 'onAuthenticationSuccess'], 0],
], 'security.event_dispatcher.api');
}

Expand All @@ -122,13 +129,15 @@ public function testListenerAlreadySpecific()

$this->container->register('app.security_listener', \stdClass::class)
->addTag('kernel.event_listener', ['method' => 'onLogout', 'event' => LogoutEvent::class, 'dispatcher' => 'security.event_dispatcher.main'])
->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20]);
->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20])
->addTag('kernel.event_listener', ['method' => 'onAuthenticationSuccess', 'event' => AuthenticationEvents::AUTHENTICATION_SUCCESS]);

$this->container->compile();

$this->assertListeners([
[LogoutEvent::class, ['app.security_listener', 'onLogout'], 0],
[LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20],
[AuthenticationEvents::AUTHENTICATION_SUCCESS, ['app.security_listener', 'onAuthenticationSuccess'], 0],
], 'security.event_dispatcher.main');
}

Expand All @@ -146,7 +155,8 @@ private function assertListeners(array $expectedListeners, string $dispatcherId
}

$foundListeners = array_uintersect($expectedListeners, $actualListeners, function (array $a, array $b) {
return $a === $b;
// PHP internally sorts all the arrays first, so returning proper 1 / -1 values is crucial
return $a <=> $b;
});

$this->assertEquals($expectedListeners, $foundListeners);
Expand All @@ -161,6 +171,7 @@ public static function getSubscribedEvents(): array
LogoutEvent::class => ['onLogout', -200],
CheckPassportEvent::class => ['onCheckPassport', 120],
LoginSuccessEvent::class => 'onLoginSuccess',
AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
];
}
}