Skip to content

[Security] Support for SwitchUserToken instances serialized with 4.4/5.1 #39187

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
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 @@ -59,7 +59,12 @@ public function __serialize(): array
*/
public function __unserialize(array $data): void
{
[$this->originalToken, $this->originatedFromUri, $parentData] = $data;
if (3 > \count($data)) {
// Support for tokens serialized with version 5.1 or lower of symfony/security-core.
[$this->originalToken, $parentData] = $data;
} else {
[$this->originalToken, $this->originatedFromUri, $parentData] = $data;
}
$parentData = \is_array($parentData) ? $parentData : unserialize($parentData);
parent::__unserialize($parentData);
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,18 @@ public function testSerializeNullImpersonateUrl()

$this->assertNull($unserializedToken->getOriginatedFromUri());
}

public function testUnserializeOldToken()
{
/** @var SwitchUserToken $token */
$token = unserialize(file_get_contents(__DIR__.'/Fixtures/switch-user-token-4.4.txt'));

self::assertInstanceOf(SwitchUserToken::class, $token);
self::assertInstanceOf(UsernamePasswordToken::class, $token->getOriginalToken());
self::assertSame('john', $token->getUsername());
self::assertSame(['foo' => 'bar'], $token->getCredentials());
self::assertSame('main', $token->getFirewallName());
self::assertEquals(['ROLE_USER'], $token->getRoleNames());
self::assertNull($token->getOriginatedFromUri());
}
}