Skip to content

Commit 7102e53

Browse files
committed
AC-10686: [PCI] SRI enabled on payment pages.
1 parent daa6e09 commit 7102e53

File tree

3 files changed

+136
-131
lines changed

3 files changed

+136
-131
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Csp\Model\Deploy\Package\Processor\PostProcessor;
9+
10+
use Magento\Deploy\Package\Package;
11+
use Magento\Deploy\Package\PackageFileFactory;
12+
use Magento\Deploy\Service\DeployStaticFile;
13+
use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
14+
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\Exception\FileSystemException;
16+
use Magento\Framework\FileSystem;
17+
use Magento\Framework\View\Asset\Minification;
18+
use Magento\Framework\View\Asset\RepositoryMap;
19+
use Magento\Csp\Model\SubresourceIntegrityFactory;
20+
use Magento\Csp\Model\SubresourceIntegrity\HashGenerator;
21+
use Magento\Framework\Filesystem\DriverInterface;
22+
use Magento\Csp\Model\SubresourceIntegrityCollector;
23+
24+
class Map extends \Magento\Deploy\Package\Processor\PostProcessor\Map
25+
{
26+
27+
/**
28+
* @var HashGenerator
29+
*/
30+
private HashGenerator $hashGenerator;
31+
32+
/**
33+
* @var SubresourceIntegrityCollector
34+
*/
35+
private SubresourceIntegrityCollector $integrityCollector;
36+
37+
/**
38+
* @var SubresourceIntegrityFactory
39+
*/
40+
private SubresourceIntegrityFactory $integrityFactory;
41+
42+
/**
43+
* @var Minification
44+
*/
45+
private Minification $minification;
46+
47+
/**
48+
* @var DriverInterface
49+
*/
50+
private DriverInterface $driver;
51+
52+
/**
53+
* @var FileSystem
54+
*/
55+
private FileSystem $filesystem;
56+
57+
/**
58+
* @param DeployStaticFile $deployStaticFile
59+
* @param PhpFormatter $formatter
60+
* @param PackageFileFactory $packageFileFactory
61+
* @param Minification $minification
62+
* @param SubresourceIntegrityFactory $integrityFactory
63+
* @param HashGenerator $hashGenerator
64+
* @param DriverInterface $driver
65+
* @param SubresourceIntegrityCollector $integrityCollector
66+
* @param FileSystem $filesystem
67+
*/
68+
public function __construct(
69+
DeployStaticFile $deployStaticFile,
70+
PhpFormatter $formatter,
71+
PackageFileFactory $packageFileFactory,
72+
Minification $minification,
73+
SubresourceIntegrityFactory $integrityFactory,
74+
HashGenerator $hashGenerator,
75+
DriverInterface $driver,
76+
SubresourceIntegrityCollector $integrityCollector,
77+
Filesystem $filesystem
78+
) {
79+
$this->minification = $minification;
80+
$this->integrityFactory = $integrityFactory;
81+
$this->hashGenerator = $hashGenerator;
82+
$this->driver = $driver;
83+
$this->integrityCollector = $integrityCollector;
84+
$this->filesystem = $filesystem;
85+
parent::__construct($deployStaticFile, $formatter, $packageFileFactory, $minification);
86+
}
87+
88+
/**
89+
* @inheritdoc
90+
* @throws FileSystemException
91+
*/
92+
public function process(Package $package, array $options)
93+
{
94+
parent::process($package, $options);
95+
$fileName = $this->minification->addMinifiedSign(RepositoryMap::REQUIRE_JS_MAP_NAME);
96+
$path = $package->getPath();
97+
$relativePath = $path . DIRECTORY_SEPARATOR . $fileName;
98+
99+
if ($this->fileExists($relativePath)) {
100+
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
101+
$absolutePath = $dir->getAbsolutePath($relativePath);
102+
$fileContent = $this->driver->fileGetContents($absolutePath);
103+
104+
if ($fileContent) {
105+
$integrity = $this->integrityFactory->create(
106+
[
107+
"data" => [
108+
'hash' => $this->hashGenerator->generate($fileContent),
109+
'path' => $relativePath
110+
]
111+
]
112+
);
113+
$this->integrityCollector->collect($integrity);
114+
}
115+
}
116+
}
117+
118+
/**
119+
* Check if file exist
120+
*
121+
* @param string $path
122+
* @return bool
123+
* @throws FileSystemException
124+
*/
125+
private function fileExists(string $path): bool
126+
{
127+
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
128+
return $dir->isExist($path);
129+
}
130+
}

app/code/Magento/Csp/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,10 @@
128128
<type name="Magento\RequireJs\Model\FileManager">
129129
<plugin name="addResourceIntegrityAfterAssetCreate" type="Magento\Csp\Plugin\GenerateAssetIntegrity"/>
130130
</type>
131+
<preference for="Magento\Deploy\Package\Processor\PostProcessor\Map" type="Magento\Csp\Model\Deploy\Package\Processor\PostProcessor\Map" />
132+
<type name="Magento\Csp\Model\Deploy\Package\Processor\PostProcessor\Map">
133+
<arguments>
134+
<argument name="driver" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
135+
</arguments>
136+
</type>
131137
</config>

dev/tests/integration/testsuite/Magento/Csp/Model/SubresourceIntegrity/SubresourceIntegrityRepositoryTest.php

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)