Skip to content

Commit 6a265fd

Browse files
Merge branch 'AC-13547-2.4-develop' into AC-13833-AC-13547
# Conflicts: # app/code/Magento/Csp/etc/di.xml
2 parents c7afb1f + d2c695b commit 6a265fd

File tree

5 files changed

+139
-7
lines changed

5 files changed

+139
-7
lines changed
Lines changed: 42 additions & 0 deletions
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+
}

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe.
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Csp\Plugin;
99

10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\App\State;
1112
use Magento\Deploy\Package\Package;
13+
use Magento\Framework\Exception\LocalizedException;
1214
use Magento\Framework\View\Asset\AssetInterface;
1315
use Magento\Framework\View\Asset\LocalInterface;
1416
use Magento\Framework\View\Asset\GroupedCollection;
1517
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
18+
use Magento\Framework\App\Request\Http;
19+
use Magento\Csp\Model\SubresourceIntegrity\SriEnabledActions;
1620

1721
/**
1822
* Plugin to add integrity to assets on page load.
@@ -29,16 +33,32 @@ class AddDefaultPropertiesToGroupPlugin
2933
*/
3034
private SubresourceIntegrityRepositoryPool $integrityRepositoryPool;
3135

36+
/**
37+
* @var Http
38+
*/
39+
private Http $request;
40+
41+
/**
42+
* @var SriEnabledActions
43+
*/
44+
private SriEnabledActions $action;
45+
3246
/**
3347
* @param State $state
3448
* @param SubresourceIntegrityRepositoryPool $integrityRepositoryPool
49+
* @param Http|null $request
50+
* @param SriEnabledActions|null $action
3551
*/
3652
public function __construct(
3753
State $state,
38-
SubresourceIntegrityRepositoryPool $integrityRepositoryPool
54+
SubresourceIntegrityRepositoryPool $integrityRepositoryPool,
55+
?Http $request = null,
56+
?SriEnabledActions $action = null
3957
) {
4058
$this->state = $state;
4159
$this->integrityRepositoryPool = $integrityRepositoryPool;
60+
$this->request = $request ?? ObjectManager::getInstance()->get(Http::class);
61+
$this->action = $action ?? ObjectManager::getInstance()->get(SriEnabledActions::class);
4262
}
4363

