Skip to content

Commit 212f5b7

Browse files
committed
minor symfony#57926 [AssetMaper] Fix entropy of hash in public path (nicolas-grekas)
This PR was merged into the 7.2 branch. Discussion ---------- [AssetMaper] Fix entropy of hash in public path | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix # | License | MIT $digest is already in hexa. Turning it in base64 requires first making it binary. Also, we have to use the url-safe variant of base64. Commits ------- b7a860f [AssetMaper] Fix entropy of hash in public path
2 parents 376e594 + b7a860f commit 212f5b7

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

src/Symfony/Component/AssetMapper/Factory/MappedAssetFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ private function getPublicPath(MappedAsset $asset, ?string $content): ?string
116116
if ($isPredigested) {
117117
return $this->assetsPathResolver->resolvePublicPath($asset->logicalPath);
118118
}
119-
120-
$digest = substr(base64_encode($digest), 0, self::PUBLIC_DIGEST_LENGTH);
121-
$digestedPath = preg_replace_callback('/\.(\w+)$/', fn ($matches) => "-{$digest}{$matches[0]}", $asset->logicalPath);
119+
$digest = base64_encode(hex2bin($digest));
120+
$digest = substr($digest, 0, self::PUBLIC_DIGEST_LENGTH);
121+
$digest = strtr($digest, '+/', '-_');
122+
$digestedPath = preg_replace('/\.(\w+)$/', "-{$digest}\\0", $asset->logicalPath);
122123

123124
return $this->assetsPathResolver->resolvePublicPath($digestedPath);
124125
}

src/Symfony/Component/AssetMapper/Tests/AssetMapperDevServerSubscriberFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testGettingAssetWorks()
2121
{
2222
$client = static::createClient();
2323

24-
$client->request('GET', '/assets/file1-YjM0NDV.css');
24+
$client->request('GET', '/assets/file1-s0Rct6h.css');
2525
$response = $client->getResponse();
2626
$this->assertSame(200, $response->getStatusCode());
2727
$this->assertInstanceOf(BinaryFileResponse::class, $response);
@@ -39,7 +39,7 @@ public function testGettingAssetWithNonAsciiFilenameWorks()
3939
{
4040
$client = static::createClient();
4141

42-
$client->request('GET', '/assets/voilà-NjM0NDQ.css');
42+
$client->request('GET', '/assets/voilà-Y0RCLaa.css');
4343
$response = $client->getResponse();
4444
$this->assertSame(200, $response->getStatusCode());
4545
$this->assertSame(<<<EOF

src/Symfony/Component/AssetMapper/Tests/Command/AssetMapperCompileCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public function testAssetsAreCompiled()
5959
// match Compiling \d+ assets
6060
$this->assertMatchesRegularExpression('/Compiled \d+ assets/', $tester->getDisplay());
6161

62-
$this->assertFileExists($targetBuildDir.'/subdir/file5-ZjRmZGM.js');
62+
$this->assertFileExists($targetBuildDir.'/subdir/file5-9P3Dc3X.js');
6363
$this->assertSame(<<<EOF
6464
import '../file4.js';
6565
console.log('file5.js');
6666
67-
EOF, $this->filesystem->readFile($targetBuildDir.'/subdir/file5-ZjRmZGM.js'));
67+
EOF, $this->filesystem->readFile($targetBuildDir.'/subdir/file5-9P3Dc3X.js'));
6868

6969
$finder = new Finder();
7070
$finder->in($targetBuildDir)->files();

src/Symfony/Component/AssetMapper/Tests/MapperAwareAssetPackageIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testDefaultAssetPackageIsDecorated()
3737
{
3838
$packages = $this->kernel->getContainer()->get('public.assets.packages');
3939
\assert($packages instanceof Packages);
40-
$this->assertSame('/assets/file1-YjM0NDV.css', $packages->getUrl('file1.css'));
40+
$this->assertSame('/assets/file1-s0Rct6h.css', $packages->getUrl('file1.css'));
4141
$this->assertSame('/non-existent.css', $packages->getUrl('non-existent.css'));
4242
}
4343
}

0 commit comments

Comments
 (0)