diff --git a/src/Symfony/Component/Lock/Key.php b/src/Symfony/Component/Lock/Key.php index 743440706b669..937836951129b 100644 --- a/src/Symfony/Component/Lock/Key.php +++ b/src/Symfony/Component/Lock/Key.php @@ -18,7 +18,7 @@ * * @author Jérémy Derussé */ -final class Key +final class Key implements \Stringable { private ?float $expiringTime = null; private array $state = []; @@ -91,9 +91,9 @@ public function isExpired(): bool public function __unserialize(array $data): void { - $this->resource = $data['resource']; - $this->expiringTime = $data['expiringTime']; - $this->state = $data['state']; + $this->resource = $data['resource'] ?? $data["\0".self::class."\0resource"]; + $this->expiringTime = $data['expiringTime'] ?? $data["\0".self::class."\0expiringTime"] ?? null; + $this->state = $data['state'] ?? $data["\0".self::class."\0state"] ?? []; } public function __serialize(): array diff --git a/src/Symfony/Component/Lock/Tests/KeyTest.php b/src/Symfony/Component/Lock/Tests/KeyTest.php index 77815b25d40ae..b34f777d6020c 100644 --- a/src/Symfony/Component/Lock/Tests/KeyTest.php +++ b/src/Symfony/Component/Lock/Tests/KeyTest.php @@ -31,6 +31,17 @@ public function testSerialize() $this->assertEqualsWithDelta($key->getRemainingLifetime(), $copy->getRemainingLifetime(), 0.001); } + public function testLegacyPayloadCanBeUnserialized() + { + $serialized = base64_decode('TzoyNjoiU3ltZm9ueVxDb21wb25lbnRcTG9ja1xLZXkiOjM6e3M6MzY6IgBTeW1mb255XENvbXBvbmVudFxMb2NrXEtleQByZXNvdXJjZSI7czo2OiJsZWdhY3kiO3M6NDA6IgBTeW1mb255XENvbXBvbmVudFxMb2NrXEtleQBleHBpcmluZ1RpbWUiO047czozMzoiAFN5bWZvbnlcQ29tcG9uZW50XExvY2tcS2V5AHN0YXRlIjthOjA6e319', true); + + $key = unserialize($serialized, ['allowed_classes' => [Key::class]]); + + $this->assertInstanceOf(Key::class, $key); + $this->assertSame('legacy', (string) $key); + $this->assertNull($key->getRemainingLifetime()); + } + public function testUnserialize() { $key = new Key(__METHOD__);