Skip to content

Commit b11b406

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

File tree

5 files changed

+153
-10
lines changed

5 files changed

+153
-10
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
* Collector of Integrity objects.
12+
*/
13+
class SubresourceIntegrityCollector
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private array $data = [];
19+
20+
/**
21+
* @param array $data
22+
*/
23+
public function __construct(array $data = []) {
24+
$this->data = $data;
25+
}
26+
27+
/**
28+
* Collects given Integrity object.
29+
*
30+
* @param SubresourceIntegrity $integrity
31+
*
32+
* @return void
33+
*/
34+
public function collect(SubresourceIntegrity $integrity): void
35+
{
36+
$this->data[] = $integrity;
37+
}
38+
39+
/**
40+
* Provides all collected Integrity objects.
41+
*
42+
* @return SubresourceIntegrity[]
43+
*/
44+
public function release(): array
45+
{
46+
return $this->data;
47+
}
48+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ public function save(SubresourceIntegrity $integrity): bool
135135
);
136136
}
137137

138+
/**
139+
* Saves a bunch of Integrity objects.
140+
*
141+
* @param SubresourceIntegrity[] $bunch
142+
*
143+
* @return bool
144+
*/
145+
public function saveBunch(array $bunch): bool
146+
{
147+
$data = $this->getData();
148+
149+
foreach ($bunch as $integrity) {
150+
$data[$integrity->getPath()] = $integrity->getHash();
151+
}
152+
153+
$this->data = $data;
154+
155+
return $this->cache->save(
156+
$this->serializer->serialize($this->data),
157+
$this->getCacheKey(),
158+
[self::CACHE_PREFIX]
159+
);
160+
}
161+
138162
/**
139163
* Deletes all Integrity objects.
140164
*

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Magento\Framework\View\Asset\LocalInterface;
1414
use Magento\Framework\View\Asset\AssetInterface;
1515
use Magento\Csp\Model\SubresourceIntegrityFactory;
16+
use Magento\Csp\Model\SubresourceIntegrityCollector;
1617
use Magento\Csp\Model\SubresourceIntegrity\HashGenerator;
17-
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
1818

1919
/**
2020
* Plugin to add asset integrity value after static content deploy.
@@ -39,23 +39,23 @@ class GenerateAssetIntegrity
3939
private SubresourceIntegrityFactory $integrityFactory;
4040

4141
/**
42-
* @var SubresourceIntegrityRepositoryPool
42+
* @var SubresourceIntegrityCollector
4343
*/
44-
private SubresourceIntegrityRepositoryPool $integrityRepositoryPool;
44+
private SubresourceIntegrityCollector $integrityCollector;
4545

4646
/**
4747
* @param HashGenerator $hashGenerator
4848
* @param SubresourceIntegrityFactory $integrityFactory
49-
* @param SubresourceIntegrityRepositoryPool $integrityRepositoryPool
49+
* @param SubresourceIntegrityCollector $integrityCollector
5050
*/
5151
public function __construct(
5252
HashGenerator $hashGenerator,
5353
SubresourceIntegrityFactory $integrityFactory,
54-
SubresourceIntegrityRepositoryPool $integrityRepositoryPool
54+
SubresourceIntegrityCollector $integrityCollector
5555
) {
5656
$this->hashGenerator = $hashGenerator;
5757
$this->integrityFactory = $integrityFactory;
58-
$this->integrityRepositoryPool = $integrityRepositoryPool;
58+
$this->integrityCollector = $integrityCollector;
5959
}
6060

6161
/**
@@ -126,9 +126,6 @@ private function generateIntegrity(LocalInterface $asset): void
126126
]
127127
);
128128

129-
$area = explode("/", $asset->getPath())[0];
130-
131-
$this->integrityRepositoryPool->get($area)
132-
->save($integrity);
129+
$this->integrityCollector->collect($integrity);
133130
}
134131
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Plugin;
9+
10+
use Magento\Deploy\Service\DeployStaticContent;
11+
use Magento\Csp\Model\SubresourceIntegrityCollector;
12+
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
13+
14+
/**
15+
* Plugin that stores generated integrity hashes for all assets.
16+
*/
17+
class StoreAssetIntegrityHashes
18+
{
19+
/**
20+
* @var SubresourceIntegrityCollector
21+
*/
22+
private SubresourceIntegrityCollector $integrityCollector;
23+
24+
/**
25+
* @var SubresourceIntegrityRepositoryPool
26+
*/
27+
private SubresourceIntegrityRepositoryPool $integrityRepositoryPool;
28+
29+
/**
30+
* @param SubresourceIntegrityCollector $integrityCollector
31+
* @param SubresourceIntegrityRepositoryPool $integrityRepositoryPool
32+
*/
33+
public function __construct(
34+
SubresourceIntegrityCollector $integrityCollector,
35+
SubresourceIntegrityRepositoryPool $integrityRepositoryPool
36+
) {
37+
$this->integrityCollector = $integrityCollector;
38+
$this->integrityRepositoryPool = $integrityRepositoryPool;
39+
}
40+
41+
/**
42+
* Stores generated integrity hashes for all assets
43+
* after static content deploy.
44+
*
45+
* @param DeployStaticContent $subject
46+
* @param mixed $result
47+
* @param array $options
48+
*
49+
* @return void
50+
*
51+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
52+
*/
53+
public function afterDeploy(
54+
DeployStaticContent $subject,
55+
mixed $result,
56+
array $options
57+
): void {
58+
$bunches = [];
59+
60+
foreach ($this->integrityCollector->release() as $integrity) {
61+
$area = explode("/", $integrity->getPath())[0];
62+
63+
$bunches[$area][] = $integrity;
64+
}
65+
66+
foreach ($bunches as $area => $bunch) {
67+
$this->integrityRepositoryPool->get($area)
68+
->saveBunch($bunch);
69+
}
70+
}
71+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@
123123
<type name="Magento\RequireJs\Model\FileManager">
124124
<plugin name="addResourceIntegrityAfterAssetCreate" type="Magento\Csp\Plugin\GenerateAssetIntegrity"/>
125125
</type>
126+
<type name="Magento\Deploy\Service\DeployStaticContent">
127+
<plugin name="storeAssetIntegrityHashes" type="Magento\Csp\Plugin\StoreAssetIntegrityHashes" />
128+
</type>
126129
</config>

0 commit comments

Comments
 (0)