Skip to content

Commit da1d0cf

Browse files
AC-13833: Refactor SRI mechanism to use filesystem for storage.
1 parent 7f6758c commit da1d0cf

File tree

3 files changed

+90
-39
lines changed

3 files changed

+90
-39
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class SubresourceIntegrityRepository
4343
* @param SubresourceIntegrityFactory $integrityFactory
4444
* @param string|null $context
4545
* @param StorageInterface|null $storage
46+
*
47+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4648
*/
4749
public function __construct(
4850
CacheInterface $cache,
@@ -152,13 +154,12 @@ private function getData(): array
152154
{
153155
if ($this->data === null) {
154156
$rawData = $this->storage->load($this->context);
155-
$decodedData = $rawData ? $this->serializer->unserialize($rawData) : [];
156157

157-
$this->data = array_map(
158-
fn($hash, $path) => new SubresourceIntegrity(["path" => $path, "hash" => $hash]),
159-
$decodedData,
160-
array_keys($decodedData)
161-
);
158+
$this->data = $rawData ? $this->serializer->unserialize($rawData) : [];
159+
160+
foreach ($this->data as $path => $hash) {
161+
$this->data[$path] = new SubresourceIntegrity(["path" => $path, "hash" => $hash]);
162+
}
162163
}
163164

164165
return $this->data;

app/code/Magento/Csp/Test/Unit/Model/SubresourceIntegrityRepositoryTest.php

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
use Magento\Csp\Model\SubresourceIntegrity;
1515
use Magento\Csp\Model\SubresourceIntegrityRepository;
1616
use Magento\Csp\Model\SubresourceIntegrityFactory;
17+
use Magento\Csp\Model\SubresourceIntegrity\StorageInterface;
1718

1819
/**
1920
* Unit Test for Class @see Magento\Csp\Model\SubresourceIntegrityRepository
2021
*
2122
*/
2223
class SubresourceIntegrityRepositoryTest extends TestCase
2324
{
25+
/**
26+
* @var string
27+
*/
28+
private string $context = "test";
2429

2530
/**
2631
* @var MockObject
@@ -32,6 +37,11 @@ class SubresourceIntegrityRepositoryTest extends TestCase
3237
*/
3338
private MockObject $serializerMock;
3439

40+
/**
41+
* @var MockObject
42+
*/
43+
private MockObject $storage;
44+
3545
/**
3646
* @var MockObject
3747
*/
@@ -61,16 +71,21 @@ protected function setUp(): void
6171
$this->integrityFactoryMock = $this->getMockBuilder(SubresourceIntegrityFactory::class)
6272
->disableOriginalConstructor()
6373
->getMock();
74+
$this->storage = $this->getMockBuilder(StorageInterface::class)
75+
->disableOriginalConstructor()
76+
->getMockForAbstractClass();
6477

6578
$this->subresourceIntegrityRepository = new SubresourceIntegrityRepository(
6679
$this->cacheMock,
6780
$this->serializerMock,
68-
$this->integrityFactoryMock
81+
$this->integrityFactoryMock,
82+
$this->context,
83+
$this->storage
6984
);
7085
}
7186

72-
/** Test save repository
73-
*
87+
/**
88+
* Test save repository
7489
*
7590
* @return void
7691
*/
@@ -85,45 +100,80 @@ public function testSave(): void
85100

86101
$expected[$data->getPath()] = $data->getHash();
87102
$serialized = json_encode($expected);
88-
$this->cacheMock->expects($this->once())->method('load')->willReturn(false);
89-
$this->serializerMock->expects($this->once())->method('serialize')->with($expected)->willReturn($serialized);
90-
$this->cacheMock->expects($this->once())->method('save')->willReturn(true);
91-
$this->assertTrue($this->subresourceIntegrityRepository->save($data));
103+
104+
$this->storage->expects($this->once())
105+
->method('load')
106+
->with($this->context)
107+
->willReturn(null);
108+
109+
$this->serializerMock->expects($this->never())
110+
->method('unserialize');
111+
112+
$this->serializerMock->expects($this->once())
113+
->method('serialize')
114+
->with($expected)
115+
->willReturn($serialized);
116+
117+
$this->storage->expects($this->once())
118+
->method('save')
119+
->with($serialized, $this->context)
120+
->willReturn(true);
121+
122+
$this->assertTrue(
123+
$this->subresourceIntegrityRepository->save($data)
124+
);
92125
}
93126

94-
/** Test that cache saves in bunch
95-
*
127+
/**
128+
* Test that cache saves in bunch
96129
*
97130
* @return void
98131
*/
99132
public function testSaveBunch(): void
100133
{
101-
$bunch1 = new SubresourceIntegrity(
102-
[
103-
'hash' => 'testhash',
104-
'path' => 'js/jquery.js'
105-
]
106-
);
107-
108-
$bunch2 = new SubresourceIntegrity(
109-
[
110-
'hash' => 'testhash2',
111-
'path' => 'js/test.js'
112-
]
113-
);
114-
115-
$bunches = [$bunch1, $bunch2];
134+
$bunch = [
135+
new SubresourceIntegrity(
136+
[
137+
'hash' => 'testhash',
138+
'path' => 'js/jquery.js'
139+
]
140+
),
141+
new SubresourceIntegrity(
142+
[
143+
'hash' => 'testhash2',
144+
'path' => 'js/test.js'
145+
]
146+
)
147+
];
116148

117149
$expected = [];
118150

119-
foreach ($bunches as $bunch) {
120-
$expected[$bunch->getPath()] = $bunch->getHash();
151+
foreach ($bunch as $integrity) {
152+
$expected[$integrity->getPath()] = $integrity->getHash();
121153
}
154+
122155
$serializedBunch = json_encode($expected);
123-
$this->cacheMock->expects($this->once())->method('load')->willReturn(false);
124-
$this->serializerMock->expects($this->once())->method('serialize')
125-
->with($expected)->willReturn($serializedBunch);
126-
$this->cacheMock->expects($this->once())->method('save')->willReturn(true);
127-
$this->assertTrue($this->subresourceIntegrityRepository->saveBunch($bunches));
156+
157+
$this->storage->expects($this->once())
158+
->method('load')
159+
->with($this->context)
160+
->willReturn(null);
161+
162+
$this->serializerMock->expects($this->never())
163+
->method('unserialize');
164+
165+
$this->serializerMock->expects($this->once())
166+
->method('serialize')
167+
->with($expected)
168+
->willReturn($serializedBunch);
169+
170+
$this->storage->expects($this->once())
171+
->method('save')
172+
->with($serializedBunch, $this->context)
173+
->willReturn(true);
174+
175+
$this->assertTrue(
176+
$this->subresourceIntegrityRepository->saveBunch($bunch)
177+
);
128178
}
129179
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2019 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

0 commit comments

Comments
 (0)