Skip to content

Commit 0b79d5c

Browse files
AC-13833: Refactor SRI mechanism to use filesystem for storage.
1 parent 3cfcaca commit 0b79d5c

File tree

4 files changed

+154
-111
lines changed

4 files changed

+154
-111
lines changed
Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
<?php
2-
/************************************************************************
3-
*
4-
* Copyright 2024 Adobe
2+
/**
3+
* Copyright 2025 Adobe
54
* 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-
* ************************************************************************
165
*/
176
declare(strict_types=1);
187

198
namespace Magento\Csp\Model\SubresourceIntegrity\Storage;
209

21-
use Magento\Deploy\Package\Package;
22-
use Magento\Framework\App\Area;
10+
use Psr\Log\LoggerInterface;
11+
use Magento\Framework\Filesystem;
2312
use Magento\Framework\App\Filesystem\DirectoryList;
2413
use Magento\Framework\Exception\FileSystemException;
25-
use Magento\Framework\Filesystem;
14+
use Magento\Csp\Model\SubresourceIntegrity\StorageInterface;
2615

2716
/**
28-
* Persistence of sri hashes in the local file system
17+
* Filesystem based SRI hashed storage.
2918
*/
30-
class File
19+
class File implements StorageInterface
3120
{
3221
/**
33-
* Constant for sri hashes filename
22+
* Name of a storage file.
23+
*
24+
* @var string
3425
*/
3526
private const FILENAME = 'sri-hashes.json';
3627

@@ -40,71 +31,95 @@ class File
4031
private Filesystem $filesystem;
4132

4233
/**
43-
* Constructor
44-
*
34+
* @var LoggerInterface
35+
*/
36+
private LoggerInterface $logger;
37+
38+
/**
4539
* @param Filesystem $filesystem
40+
* @param LoggerInterface $logger
4641
*/
4742
public function __construct(
48-
Filesystem $filesystem
43+
Filesystem $filesystem,
44+
LoggerInterface $logger
4945
) {
5046
$this->filesystem = $filesystem;
47+
$this->logger = $logger;
5148
}
5249

5350
/**
54-
* Load data from filesystem
55-
*
56-
* @param string|null $area
57-
* @return string|bool
58-
* @throws FileSystemException
51+
* @inheritDoc
5952
*/
60-
public function load(?string $area = null): string|bool
53+
public function load(?string $context): ?string
6154
{
62-
$staticDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
55+
try {
56+
$staticDir = $this->filesystem->getDirectoryRead(
57+
DirectoryList::STATIC_VIEW
58+
);
6359

64-
if ($area) {
65-
$path = $area . DIRECTORY_SEPARATOR . self::FILENAME;
66-
if ($staticDir->isFile($path)) {
67-
return $staticDir->readFile($path);
60+
$path = $this->resolveFilePath($context);
61+
62+
if (!$staticDir->isFile($path)) {
63+
return null;
6864
}
65+
66+
return $staticDir->readFile($path);
67+
} catch (FileSystemException $exception) {
68+
$this->logger->critical($exception);
69+
70+
return null;
6971
}
70-
return false;
7172
}
7273

7374
/**
74-
* Save File to Local Storage by area
75-
*
76-
* @param string $data
77-
* @param string|null $area
78-
* @return bool
79-
* @throws FileSystemException
75+
* @inheritDoc
8076
*/
81-
public function save(string $data, ?string $area = null): bool
77+
public function save(string $data, ?string $context): bool
8278
{
83-
$staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
79+
try {
80+
$staticDir = $this->filesystem->getDirectoryWrite(
81+
DirectoryList::STATIC_VIEW
82+
);
83+
84+
return (bool) $staticDir->writeFile(
85+
$this->resolveFilePath($context),
86+
$data,
87+
'w'
88+
);
89+
} catch (FileSystemException $exception) {
90+
$this->logger->critical($exception);
8491

85-
if ($area) {
86-
$path = $area . DIRECTORY_SEPARATOR . self::FILENAME;
87-
return (bool)$staticDir->writeFile($path, $data, 'w');
92+
return false;
8893
}
89-
return false;
9094
}
9195

9296
/**
93-
* Delete all Sri Hashes files
94-
*
95-
* @throws FileSystemException
97+
* @inheritDoc
9698
*/
97-
public function remove():bool
99+
public function remove(?string $context): bool
98100
{
99-
$staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
101+
try {
102+
$staticDir = $this->filesystem->getDirectoryWrite(
103+
DirectoryList::STATIC_VIEW
104+
);
100105

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-
}
106+
return $staticDir->delete($this->resolveFilePath($context));
107+
} catch (FileSystemException $exception) {
108+
$this->logger->critical($exception);
109+
110+
return false;
107111
}
108-
return true;
112+
}
113+
114+
/**
115+
* Resolves a storage file path for a given context.
116+
*
117+
* @param string|null $context
118+
*
119+
* @return string
120+
*/
121+
private function resolveFilePath(?string $context): string
122+
{
123+
return ($context ? $context . DIRECTORY_SEPARATOR : '') . self::FILENAME;
109124
}
110125
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Csp\Model\SubresourceIntegrity;
9+
10+
/**
11+
* Interface for an SRI hashes storage.
12+
*/
13+
interface StorageInterface
14+
{
15+
/**
16+
* Loads SRI hashes from a storage.
17+
*
18+
* @param string|null $context
19+
*
20+
* @return string|null
21+
*/
22+
public function load(?string $context): ?string;
23+
24+
/**
25+
* Saves SRI hashes to a storage.
26+
*
27+
* @param string $data
28+
* @param string|null $context
29+
*
30+
* @return bool
31+
*/
32+
public function save(string $data, ?string $context): bool;
33+
34+
/**
35+
* Deletes all SRI hashes from a storage.
36+
*
37+
* @param string|null $context
38+
*
39+
* @return bool
40+
*/
41+
public function remove(?string $context): bool;
42+
}

0 commit comments

Comments
 (0)