Skip to content

Commit

Permalink
Merge branch '3.4' into 4.3
Browse files Browse the repository at this point in the history
* 3.4:
  [Intl] Update the ICU data to 65.1
  [VarDumper] fix dumping uninitialized SplFileInfo
  Added missing translations.
  Fixed invalid VarDumper upgrade doc.
  [HttpFoundation] Check if data passed to SessionBagProxy::initialize is an array
  Don't let falsey usernames slip through
  • Loading branch information
nicolas-grekas committed Oct 4, 2019
2 parents 1d36e2a + fb11079 commit e64b44b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Firewall/SwitchUserListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ public function __construct(TokenStorageInterface $tokenStorage, UserProviderInt
public function __invoke(RequestEvent $event)
{
$request = $event->getRequest();
$username = $request->get($this->usernameParameter) ?: $request->headers->get($this->usernameParameter);

if (!$username) {
// usernames can be falsy
$username = $request->get($this->usernameParameter);

if (null === $username || '' === $username) {
$username = $request->headers->get($this->usernameParameter);
}

// if it's still "empty", nothing to do.
if (null === $username || '' === $username) {
return;
}

Expand Down
26 changes: 26 additions & 0 deletions Tests/Firewall/SwitchUserListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,32 @@ public function testSwitchUser()
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $this->tokenStorage->getToken());
}

public function testSwitchUserWorksWithFalsyUsernames()
{
$token = new UsernamePasswordToken('username', '', 'key', ['ROLE_FOO']);
$user = new User('username', 'password', []);

$this->tokenStorage->setToken($token);
$this->request->query->set('_switch_user', '0');

$this->accessDecisionManager->expects($this->once())
->method('decide')->with($token, ['ROLE_ALLOWED_TO_SWITCH'])
->willReturn(true);

$this->userProvider->expects($this->once())
->method('loadUserByUsername')->with('0')
->willReturn($user);
$this->userChecker->expects($this->once())
->method('checkPostAuth')->with($user);

$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);

$this->assertSame([], $this->request->query->all());
$this->assertSame('', $this->request->server->get('QUERY_STRING'));
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $this->tokenStorage->getToken());
}

public function testSwitchUserKeepsOtherQueryStringParameters()
{
$token = new UsernamePasswordToken('username', '', 'key', ['ROLE_FOO']);
Expand Down

0 comments on commit e64b44b

Please sign in to comment.