Skip to content

Commit f2cc975

Browse files
authored
Merge branch '2.4-develop' into graphql-api-enhancements
2 parents 2ba7581 + 38501f0 commit f2cc975

File tree

22 files changed

+838
-180
lines changed

22 files changed

+838
-180
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe.
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Csp\Model\SubresourceIntegrity;
9+
10+
/**
11+
* Defines which payment page actions can add SRI attributes
12+
*/
13+
class SriEnabledActions
14+
{
15+
/**
16+
* @var array $paymentActions
17+
*/
18+
private array $paymentActions;
19+
20+
/**
21+
* @param array $paymentActions
22+
*/
23+
public function __construct(
24+
array $paymentActions = []
25+
) {
26+
$this->paymentActions = $paymentActions;
27+
}
28+
29+
/**
30+
* Check if action is for payment page on storefront or admin
31+
*
32+
* @param string $actionName
33+
* @return bool
34+
*/
35+
public function isPaymentPageAction(string $actionName): bool
36+
{
37+
return in_array(
38+
$actionName,
39+
$this->paymentActions
40+
);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Csp\Model\SubresourceIntegrity\Storage;
9+
10+
use Psr\Log\LoggerInterface;
11+
use Magento\Framework\Filesystem;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Exception\FileSystemException;
14+
use Magento\Csp\Model\SubresourceIntegrity\StorageInterface;
15+
16+
/**
17+
* Filesystem based SRI hashed storage.
18+
*/
19+
class File implements StorageInterface
20+
{
21+
/**
22+
* Name of a storage file.
23+
*
24+
* @var string
25+
*/
26+
private const FILENAME = 'sri-hashes.json';
27+
28+
/**
29+
* @var Filesystem
30+
*/
31+
private Filesystem $filesystem;
32+
33+
/**
34+
* @var LoggerInterface
35+
*/
36+
private LoggerInterface $logger;
37+
38+
/**
39+
* @param Filesystem $filesystem
40+
* @param LoggerInterface $logger
41+
*/
42+
public function __construct(
43+
Filesystem $filesystem,
44+
LoggerInterface $logger
45+
) {
46+
$this->filesystem = $filesystem;
47+
$this->logger = $logger;
48+
}
49+
50+
/**
51+
* @inheritDoc
52+
*/
53+
public function load(?string $context): ?string
54+
{
55+
try {
56+
$staticDir = $this->filesystem->getDirectoryRead(
57+
DirectoryList::STATIC_VIEW
58+
);
59+
60+
$path = $this->resolveFilePath($context);
61+
62+
if (!$staticDir->isFile($path)) {
63+
return null;
64+
}
65+
66+
return $staticDir->readFile($path);
67+
} catch (FileSystemException $exception) {
68+
$this->logger->critical($exception);
69+
70+
return null;
71+
}
72+
}
73+
74+
/**
75+
* @inheritDoc
76+
*/
77+
public function save(string $data, ?string $context): bool
78+
{
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);
91+
92+
return false;
93+
}
94+
}
95+
96+
/**
97+
* @inheritDoc
98+
*/
99+
public function remove(?string $context): bool
100+
{
101+
try {
102+
$staticDir = $this->filesystem->getDirectoryWrite(
103+
DirectoryList::STATIC_VIEW
104+
);
105+
106+
return $staticDir->delete($this->resolveFilePath($context));
107+
} catch (FileSystemException $exception) {
108+
$this->logger->critical($exception);
109+
110+
return false;
111+
}
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;
124+
}
125+
}
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)