Skip to content

Commit 770a8de

Browse files
committed
ACP2E-3854: Bundled/Merged JS not part of SRI Hashes
1 parent f7a954b commit 770a8de

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

app/code/Magento/Csp/Plugin/GenerateBundleAssetIntegrity.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Csp\Model\SubresourceIntegrityFactory;
1313
use Magento\Deploy\Service\Bundle;
1414
use Magento\Framework\App\Filesystem\DirectoryList;
15-
use Magento\Framework\App\Utility\Files;
1615
use Magento\Framework\Exception\FileSystemException;
1716
use Magento\Framework\Filesystem;
1817

@@ -38,30 +37,22 @@ class GenerateBundleAssetIntegrity
3837
*/
3938
private Filesystem $filesystem;
4039

41-
/**
42-
* @var Files
43-
*/
44-
private Files $utilityFiles;
45-
4640
/**
4741
* @param HashGenerator $hashGenerator
4842
* @param SubresourceIntegrityFactory $integrityFactory
4943
* @param SubresourceIntegrityCollector $integrityCollector
5044
* @param Filesystem $filesystem
51-
* @param Files $utilityFiles
5245
*/
5346
public function __construct(
5447
HashGenerator $hashGenerator,
5548
SubresourceIntegrityFactory $integrityFactory,
5649
SubresourceIntegrityCollector $integrityCollector,
57-
Filesystem $filesystem,
58-
Files $utilityFiles
50+
Filesystem $filesystem
5951
) {
6052
$this->hashGenerator = $hashGenerator;
6153
$this->integrityFactory = $integrityFactory;
6254
$this->integrityCollector = $integrityCollector;
6355
$this->filesystem = $filesystem;
64-
$this->utilityFiles = $utilityFiles;
6556
}
6657

6758
/**
@@ -79,20 +70,19 @@ public function __construct(
7970
public function afterDeploy(Bundle $subject, ?string $result, string $area, string $theme, string $locale)
8071
{
8172
if (PHP_SAPI == 'cli') {
82-
$pubStaticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
83-
$bundleDir = $pubStaticDir->getAbsolutePath($area . '/' . $theme . '/' . $locale) .
84-
"/". Bundle::BUNDLE_JS_DIR;
85-
$files = $this->utilityFiles->getFiles([$bundleDir], '*.js');
86-
73+
$pubStaticDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
74+
$files = $pubStaticDir->search(
75+
$area ."/" . $theme . "/" . $locale . "/" . Bundle::BUNDLE_JS_DIR . "/*.js"
76+
);
8777
foreach ($files as $file) {
8878
$integrity = $this->integrityFactory->create(
8979
[
9080
"data" => [
9181
'hash' => $this->hashGenerator->generate(
92-
file_get_contents($file)
82+
$pubStaticDir->readFile($file)
9383
),
9484
'path' => $area . '/' . $theme . '/' . $locale .
95-
"/" . Bundle::BUNDLE_JS_DIR . '/' . basename($file)
85+
"/" . Bundle::BUNDLE_JS_DIR . '/' . pathinfo($file)['basename']
9686
]
9787
]
9888
);

app/code/Magento/Csp/Plugin/GenerateMergedAssetIntegrity.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ public function afterMerge(FileExists $subject, ?string $result, array $assetsTo
7575
return $result;
7676
}
7777
$pubStaticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
78-
$absolutePath = $pubStaticDir->getAbsolutePath() . $resultAsset->getRelativeSourceFilePath();
7978
$integrity = $this->integrityFactory->create(
8079
[
8180
"data" => [
82-
'hash' => $this->hashGenerator->generate(file_get_contents($absolutePath)),
81+
'hash' => $this->hashGenerator->generate(
82+
$pubStaticDir->readFile($resultAsset->getRelativeSourceFilePath())
83+
),
8384
'path' => $resultAsset->getRelativeSourceFilePath()
8485
]
8586
]

0 commit comments

Comments
 (0)