8000 [Security] Deprecated not being logged out after user change by linaori · Pull Request #23882 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Deprecated not being logged out after user change #23882

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Deprecated not being logged out after user change
  • Loading branch information
Iltar van der Berg committed Sep 21, 2017
commit 908db1e4222cc3c98b3b9ab39c67a2d2e31e2582
34 changes: 28 additions & 6 deletions src/Symfony/Component/Security/Http/Firewall/ContextListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
class ContextListener implements ListenerInterface
{
private $tokenStorage;
private $contextKey;
private $sessionKey;
private $logger;
private $userProviders;
private $dispatcher;
private $registered;
private $trustResolver;
private $logoutOnUserChange = false;

/**
* @param TokenStorageInterface $tokenStorage
Expand All @@ -61,13 +61,22 @@ public function __construct(TokenStorageInterface $tokenStorage, $userProviders,

$this->tokenStorage = $tokenStorage;
$this->userProviders = $userProviders;
$this->contextKey = $contextKey;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to deprecate the constructor argument if the class doesn't need it anymore. That can be done in another PR, could you revert this change (with the property removal)?

Copy link
Contributor
@ogizanagi ogizanagi Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still used for the sessionKey below. Only the property is removed because it's not used. (However again, it could have been done on the 2.7 branch ^^)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I've merely removed the property because the property wasn't used anymore. If desired, I can change this in a lower branch instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iltar that would be nice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #24329

$this->sessionKey = '_security_'.$contextKey;
$this->logger = $logger;
$this->dispatcher = $dispatcher;
$this->trustResolver = $trustResolver ?: new AuthenticationTrustResolver(AnonymousToken::class, RememberMeToken::class);
}

/**
* Enables deauthentication during refreshUser when the user has changed.
*
* @param bool $logoutOnUserChange
*/
public function setLogoutOnUserChange($logoutOnUserChange)
{
$this->logoutOnUserChange = (bool) $logoutOnUserChange;
}

/**
* Reads the Security Token from the session.
*
Expand Down Expand Up @@ -122,14 +131,14 @@ public function onKernelResponse(FilterResponseEvent $event)
return;
}

if (!$event->getRequest()->hasSession()) {
$request = $event->getRequest();

if (!$request->hasSession()) {
return;
}

$this->dispatcher->removeListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse'));
$this->registered = false;

$request = $event->getRequest();
$session = $request->getSession();

if ((null === $token = $this->tokenStorage->getToken()) || $this->trustResolver->isAnonymous($token)) {
Expand Down Expand Up @@ -172,6 +181,19 @@ protected function refreshUser(TokenInterface $token)
$refreshedUser = $provider->refreshUser($user);
$token->setUser($refreshedUser);

// tokens can be deauthenticated if the user has been changed.
if (!$token->isAuthenticated()) {
if ($this->logoutOnUserChange) {
if (null !== $this->logger) {
$this->logger->debug('Token was deauthenticated after trying to refresh it.', array('username' => $refreshedUser->getUsername(), 'provider' => get_class($provider)));
}

return null;
}

@trigger_error('Refreshing a deauthenticated user is deprecated as of 3.4 and will trigger a logout in 4.0.', E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a changelog+upgrade entries would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add them as soon as I know how to proceed with the actual logging out users, as this might need more additions to the changelog and upgrade entries 👍

}

if (null !== $this->logger) {
$context = array('provider' => get_class($provider), 'username' => $refreshedUser->getUsername());

Expand All @@ -198,7 +220,7 @@ protected function refreshUser(TokenInterface $token)
}

if ($userNotFoundByProvider) {
return;
return null;
Copy link
Contributor
@ogizanagi ogizanagi Sep 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be reverted as it should be applied to the 2.7 branch. But it is worth a dedicated PR on the whole codebase. Related to #17201.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix

}

throw new \RuntimeException(sprintf('There is no user provider for user "%s".', get_class($user)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ public function testHandleRemovesTokenIfNoPreviousSessionWasFound()
$listener->handle($event);
}

public function testTryAllUserProvidersUntilASupportingUserProviderIsFound()
/**
* @group legacy
* @expectedDeprecation Refreshing a deauthenticated user is deprecated as of 3.4 and will trigger a logout in 4.0.
*/
public function testIfTokenIsDeauthenticatedTriggersDeprecations()
{
$tokenStorage = new TokenStorage();
$refreshedUser = new User('foobar', 'baz');
Expand All @@ -258,11 +262,29 @@ public function testTryAllUserProvidersUntilASupportingUserProviderIsFound()
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
}

public function testIfTokenIsDeauthenticated()
{
$tokenStorage = new TokenStorage();
$refreshedUser = new User('foobar', 'baz');
$this->handleEventWithPreviousSession($tokenStorage, array(new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)), null, true);

$this->assertNull($tokenStorage->getToken());
}

public function testTryAllUserProvidersUntilASupportingUserProviderIsFound()
{
$tokenStorage = new TokenStorage();
$refreshedUser = new User('foobar', 'baz');
$this->handleEventWithPreviousSession($tokenStorage, array(new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)), $refreshedUser);

$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
}

public function testNextSupportingUserProviderIsTriedIfPreviousSupportingUserProviderDidNotLoadTheUser()
{
$tokenStorage = new TokenStorage();
$refreshedUser = new User('foobar', 'baz');
$this->handleEventWithPreviousSession($tokenStorage, array(new SupportingUserProvider(), new SupportingUserProvider($refreshedUser)));
$this->handleEventWithPreviousSession($tokenStorage, array(new SupportingUserProvider(), new SupportingUserProvider($refreshedUser)), $refreshedUser);

$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
}
Expand All @@ -287,7 +309,7 @@ public function testAcceptsProvidersAsTraversable()
{
$tokenStorage = new TokenStorage();
$refreshedUser = new User('foobar', 'baz');
$this->handleEventWithPreviousSession($tokenStorage, new \ArrayObject(array(new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser))));
$this->handleEventWithPreviousSession($tokenStorage, new \ArrayObject(array(new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser))), $refreshedUser);

$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
}
Expand Down Expand Up @@ -320,16 +342,18 @@ protected function runSessionOnKernelResponse($newToken, $original = null)
return $session;
}

private function handleEventWithPreviousSession(TokenStorageInterface $tokenStorage, $userProviders)
private function handleEventWithPreviousSession(TokenStorageInterface $tokenStorage, $userProviders, UserInterface $user = null, $logoutOnUserChange = false)
{
$user = $user ?: new User('foo', 'bar');
$session = new Session(new MockArraySessionStorage());
$session->set('_security_context_key', serialize(new UsernamePasswordToken(new User('foo', 'bar'), '', 'context_key')));
$session->set('_security_context_key', serialize(new UsernamePasswordToken($user, '', 'context_key', array('ROLE_USER'))));

$request = new Request();
$request->setSession($session);
$request->cookies->set('MOCKSESSID', true);

$listener = new ContextListener($tokenStorage, $userProviders, 'context_key');
$listener->setLogoutOnUserChange($logoutOnUserChange);
$listener->handle(new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, HttpKernelInterface::MASTER_REQUEST));
}
}
Expand Down
0