Skip to content

Commit

Permalink
[Security] Fix possible session fixation when only the *token* changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertMe authored and nicolas-grekas committed Nov 3, 2023
1 parent a86dc99 commit 6d3cd5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion EventListener/SessionStrategyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function onSuccessfulLogin(LoginSuccessEvent $event): void
$user = method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
$previousUser = method_exists($previousToken, 'getUserIdentifier') ? $previousToken->getUserIdentifier() : $previousToken->getUsername();

if ('' !== ($user ?? '') && $user === $previousUser) {
if ('' !== ($user ?? '') && $user === $previousUser && \get_class($token) === \get_class($previousToken)) {
return;
}
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/EventListener/SessionStrategyListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
Expand Down Expand Up @@ -81,6 +82,26 @@ public function testRequestWithSamePreviousUser()
$this->listener->onSuccessfulLogin($event);
}

public function testRequestWithSamePreviousUserButDifferentTokenType()
{
$this->configurePreviousSession();

$token = $this->createMock(NullToken::class);
$token->expects($this->once())
->method('getUserIdentifier')
->willReturn('test');
$previousToken = $this->createMock(UsernamePasswordToken::class);
$previousToken->expects($this->once())
->method('getUserIdentifier')
->willReturn('test');

$this->sessionAuthenticationStrategy->expects($this->once())->method('onAuthentication')->with($this->request, $token);

$event = new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), new SelfValidatingPassport(new UserBadge('test', function () {})), $token, $this->request, null, 'main_firewall', $previousToken);

$this->listener->onSuccessfulLogin($event);
}

private function createEvent($firewallName)
{
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), new SelfValidatingPassport(new UserBadge('test', function ($username) { return new InMemoryUser($username, null); })), $this->token, $this->request, null, $firewallName);
Expand Down

0 comments on commit 6d3cd5a

Please sign in to comment.