Skip to content

Commit 5e57b66

Browse files
committed
AC-13833: Refactor SRI mechanism to use filesystem for storage.
1 parent 2217274 commit 5e57b66

File tree

2 files changed

+130
-11
lines changed

2 files changed

+130
-11
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Csp\Model\SubresourceIntegrity\Storage;
20+
21+
use Magento\Deploy\Package\Package;
22+
use Magento\Framework\App\Area;
23+
use Magento\Framework\App\Filesystem\DirectoryList;
24+
use Magento\Framework\Exception\FileSystemException;
25+
use Magento\Framework\Filesystem;
26+
27+
/**
28+
* Persistence of sri hashes in the local file system
29+
*/
30+
class File
31+
{
32+
/**
33+
* Constant for sri hashes filename
34+
*/
35+
private const FILENAME = 'sri-hashes.json';
36+
37+
/**
38+
* @var Filesystem
39+
*/
40+
private Filesystem $filesystem;
41+
42+
/**
43+
* Constructor
44+
*
45+
* @param Filesystem $filesystem
46+
*/
47+
public function __construct(
48+
Filesystem $filesystem
49+
) {
50+
$this->filesystem = $filesystem;
51+
}
52+
53+
/**
54+
* Load data from filesystem
55+
*
56+
* @param string|null $area
57+
* @return string|bool
58+
* @throws FileSystemException
59+
*/
60+
public function load(?string $area = null): string|bool
61+
{
62+
$staticDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
63+
64+
if ($area) {
65+
$path = $area . DIRECTORY_SEPARATOR . self::FILENAME;
66+
if ($staticDir->isFile($path)) {
67+
return $staticDir->readFile($path);
68+
}
69+
}
70+
return false;
71+
}
72+
73+
/**
74+
* Save File to Local Storage by area
75+
*
76+
* @param string $data
77+
* @param string|null $area
78+
* @return bool
79+
* @throws FileSystemException
80+
*/
81+
public function save(string $data, ?string $area = null): bool
82+
{
83+
$staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
84+
85+
if ($area) {
86+
$path = $area . DIRECTORY_SEPARATOR . self::FILENAME;
87+
return (bool)$staticDir->writeFile($path, $data, 'w');
88+
}
89+
return false;
90+
}
91+
92+
/**
93+
* Delete all Sri Hashes files
94+
*
95+
* @throws FileSystemException
96+
*/
97+
public function remove():bool
98+
{
99+
$staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
100+
101+
//delete all json files from all areas
102+
foreach ([Package::BASE_AREA, Area::AREA_FRONTEND, Area::AREA_ADMINHTML] as $area) {
103+
$path = $area . DIRECTORY_SEPARATOR . self::FILENAME;
104+
if ($staticDir->isFile($path)) {
105+
$staticDir->delete($path);
106+
}
107+
}
108+
return true;
109+
}
110+
}

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
namespace Magento\Csp\Model;
99

1010
use Magento\Framework\App\CacheInterface;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Exception\FileSystemException;
1113
use Magento\Framework\Serialize\SerializerInterface;
14+
use Magento\Csp\Model\SubresourceIntegrity\Storage\File;
1215

1316
/**
1417
* Class contains methods equivalent to repository design to manage SRI hashes in cache.
@@ -47,22 +50,27 @@ class SubresourceIntegrityRepository
4750
*/
4851
private SubresourceIntegrityFactory $integrityFactory;
4952

53+
private File $sriStorage;
54+
5055
/**
5156
* @param CacheInterface $cache
5257
* @param SerializerInterface $serializer
5358
* @param SubresourceIntegrityFactory $integrityFactory
5459
* @param string|null $context
60+
* @param File|null $sriStorage
5561
*/
5662
public function __construct(
5763
CacheInterface $cache,
5864
SerializerInterface $serializer,
5965
SubresourceIntegrityFactory $integrityFactory,
60-
?string $context = null
66+
?string $context = null,
67+
? File $sriStorage = null
6168
) {
6269
$this->cache = $cache;
6370
$this->serializer = $serializer;
6471
$this->integrityFactory = $integrityFactory;
6572
$this->context = $context;
73+
$this->sriStorage = $sriStorage ?? ObjectManager::getInstance()->get(File::class);
6674
}
6775

6876
/**
@@ -94,6 +102,7 @@ public function getByPath(string $path): ?SubresourceIntegrity
94102
* Gets all available Integrity objects.
95103
*
96104
* @return SubresourceIntegrity[]
105+
* @throws FileSystemException
97106
*/
98107
public function getAll(): array
99108
{
@@ -119,6 +128,7 @@ public function getAll(): array
119128
* @param SubresourceIntegrity $integrity
120129
*
121130
* @return bool
131+
* @throws FileSystemException
122132
*/
123133
public function save(SubresourceIntegrity $integrity): bool
124134
{
@@ -128,10 +138,9 @@ public function save(SubresourceIntegrity $integrity): bool
128138

129139
$this->data = $data;
130140

131-
return $this->cache->save(
141+
return $this->sriStorage->save(
132142
$this->serializer->serialize($this->data),
133-
$this->getCacheKey(),
134-
[self::CACHE_PREFIX]
143+
$this->context
135144
);
136145
}
137146

@@ -152,36 +161,35 @@ public function saveBunch(array $bunch): bool
152161

153162
$this->data = $data;
154163

155-
return $this->cache->save(
164+
return $this->sriStorage->save(
156165
$this->serializer->serialize($this->data),
157-
$this->getCacheKey(),
158-
[self::CACHE_PREFIX]
166+
$this->context
159167
);
160168
}
161169

162170
/**
163171
* Deletes all Integrity objects.
164172
*
165173
* @return bool
174+
* @throws FileSystemException
166175
*/
167176
public function deleteAll(): bool
168177
{
169178
$this->data = null;
170179

171-
return $this->cache->remove(
172-
$this->getCacheKey()
173-
);
180+
return $this->sriStorage->remove();
174181
}
175182

176183
/**
177184
* Loads integrity data from a storage.
178185
*
179186
* @return array
187+
* @throws FileSystemException
180188
*/
181189
private function getData(): array
182190
{
183191
if ($this->data === null) {
184-
$cache = $this->cache->load($this->getCacheKey());
192+
$cache = $this->sriStorage->load($this->context);
185193

186194
$this->data = $cache ? $this->serializer->unserialize($cache) : [];
187195
}
@@ -193,6 +201,7 @@ private function getData(): array
193201
* Gets a cache key based on current context.
194202
*
195203
* @return string
204+
* @deprecated Filesystem storage used instead of a cache
196205
*/
197206
private function getCacheKey(): string
198207
{

0 commit comments

Comments
 (0)