|
| 1 | +diff --git a/vendor/magento/module-csp/Model/Deploy/Package/Processor/PostProcessor/Integrity.php b/vendor/magento/module-csp/Model/Deploy/Package/Processor/PostProcessor/Integrity.php |
| 2 | +index ba77e172355d5..fd498faaf183e 100644 |
| 3 | +--- a/vendor/magento/module-csp/Model/Deploy/Package/Processor/PostProcessor/Integrity.php |
| 4 | ++++ b/vendor/magento/module-csp/Model/Deploy/Package/Processor/PostProcessor/Integrity.php |
| 5 | +@@ -16,6 +16,7 @@ |
| 6 | + use Magento\Deploy\Package\Processor\ProcessorInterface; |
| 7 | + use Magento\Csp\Model\SubresourceIntegrity\HashGenerator; |
| 8 | + use Magento\Framework\App\ObjectManager; |
| 9 | ++use Magento\Framework\View\Asset\Minification; |
| 10 | + use Psr\Log\LoggerInterface; |
| 11 | + |
| 12 | + /** |
| 13 | +@@ -53,6 +54,11 @@ class Integrity implements ProcessorInterface |
| 14 | + */ |
| 15 | + private LoggerInterface $logger; |
| 16 | + |
| 17 | ++ /** |
| 18 | ++ * @var Minification |
| 19 | ++ */ |
| 20 | ++ private Minification $minification; |
| 21 | ++ |
| 22 | + /** |
| 23 | + * @param Filesystem $filesystem |
| 24 | + * @param HashGenerator $hashGenerator |
| 25 | +@@ -60,6 +66,7 @@ class Integrity implements ProcessorInterface |
| 26 | + * @param SubresourceIntegrityCollector $integrityCollector |
| 27 | + * @param LoggerInterface|null $logger |
| 28 | + * @param SubresourceIntegrityRepositoryPool|null $repositoryPool |
| 29 | ++ * @param Minification|null $minification |
| 30 | + */ |
| 31 | + public function __construct( |
| 32 | + Filesystem $filesystem, |
| 33 | +@@ -67,7 +74,8 @@ public function __construct( |
| 34 | + SubresourceIntegrityFactory $integrityFactory, |
| 35 | + SubresourceIntegrityCollector $integrityCollector, |
| 36 | + ?LoggerInterface $logger = null, |
| 37 | +- ?SubresourceIntegrityRepositoryPool $repositoryPool = null |
| 38 | ++ ?SubresourceIntegrityRepositoryPool $repositoryPool = null, |
| 39 | ++ ?Minification $minification = null |
| 40 | + ) { |
| 41 | + $this->filesystem = $filesystem; |
| 42 | + $this->hashGenerator = $hashGenerator; |
| 43 | +@@ -76,6 +84,8 @@ public function __construct( |
| 44 | + $this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class); |
| 45 | + $this->repositoryPool = $repositoryPool ?? |
| 46 | + ObjectManager::getInstance()->get(SubresourceIntegrityRepositoryPool::class); |
| 47 | ++ $this->minification = $minification ?? |
| 48 | ++ ObjectManager::getInstance()->get(Minification::class); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | +@@ -84,18 +94,22 @@ public function __construct( |
| 53 | + public function process(Package $package, array $options): bool |
| 54 | + { |
| 55 | + $staticDir = $this->filesystem->getDirectoryRead( |
| 56 | +- DirectoryList::ROOT |
| 57 | ++ DirectoryList::STATIC_VIEW |
| 58 | + ); |
| 59 | + |
| 60 | + foreach ($package->getFiles() as $file) { |
| 61 | + if (strtolower($file->getExtension()) === "js") { |
| 62 | ++ $deployedFilePath = $this->minification->addMinifiedSign( |
| 63 | ++ $file->getDeployedFilePath() |
| 64 | ++ ); |
| 65 | ++ |
| 66 | + $integrity = $this->integrityFactory->create( |
| 67 | + [ |
| 68 | + "data" => [ |
| 69 | + 'hash' => $this->hashGenerator->generate( |
| 70 | +- $staticDir->readFile($file->getSourcePath()) |
| 71 | ++ $staticDir->readFile($deployedFilePath) |
| 72 | + ), |
| 73 | +- 'path' => $file->getDeployedFilePath() |
| 74 | ++ 'path' => $deployedFilePath |
| 75 | + ] |
| 76 | + ] |
| 77 | + ); |
| 78 | +diff --git a/vendor/magento/module-csp/Plugin/GenerateAssetIntegrity.php b/vendor/magento/module-csp/Plugin/GenerateAssetIntegrity.php |
| 79 | +index 2b461588fa5ad..5c66290652da2 100644 |
| 80 | +--- a/vendor/magento/module-csp/Plugin/GenerateAssetIntegrity.php |
| 81 | ++++ b/vendor/magento/module-csp/Plugin/GenerateAssetIntegrity.php |
| 82 | +@@ -70,22 +70,56 @@ public function afterCreateRequireJsConfigAsset( |
| 83 | + File $result |
| 84 | + ): File { |
| 85 | + if (PHP_SAPI == 'cli') { |
| 86 | +- if (in_array($result->getContentType(), self::CONTENT_TYPES)) { |
| 87 | +- $integrity = $this->integrityFactory->create( |
| 88 | +- [ |
| 89 | +- "data" => [ |
| 90 | +- 'hash' => $this->hashGenerator->generate( |
| 91 | +- $result->getContent() |
| 92 | +- ), |
| 93 | +- 'path' => $result->getPath() |
| 94 | +- ] |
| 95 | +- ] |
| 96 | +- ); |
| 97 | ++ $this->generateIntegrity($result); |
| 98 | ++ } |
| 99 | + |
| 100 | +- $this->integrityCollector->collect($integrity); |
| 101 | +- } |
| 102 | ++ return $result; |
| 103 | ++ } |
| 104 | ++ |
| 105 | ++ /** |
| 106 | ++ * Generates integrity for '.min' files resolver. |
| 107 | ++ * |
| 108 | ++ * @param FileManager $subject |
| 109 | ++ * @param File $result |
| 110 | ++ * |
| 111 | ++ * @return File |
| 112 | ++ * |
| 113 | ++ * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 114 | ++ */ |
| 115 | ++ public function afterCreateMinResolverAsset( |
| 116 | ++ FileManager $subject, |
| 117 | ++ File $result |
| 118 | ++ ): File { |
| 119 | ++ if (PHP_SAPI == 'cli') { |
| 120 | ++ $this->generateIntegrity($result); |
| 121 | + } |
| 122 | + |
| 123 | + return $result; |
| 124 | + } |
| 125 | ++ |
| 126 | ++ /** |
| 127 | ++ * Generates integrity for a given asset. |
| 128 | ++ * |
| 129 | ++ * @param File $file |
| 130 | ++ * |
| 131 | ++ * @return void |
| 132 | ++ */ |
| 133 | ++ private function generateIntegrity( |
| 134 | ++ File $file |
| 135 | ++ ): void { |
| 136 | ++ if (in_array($file->getContentType(), self::CONTENT_TYPES)) { |
| 137 | ++ $integrity = $this->integrityFactory->create( |
| 138 | ++ [ |
| 139 | ++ "data" => [ |
| 140 | ++ 'hash' => $this->hashGenerator->generate( |
| 141 | ++ $file->getContent() |
| 142 | ++ ), |
| 143 | ++ 'path' => $file->getPath() |
| 144 | ++ ] |
| 145 | ++ ] |
| 146 | ++ ); |
| 147 | ++ |
| 148 | ++ $this->integrityCollector->collect($integrity); |
| 149 | ++ } |
| 150 | ++ } |
| 151 | + } |
| 152 | + |
0 commit comments