Skip to content

Commit 62ab26c

Browse files
AC-10686: [PCI] SRI enabled on payment pages
1 parent a72e215 commit 62ab26c

12 files changed

+296
-357
lines changed

app/code/Magento/Csp/Block/Sri/Hashes.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
use Magento\Framework\App\ObjectManager;
99
use Magento\Framework\View\Element\Template;
10-
use Magento\Framework\Exception\FileSystemException;
11-
use Magento\Framework\Exception\RuntimeException;
1210
use Magento\Framework\Serialize\SerializerInterface;
1311
use Magento\Framework\View\Element\Template\Context;
14-
use Magento\Csp\Model\SubresourceIntegrityRepository;
12+
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
1513

1614
/**
1715
* Block for Subresource Integrity hashes rendering.
16+
*
1817
* @api
1918
*/
2019
class Hashes extends Template
@@ -25,26 +24,26 @@ class Hashes extends Template
2524
private SerializerInterface $serializer;
2625

2726
/**
28-
* @var SubresourceIntegrityRepository
27+
* @var SubresourceIntegrityRepositoryPool
2928
*/
30-
private SubresourceIntegrityRepository $integrityRepository;
29+
private SubresourceIntegrityRepositoryPool $integrityRepositoryPool;
3130

3231
/**
3332
* @param Context $context
3433
* @param array $data
35-
* @param SubresourceIntegrityRepository|null $integrityRepository
34+
* @param SubresourceIntegrityRepositoryPool|null $integrityRepositoryPool
3635
* @param SerializerInterface|null $serializer
3736
*/
3837
public function __construct(
3938
Context $context,
4039
array $data = [],
41-
?SubresourceIntegrityRepository $integrityRepository = null,
40+
?SubresourceIntegrityRepositoryPool $integrityRepositoryPool = null,
4241
?SerializerInterface $serializer = null
4342
) {
4443
parent::__construct($context, $data);
4544

46-
$this->integrityRepository = $integrityRepository ?: ObjectManager::getInstance()
47-
->get(SubresourceIntegrityRepository::class);
45+
$this->integrityRepositoryPool = $integrityRepositoryPool ?: ObjectManager::getInstance()
46+
->get(SubresourceIntegrityRepositoryPool::class);
4847

4948
$this->serializer = $serializer ?: ObjectManager::getInstance()
5049
->get(SerializerInterface::class);
@@ -54,16 +53,16 @@ public function __construct(
5453
* Retrieve serialized integrity hashes.
5554
*
5655
* @return string
57-
*
58-
* @throws FileSystemException
59-
* @throws RuntimeException
6056
*/
6157
public function getSerialized(): string
6258
{
6359
$result = [];
64-
$assetIntegrity = $this->integrityRepository->getAll();
6560

66-
foreach ($assetIntegrity as $integrity) {
61+
$integrityRepository = $this->integrityRepositoryPool->get(
62+
$this->getRequest()->getFullActionName()
63+
);
64+
65+
foreach ($integrityRepository->getAll() as $integrity) {
6766
$result[$integrity->getUrl()] = $integrity->getHash();
6867
}
6968

app/code/Magento/Csp/Model/Collector/SubresourceIntegrityCollector.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,36 @@
77

88
namespace Magento\Csp\Model\Collector;
99

10+
use Magento\Framework\App\Request\Http;
1011
use Magento\Csp\Model\Policy\FetchPolicy;
1112
use Magento\Csp\Api\PolicyCollectorInterface;
12-
use Magento\Csp\Model\SubresourceIntegrityRepository;
13+
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
1314

1415
/**
1516
* Collects policies auto-defined by Subresource Integrity.
1617
*/
1718
class SubresourceIntegrityCollector implements PolicyCollectorInterface
1819
{
1920
/**
20-
* @var SubresourceIntegrityRepository
21+
* @var Http
2122
*/
22-
private SubresourceIntegrityRepository $integrityRepository;
23+
private Http $request;
2324

2425
/**
25-
* @param SubresourceIntegrityRepository $integrityRepository
26+
* @var SubresourceIntegrityRepositoryPool
27+
*/
28+
private SubresourceIntegrityRepositoryPool $integrityRepositoryPool;
29+
30+
/**
31+
* @param Http $request
32+
* @param SubresourceIntegrityRepositoryPool $integrityRepositoryPool
2633
*/
2734
public function __construct(
28-
SubresourceIntegrityRepository $integrityRepository
35+
Http $request,
36+
SubresourceIntegrityRepositoryPool $integrityRepositoryPool
2937
) {
30-
$this->integrityRepository = $integrityRepository;
38+
$this->request = $request;
39+
$this->integrityRepositoryPool = $integrityRepositoryPool;
3140
}
3241

3342
/**
@@ -36,9 +45,12 @@ public function __construct(
3645
public function collect(array $defaultPolicies = []): array
3746
{
3847
$integrityHashes = [];
39-
$assetIntegrity = $this->integrityRepository->getAll();
4048

41-
foreach ($assetIntegrity as $integrity) {
49+
$integrityRepository = $this->integrityRepositoryPool->get(
50+
$this->request->getFullActionName()
51+
);
52+
53+
foreach ($integrityRepository->getAll() as $integrity) {
4254
$hashParts = explode("-", $integrity->getHash());
4355

4456
if (is_array($hashParts) && count($hashParts) > 1) {

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
*/
1313
class SubresourceIntegrity extends \Magento\Framework\DataObject
1414
{
15-
/**
16-
* Expected asset content type.
17-
*
18-
* @var string
19-
*/
20-
public const CONTENT_TYPE = 'js';
21-
2215
/**
2316
* Gets an integrity URL.
2417
*

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

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

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class SubresourceIntegrityRepository
2727
*/
2828
private ?array $data = null;
2929

30+
/**
31+
* @var string|null
32+
*/
33+
private ?string $context;
34+
3035
/**
3136
* @var CacheInterface
3237
*/
@@ -46,15 +51,18 @@ class SubresourceIntegrityRepository
4651
* @param CacheInterface $cache
4752
* @param SerializerInterface $serializer
4853
* @param SubresourceIntegrityFactory $integrityFactory
54+
* @param string|null $context
4955
*/
5056
public function __construct(
5157
CacheInterface $cache,
5258
SerializerInterface $serializer,
53-
SubresourceIntegrityFactory $integrityFactory
59+
SubresourceIntegrityFactory $integrityFactory,
60+
?string $context = null
5461
) {
5562
$this->cache = $cache;
5663
$this->serializer = $serializer;
5764
$this->integrityFactory = $integrityFactory;
65+
$this->context = $context;
5866
}
5967

6068
/**
@@ -117,7 +125,7 @@ public function save(SubresourceIntegrity $integrity): bool
117125

118126
return $this->cache->save(
119127
$this->serializer->serialize($this->data),
120-
self::CACHE_PREFIX,
128+
$this->getCacheKey(),
121129
[self::CACHE_PREFIX]
122130
);
123131
}
@@ -130,11 +138,27 @@ public function save(SubresourceIntegrity $integrity): bool
130138
private function getData(): array
131139
{
132140
if ($this->data === null) {
133-
$cache = $this->cache->load(self::CACHE_PREFIX);
141+
$cache = $this->cache->load($this->getCacheKey());
134142

135143
$this->data = $cache ? $this->serializer->unserialize($cache) : [];
136144
}
137145

138146
return $this->data;
139147
}
148+
149+
/**
150+
* Gets a cache key based on current context.
151+
*
152+
* @return string
153+
*/
154+
private function getCacheKey(): string
155+
{
156+
$cacheKey = self::CACHE_PREFIX;
157+
158+
if ($this->context) {
159+
$cacheKey .= "_" . $this->context;
160+
}
161+
162+
return $cacheKey;
163+
}
140164
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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;
9+
10+
/**
11+
* Pool of subresource integrity repositories.
12+
*/
13+
class SubresourceIntegrityRepositoryPool
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private array $repositories = [];
19+
20+
/**
21+
* @var SubresourceIntegrityRepositoryFactory
22+
*/
23+
private SubresourceIntegrityRepositoryFactory $integrityRepositoryFactory;
24+
25+
/**
26+
* @param SubresourceIntegrityRepositoryFactory $integrityRepositoryFactory
27+
*/
28+
public function __construct(
29+
SubresourceIntegrityRepositoryFactory $integrityRepositoryFactory
30+
) {
31+
$this->integrityRepositoryFactory = $integrityRepositoryFactory;
32+
}
33+
34+
/**
35+
* Gets subresource integrity repository by given context.
36+
*
37+
* @param string $context
38+
*
39+
* @return SubresourceIntegrityRepository
40+
*/
41+
public function get(string $context): SubresourceIntegrityRepository
42+
{
43+
if (!isset($this->repositories[$context])) {
44+
$this->repositories[$context] = $this->integrityRepositoryFactory->create(
45+
[
46+
"context" => $context
47+
]
48+
);
49+
}
50+
51+
return $this->repositories[$context];
52+
}
53+
}

0 commit comments

Comments
 (0)