Skip to content

Commit c43a084

Browse files
bug #62653 [Lock] Fix unserializing already serialized Key payloads (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [Lock] Fix unserializing already serialized Key payloads | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62585 | License | MIT Issue introduced in #60023 Commits ------- 44cf8da [Lock] Fix unserializing already serialized Key payloads
2 parents 92f83e4 + 44cf8da commit c43a084

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Symfony/Component/Lock/Key.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Jérémy Derussé <jeremy@derusse.com>
2020
*/
21-
final class Key
21+
final class Key implements \Stringable
2222
{
2323
private ?float $expiringTime = null;
2424
private array $state = [];
@@ -91,9 +91,9 @@ public function isExpired(): bool
9191

9292
public function __unserialize(array $data): void
9393
{
94-
$this->resource = $data['resource'];
95-
$this->expiringTime = $data['expiringTime'];
96-
$this->state = $data['state'];
94+
$this->resource = $data['resource'] ?? $data["\0".self::class."\0resource"];
95+
$this->expiringTime = $data['expiringTime'] ?? $data["\0".self::class."\0expiringTime"] ?? null;
96+
$this->state = $data['state'] ?? $data["\0".self::class."\0state"] ?? [];
9797
}
9898

9999
public function __serialize(): array

src/Symfony/Component/Lock/Tests/KeyTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ public function testSerialize()
3131
$this->assertEqualsWithDelta($key->getRemainingLifetime(), $copy->getRemainingLifetime(), 0.001);
3232
}
3333

34+
public function testLegacyPayloadCanBeUnserialized()
35+
{
36+
$serialized = base64_decode('TzoyNjoiU3ltZm9ueVxDb21wb25lbnRcTG9ja1xLZXkiOjM6e3M6MzY6IgBTeW1mb255XENvbXBvbmVudFxMb2NrXEtleQByZXNvdXJjZSI7czo2OiJsZWdhY3kiO3M6NDA6IgBTeW1mb255XENvbXBvbmVudFxMb2NrXEtleQBleHBpcmluZ1RpbWUiO047czozMzoiAFN5bWZvbnlcQ29tcG9uZW50XExvY2tcS2V5AHN0YXRlIjthOjA6e319', true);
37+
38+
$key = unserialize($serialized, ['allowed_classes' => [Key::class]]);
39+
40+
$this->assertInstanceOf(Key::class, $key);
41+
$this->assertSame('legacy', (string) $key);
42+
$this->assertNull($key->getRemainingLifetime());
43+
}
44+
3445
public function testUnserialize()
3546
{
3647
$key = new Key(__METHOD__);

0 commit comments

Comments
 (0)