Skip to content

Commit 878667b

Browse files
committed
[Security] Ignore target route when exiting impersonation
1 parent bd6ce06 commit 878667b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Firewall/SwitchUserListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function authenticate(RequestEvent $event): void
124124
if (!$this->stateless) {
125125
$request->query->remove($this->usernameParameter);
126126
$request->server->set('QUERY_STRING', http_build_query($request->query->all(), '', '&'));
127-
$response = new RedirectResponse($this->urlGenerator && $this->targetRoute ? $this->urlGenerator->generate($this->targetRoute) : $request->getUri(), 302);
127+
$response = new RedirectResponse($this->urlGenerator && $this->targetRoute && self::EXIT_VALUE !== $username ? $this->urlGenerator->generate($this->targetRoute) : $request->getUri(), 302);
128128

129129
$event->setResponse($response);
130130
}

Tests/Firewall/SwitchUserListenerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpKernel\Event\RequestEvent;
1919
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2021
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
2122
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
2223
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -105,6 +106,20 @@ public function testExitUserUpdatesToken()
105106
$this->assertSame($originalToken, $this->tokenStorage->getToken());
106107
}
107108

109+
public function testExitUserDoesNotRedirectToTargetRoute()
110+
{
111+
$originalToken = new UsernamePasswordToken(new InMemoryUser('username', '', []), 'key', []);
112+
$this->tokenStorage->setToken(new SwitchUserToken(new InMemoryUser('username', '', ['ROLE_USER']), 'key', ['ROLE_USER'], $originalToken));
113+
114+
$this->request->query->set('_switch_user', SwitchUserListener::EXIT_VALUE);
115+
116+
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, urlGenerator: $this->createMock(UrlGeneratorInterface::class), targetRoute: 'whatever');
117+
$listener($this->event);
118+
119+
$this->assertInstanceOf(RedirectResponse::class, $this->event->getResponse());
120+
$this->assertSame($this->request->getUri(), $this->event->getResponse()->getTargetUrl());
121+
}
122+
108123
public function testExitUserDispatchesEventWithRefreshedUser()
109124
{
110125
$originalUser = new InMemoryUser('username', null);

0 commit comments

Comments
 (0)