Skip to content

Commit 36233da

Browse files
committed
AC-15165: SRI Improvements.
Use global static array for SRI collector.
1 parent 52f4632 commit 36233da

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

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

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

88
namespace Magento\Csp\Model;
99

10+
use Magento\Framework\App\ObjectManager;
11+
use Psr\Log\LoggerInterface;
12+
1013
/**
1114
* Collector of Integrity objects.
15+
*
16+
* Uses static storage to persist data across ObjectManager instances
17+
* during area emulation in static content deployment.
1218
*/
1319
class SubresourceIntegrityCollector
1420
{
1521
/**
22+
* Global storage that persists across ObjectManager instances
1623
* @var array
1724
*/
18-
private array $data = [];
25+
private static array $globalData = [];
1926

2027
/**
21-
* @param array $data
28+
* @var LoggerInterface
2229
*/
23-
public function __construct(array $data = [])
24-
{
25-
$this->data = $data;
30+
private LoggerInterface $logger;
31+
32+
/**
33+
* @param LoggerInterface|null $logger
34+
*/
35+
public function __construct(?LoggerInterface $logger = null) {
36+
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
37+
38+
$this->logger->info('SRI Collector: Initialized with ' . count(self::$globalData) . ' objects (global storage)');
2639
}
2740

2841
/**
@@ -34,7 +47,8 @@ public function __construct(array $data = [])
3447
*/
3548
public function collect(SubresourceIntegrity $integrity): void
3649
{
37-
$this->data[] = $integrity;
50+
self::$globalData[] = $integrity;
51+
$this->logger->info('SRI Collector: Collected object, total: ' . count(self::$globalData));
3852
}
3953

4054
/**
@@ -44,6 +58,20 @@ public function collect(SubresourceIntegrity $integrity): void
4458
*/
4559
public function release(): array
4660
{
47-
return $this->data;
61+
$count = count(self::$globalData);
62+
$this->logger->info('SRI Collector: Releasing ' . $count . ' objects');
63+
return self::$globalData;
64+
}
65+
66+
/**
67+
* Clear all collected data.
68+
*
69+
* @return void
70+
*/
71+
public function clear(): void
72+
{
73+
$count = count(self::$globalData);
74+
self::$globalData = [];
75+
$this->logger->info('SRI Collector: Cleared ' . $count . ' objects');
4876
}
4977
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Deploy\Console\DeployStaticOptions;
1313
use Magento\Deploy\Service\DeployStaticContent;
1414
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
15+
use Magento\Csp\Model\SubresourceIntegrityCollector;
1516

1617
/**
1718
* Plugin that removes existing integrity hashes for all assets.
@@ -23,13 +24,21 @@ class RemoveAllAssetIntegrityHashes
2324
*/
2425
private SubresourceIntegrityRepositoryPool $integrityRepositoryPool;
2526

27+
/**
28+
* @var SubresourceIntegrityCollector
29+
*/
30+
private SubresourceIntegrityCollector $integrityCollector;
31+
2632
/**
2733
* @param SubresourceIntegrityRepositoryPool $integrityRepositoryPool
34+
* @param SubresourceIntegrityCollector $integrityCollector
2835
*/
2936
public function __construct(
30-
SubresourceIntegrityRepositoryPool $integrityRepositoryPool
37+
SubresourceIntegrityRepositoryPool $integrityRepositoryPool,
38+
SubresourceIntegrityCollector $integrityCollector
3139
) {
3240
$this->integrityRepositoryPool = $integrityRepositoryPool;
41+
$this->integrityCollector = $integrityCollector;
3342
}
3443

3544
/**
@@ -47,10 +56,14 @@ public function beforeDeploy(
4756
array $options
4857
): void {
4958
if (PHP_SAPI == 'cli' && !$this->isRefreshContentVersionOnly($options)) {
59+
// Clear stored integrity hashes from all areas
5060
foreach ([Package::BASE_AREA, Area::AREA_FRONTEND, Area::AREA_ADMINHTML] as $area) {
5161
$this->integrityRepositoryPool->get($area)
5262
->deleteAll();
5363
}
64+
65+
// Clear any leftover in-memory integrity hashes from previous runs
66+
$this->integrityCollector->clear();
5467
}
5568
}
5669

0 commit comments

Comments
 (0)