4464
/**
@@ -49,13 +69,14 @@ public function __construct(
4969
* @param array $properties
5070
* @return array
5171
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
72+
* @throws LocalizedException
5273
*/
5374
public function beforeGetFilteredProperties(
5475
GroupedCollection $subject,
5576
AssetInterface $asset,
5677
array $properties = []
5778
): array {
58-
if ($asset instanceof LocalInterface) {
79+
if ($this->canExecute($asset)) {
5980
$integrityRepository = $this->integrityRepositoryPool->get(
6081
Package::BASE_AREA
6182
);
@@ -78,4 +99,18 @@ public function beforeGetFilteredProperties(
7899

79100
return [$asset, $properties];
80101
}
102+
103+
/**
104+
* Check if beforeGetFilteredProperties plugin should execute
105+
*
106+
* @param AssetInterface $asset
107+
* @return bool
108+
*/
109+
private function canExecute(AssetInterface $asset): bool
110+
{
111+
return $asset instanceof LocalInterface &&
112+
$this->action->isPaymentPageAction(
113+
$this->request->getFullActionName()
114+
);
115+
}
81116
}

app/code/Magento/Csp/Test/Unit/Plugin/AddDefaultPropertiesToGroupPluginTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe.
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -10,12 +10,15 @@
1010
use Magento\Csp\Model\SubresourceIntegrity;
1111
use Magento\Csp\Model\SubresourceIntegrityRepository;
1212
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
13+
use Magento\Framework\Exception\LocalizedException;
1314
use PHPUnit\Framework\MockObject\MockObject;
1415
use PHPUnit\Framework\TestCase;
1516
use Magento\Csp\Plugin\AddDefaultPropertiesToGroupPlugin;
1617
use Magento\Framework\View\Asset\File;
1718
use Magento\Framework\View\Asset\GroupedCollection;
1819
use Magento\Framework\App\State;
20+
use Magento\Framework\App\Request\Http;
21+
use Magento\Csp\Model\SubresourceIntegrity\SriEnabledActions;
1922

2023
/**
2124
* Test for class Magento\Csp\Plugin\AddDefaultPropertiesToGroupPlugin
@@ -39,6 +42,16 @@ class AddDefaultPropertiesToGroupPluginTest extends TestCase
3942
*/
4043
private MockObject $stateMock;
4144

45+
/**
46+
* @var MockObject
47+
*/
48+
private MockObject $httpMock;
49+
50+
/**
51+
* @var MockObject
52+
*/
53+
private MockObject $sriEnabledActionsMock;
54+
4255
/**
4356
* @var AddDefaultPropertiesToGroupPlugin
4457
*/
@@ -64,19 +77,33 @@ protected function setUp(): void
6477
->disableOriginalConstructor()
6578
->onlyMethods(['getAreaCode'])
6679
->getMock();
80+
$this->httpMock = $this->getMockBuilder(Http::class)
81+
->disableOriginalConstructor()
82+
->onlyMethods(['getFullActionName'])
83+
->getMock();
84+
$this->sriEnabledActionsMock = $this->getMockBuilder(SriEnabledActions::class)
85+
->disableOriginalConstructor()
86+
->onlyMethods(['isPaymentPageAction'])
87+
->getMock();
6788
$this->plugin = new AddDefaultPropertiesToGroupPlugin(
6889
$this->stateMock,
69-
$this->integrityRepositoryPoolMock
90+
$this->integrityRepositoryPoolMock,
91+
$this->httpMock,
92+
$this->sriEnabledActionsMock
7093
);
7194
}
7295

7396
/**
7497
* Test for plugin with Js assets
7598
*
7699
* @return void
100+
* @throws LocalizedException
77101
*/
78102
public function testBeforeGetFilteredProperties(): void
79103
{
104+
$actionName = "sales_order_create_index";
105+
$this->sriEnabledActionsMock->expects($this->once())->method('isPaymentPageAction')->willReturn(true);
106+
$this->httpMock->expects($this->once())->method('getFullActionName')->willReturn($actionName);
80107
$integrityRepositoryMock = $this->getMockBuilder(SubresourceIntegrityRepository::class)
81108
->disableOriginalConstructor()
82109
->onlyMethods(['getByPath'])

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,16 @@
134134
<argument name="driver" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
135135
</arguments>
136136
</type>
137+
<type name="Magento\Csp\Model\SubresourceIntegrity\SriEnabledActions">
138+
<arguments>
139+
<argument name="paymentActions" xsi:type="array">
140+
<item name="sales_order_create_index" xsi:type="string">sales_order_create_index</item>
141+
<item name="sales_order_create_load_block" xsi:type="string">sales_order_create_loadBlock</item>
142+
<item name="checkout_index_index" xsi:type="string">checkout_index_index</item>
143+
<item name="checkout_onepage_success" xsi:type="string">checkout_onepage_success</item>
144+
<item name="multishipping_checkout_billing" xsi:type="string">multishipping_checkout_billing</item>
145+
</argument>
146+
</arguments>
147+
</type>
137148
<preference for="Magento\Csp\Model\SubresourceIntegrity\StorageInterface" type="Magento\Csp\Model\SubresourceIntegrity\Storage\File" />
138149
</config>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<head>
10+
<link src="Magento_Csp::js/sri.js"/>
11+
</head>
12+
<body>
13+
<referenceBlock name="head.additional">
14+
<block class="Magento\Csp\Block\Sri\Hashes" name="csp.sri.hashes" template="Magento_Csp::sri/hashes.phtml"/>
15+
</referenceBlock>
16+
</body>
17+
</page>

0 commit comments

Comments
 (0)