Skip to content

Commit 8a09223

Browse files
committed
Fix incorrect serialized data mangling
This takes into account the fact that the property id string consisting of the fully-qualified class name and property name may be more than 100 characters long and its length may thus take up more than 2 characters in the serialized format. Fixes #61806
1 parent a638b29 commit 8a09223

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

ResourceCheckerConfigCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function write(string $content, ?array $metadata = null): void
126126
}
127127

128128
$ser = preg_replace_callback('/;O:(\d+):"/', static fn ($m) => ';O:'.(9 + $m[1]).':"Tracking\\', $ser);
129-
$ser = preg_replace_callback('/s:(\d+):"\0[^\0]++\0/', static fn ($m) => 's:'.($m[1] - \strlen($m[0]) + 6).':"', $ser);
129+
$ser = preg_replace_callback('/s:(\d+):"(\0[^\0]++\0)/', static fn ($m) => 's:'.($m[1] - \strlen($m[2])).':"', $ser);
130130
$ser = unserialize($ser, ['allowed_classes' => false]);
131131
$ser = @json_encode($ser, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE) ?: [];
132132
$ser = str_replace('"__PHP_Incomplete_Class_Name":"Tracking\\\\', '"@type":"', $ser);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\Config\Tests\Fixtures;
4+
5+
use Symfony\Component\Config\Resource\ResourceInterface;
6+
7+
class ResourceWithVeryVeryVeryVeryVeryVeryVeryVeryLongName implements ResourceInterface
8+
{
9+
public function __construct(private string $resource)
10+
{
11+
}
12+
13+
public function __toString(): string
14+
{
15+
return __CLASS__;
16+
}
17+
}

Tests/ResourceCheckerConfigCacheTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Resource\FileResource;
1616
use Symfony\Component\Config\ResourceCheckerConfigCache;
1717
use Symfony\Component\Config\ResourceCheckerInterface;
18+
use Symfony\Component\Config\Tests\Fixtures\ResourceWithVeryVeryVeryVeryVeryVeryVeryVeryLongName;
1819
use Symfony\Component\Config\Tests\Resource\ResourceStub;
1920

2021
class ResourceCheckerConfigCacheTest extends TestCase
@@ -170,4 +171,21 @@ public function testCacheWithCustomMetaFile()
170171
],
171172
], \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
172173
}
174+
175+
public function testCacheWithResourceWithLongPropertyId()
176+
{
177+
$cache = new ResourceCheckerConfigCache($this->cacheFile);
178+
$cache->write('foo', [new ResourceWithVeryVeryVeryVeryVeryVeryVeryVeryLongName(__FILE__)]);
179+
180+
$this->assertStringNotEqualsFile($this->cacheFile.'.meta', '');
181+
182+
$this->assertStringEqualsFile($this->cacheFile.'.meta.json', json_encode([
183+
'resources' => [
184+
[
185+
'@type' => ResourceWithVeryVeryVeryVeryVeryVeryVeryVeryLongName::class,
186+
'resource' => __FILE__,
187+
],
188+
],
189+
], \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
190+
}
173191
}

0 commit comments

Comments
 (0)