Skip to content

Commit 5bebddb

Browse files
AC-10686: [PCI] SRI enabled on payment pages
1 parent 4555d64 commit 5bebddb

File tree

4 files changed

+41
-67
lines changed

4 files changed

+41
-67
lines changed

app/code/Magento/Csp/Model/SubresourceIntegrity.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
class SubresourceIntegrity extends \Magento\Framework\DataObject
1414
{
15-
1615
/**
1716
* Expected asset content type.
1817
*
@@ -25,7 +24,7 @@ class SubresourceIntegrity extends \Magento\Framework\DataObject
2524
*
2625
* @return string|null
2726
*/
28-
public function getUrl(): string|null
27+
public function getUrl(): ?string
2928
{
3029
return $this->getData("url");
3130
}
@@ -35,7 +34,7 @@ public function getUrl(): string|null
3534
*
3635
* @return string|null
3736
*/
38-
public function getHash(): string|null
37+
public function getHash(): ?string
3938
{
4039
return $this->getData("hash");
4140
}

app/code/Magento/Csp/Model/SubresourceIntegrity/File.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121
class File
2222
{
23-
2423
/**
2524
* @var Filesystem
2625
*/

app/code/Magento/Csp/Model/SubresourceIntegrityRepository.php

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@
77

88
namespace Magento\Csp\Model;
99

10-
use Psr\Log\LoggerInterface;
1110
use Magento\Framework\App\CacheInterface;
12-
use Magento\Framework\App\DeploymentConfig;
13-
use Magento\Framework\Exception\FileSystemException;
14-
use Magento\Framework\Exception\RuntimeException;
1511
use Magento\Framework\Serialize\SerializerInterface;
1612

1713
/**
18-
* Class contains methods equivalent to repository design to manage SRI hashes in cache
14+
* Class contains methods equivalent to repository design to manage SRI hashes in cache.
1915
*/
2016
class SubresourceIntegrityRepository
2117
{
@@ -24,22 +20,17 @@ class SubresourceIntegrityRepository
2420
*
2521
* @var string
2622
*/
27-
private const CACHE_PREFIX = 'INTEGRITY_HASH';
23+
private const CACHE_PREFIX = 'INTEGRITY';
2824

2925
/**
30-
* @var CacheInterface
31-
*/
32-
private CacheInterface $cache;
33-
34-
/**
35-
* @var LoggerInterface
26+
* @var array|null
3627
*/
37-
private LoggerInterface $logger;
28+
private ?array $data = null;
3829

3930
/**
40-
* @var DeploymentConfig
31+
* @var CacheInterface
4132
*/
42-
private DeploymentConfig $config;
33+
private CacheInterface $cache;
4334

4435
/**
4536
* @var SerializerInterface
@@ -52,24 +43,16 @@ class SubresourceIntegrityRepository
5243
private SubresourceIntegrityFactory $integrityFactory;
5344

5445
/**
55-
* constructor
56-
*
5746
* @param CacheInterface $cache
58-
* @param LoggerInterface $logger
59-
* @param DeploymentConfig $config
6047
* @param SerializerInterface $serializer
6148
* @param SubresourceIntegrityFactory $integrityFactory
6249
*/
6350
public function __construct(
6451
CacheInterface $cache,
65-
LoggerInterface $logger,
66-
DeploymentConfig $config,
6752
SerializerInterface $serializer,
6853
SubresourceIntegrityFactory $integrityFactory
6954
) {
7055
$this->cache = $cache;
71-
$this->logger = $logger;
72-
$this->config = $config;
7356
$this->serializer = $serializer;
7457
$this->integrityFactory = $integrityFactory;
7558
}
@@ -83,17 +66,15 @@ public function __construct(
8366
*/
8467
public function getByUrl(string $url): ?SubresourceIntegrity
8568
{
86-
$integrity = $this->cache->load(
87-
self::CACHE_PREFIX . $url
88-
);
69+
$data = $this->getData();
8970

90-
if (!$integrity) {
91-
return null;
71+
if (isset($data[$url])) {
72+
return $this->integrityFactory->create(
73+
["data" => $data[$url]]
74+
);
9275
}
9376

94-
return $this->integrityFactory->create(
95-
["data" => $this->serializer->unserialize($integrity)]
96-
);
77+
return null;
9778
}
9879

9980
/**
@@ -105,29 +86,12 @@ public function getAll(): array
10586
{
10687
$result = [];
10788

108-
try {
109-
$defaultCachePrefix = $this->config->get(
110-
"cache/frontend/default/id_prefix"
89+
foreach ($this->getData() as $integrity) {
90+
$result[] = $this->integrityFactory->create(
91+
[
92+
"data" => $integrity
93+
]
11194
);
112-
113-
$cacheIds = $this->cache->getFrontend()->getLowLevelFrontend()
114-
->getIdsMatchingAnyTags(
115-
[$defaultCachePrefix . self::CACHE_PREFIX]
116-
);
117-
118-
foreach ($cacheIds as $id) {
119-
$integrity = $this->cache->load($id);
120-
121-
if ($integrity) {
122-
$result[] = $this->integrityFactory->create(
123-
[
124-
"data" => $this->serializer->unserialize($integrity)
125-
]
126-
);
127-
}
128-
}
129-
} catch (\Exception $e) {
130-
$this->logger->critical($e);
13195
}
13296

13397
return $result;
@@ -142,24 +106,35 @@ public function getAll(): array
142106
*/
143107
public function save(SubresourceIntegrity $integrity): bool
144108
{
109+
$data = $this->getData();
110+
111+
$data[$integrity->getUrl()] = [
112+
"url" => $integrity->getUrl(),
113+
"hash" => $integrity->getHash()
114+
];
115+
116+
$this->data = $data;
117+
145118
return $this->cache->save(
146-
$this->serializer->serialize($integrity->getData()),
147-
self::CACHE_PREFIX . $integrity->getUrl(),
119+
$this->serializer->serialize($this->data),
120+
self::CACHE_PREFIX,
148121
[self::CACHE_PREFIX]
149122
);
150123
}
151124

152125
/**
153-
* Clear contents of cache
154-
*
155-
* @param string $url
126+
* Loads integrity data from a storage.
156127
*
157-
* @return bool
128+
* @return array
158129
*/
159-
public function deleteByUrl(string $url): bool
130+
private function getData(): array
160131
{
161-
return $this->cache->clean(
162-
[self::CACHE_PREFIX . $url]
163-
);
132+
if ($this->data === null) {
133+
$cache = $this->cache->load(self::CACHE_PREFIX);
134+
135+
$this->data = $cache ? $this->serializer->unserialize($cache) : [];
136+
}
137+
138+
return $this->data;
164139
}
165140
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<item name="whitelist" xsi:type="object" sortOrder="2">Magento\Csp\Model\Collector\CspWhitelistXmlCollector\Proxy</item>
3434
<item name="controller" xsi:type="object" sortOrder="100">Magento\Csp\Model\Collector\ControllerCollector\Proxy</item>
3535
<item name="dynamic" xsi:type="object" sortOrder="3">Magento\Csp\Model\Collector\DynamicCollector\Proxy</item>
36+
<item name="sri" xsi:type="object" sortOrder="2">Magento\Csp\Model\Collector\SubresourceIntegrityCollector\Proxy</item>
3637
</argument>
3738
<argument name="mergers" xsi:type="array">
3839
<item name="composite" xsi:type="object">Magento\Csp\Model\Collector\MergerInterface</item>

0 commit comments

Comments
 (0)