Skip to content

Commit 9f2d5e0

Browse files
MC-39698: Modify Magento Admin CSP for Gainsight enablement For Magento 2.3.x line
1 parent d4289bd commit 9f2d5e0

File tree

8 files changed

+114
-10
lines changed

8 files changed

+114
-10
lines changed

app/code/Magento/AdminAnalytics/etc/csp_whitelist.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
<policy id="script-src">
1212
<values>
1313
<value id="adobedtm" type="host">assets.adobedtm.com</value>
14+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
15+
</values>
16+
</policy>
17+
<policy id="style-src">
18+
<values>
19+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
20+
<value id="fonts_googleapis" type="host">fonts.googleapis.com</value>
21+
</values>
22+
</policy>
23+
<policy id="img-src">
24+
<values>
25+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
26+
<value id="storage_googleapis" type="host">storage.googleapis.com</value>
27+
</values>
28+
</policy>
29+
<policy id="connect-src">
30+
<values>
31+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
32+
</values>
33+
</policy>
34+
<policy id="font-src">
35+
<values>
36+
<value id="fonts_gstatic" type="host">fonts.gstatic.com</value>
1437
</values>
1538
</policy>
1639
</policies>

app/code/Magento/Csp/Model/Collector/CspWhitelistXml/Converter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ public function convert($source)
3636
/** @var \DOMElement $value */
3737
foreach ($policy->getElementsByTagName('value') as $value) {
3838
if ($value->attributes->getNamedItem('type')->nodeValue === 'host') {
39-
$policyConfig[$id]['hosts'][] = $value->nodeValue;
39+
$policyConfig[$id]['hosts'][$value->attributes->getNamedItem('id')->nodeValue] = $value->nodeValue;
4040
} else {
4141
$policyConfig[$id]['hashes'][$value->nodeValue]
4242
= $value->attributes->getNamedItem('algorithm')->nodeValue;
4343
}
4444
}
45-
$policyConfig[$id]['hosts'] = array_unique($policyConfig[$id]['hosts']);
4645
}
4746

4847
return $policyConfig;

app/code/Magento/Csp/Model/Collector/FetchPolicyMerger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function merge(PolicyInterface $policy1, PolicyInterface $policy2): Polic
2525
return new FetchPolicy(
2626
$policy1->getId(),
2727
$policy1->isNoneAllowed() || $policy2->isNoneAllowed(),
28-
array_unique(array_merge($policy1->getHostSources(), $policy2->getHostSources())),
29-
array_unique(array_merge($policy1->getSchemeSources(), $policy2->getSchemeSources())),
28+
array_merge($policy1->getHostSources(), $policy2->getHostSources()),
29+
array_merge($policy1->getSchemeSources(), $policy2->getSchemeSources()),
3030
$policy1->isSelfAllowed() || $policy2->isSelfAllowed(),
3131
$policy1->isInlineAllowed() || $policy2->isInlineAllowed(),
3232
$policy1->isEvalAllowed() || $policy2->isEvalAllowed(),
33-
array_unique(array_merge($policy1->getNonceValues(), $policy2->getNonceValues())),
33+
array_merge($policy1->getNonceValues(), $policy2->getNonceValues()),
3434
array_merge($policy1->getHashes(), $policy2->getHashes()),
3535
$policy1->isDynamicAllowed() || $policy2->isDynamicAllowed(),
3636
$policy1->areEventHandlersAllowed() || $policy2->areEventHandlersAllowed()

app/code/Magento/Csp/Model/Collector/PluginTypesPolicyMerger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function merge(PolicyInterface $policy1, PolicyInterface $policy2): Polic
2222
{
2323
/** @var PluginTypesPolicy $policy1 */
2424
/** @var PluginTypesPolicy $policy2 */
25-
return new PluginTypesPolicy(array_unique(array_merge($policy1->getTypes(), $policy2->getTypes())));
25+
return new PluginTypesPolicy(array_merge($policy1->getTypes(), $policy2->getTypes()));
2626
}
2727

2828
/**

app/code/Magento/Csp/Model/Policy/FetchPolicy.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ public function __construct(
116116
) {
117117
$this->id = $id;
118118
$this->noneAllowed = $noneAllowed;
119-
$this->hostSources = array_unique($hostSources);
120-
$this->schemeSources = array_unique($schemeSources);
119+
$this->hostSources = array_values(array_unique($hostSources));
120+
$this->schemeSources = array_values(array_unique($schemeSources));
121121
$this->selfAllowed = $selfAllowed;
122122
$this->inlineAllowed = $inlineAllowed;
123123
$this->evalAllowed = $evalAllowed;
124-
$this->nonceValues = array_unique($nonceValues);
124+
$this->nonceValues = array_values(array_unique($nonceValues));
125125
$this->hashes = $hashValues;
126126
$this->dynamicAllowed = $dynamicAllowed;
127127
$this->eventHandlersAllowed = $eventHandlersAllowed;

app/code/Magento/Csp/Model/Policy/PluginTypesPolicy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(array $types)
2525
if (!$types) {
2626
throw new \RuntimeException('PluginTypePolicy must be given at least 1 type.');
2727
}
28-
$this->types = array_unique($types);
28+
$this->types = array_values(array_unique($types));
2929
}
3030

3131
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp/etc/csp_whitelist.xsd">
9+
<policies>
10+
<policy id="object-src">
11+
<values>
12+
<value id="example-base" type="host">example.magento.com</value>
13+
<value id="mage-base" type="host">https://admin.magento.com</value>
14+
</values>
15+
</policy>
16+
<policy id="media-src">
17+
<values>
18+
<value id="example-base" type="host">example.magento.com</value>
19+
<value id="mage-base" type="host">https://admin.magento.com</value>
20+
</values>
21+
</policy>
22+
</policies>
23+
</csp_whitelist>

dev/tests/integration/testsuite/Magento/Csp/Model/Collector/CspWhitelistXmlCollectorTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,63 @@ public function testCollecting(): void
7272
$this->assertTrue($objectSrcChecked);
7373
$this->assertTrue($mediaSrcChecked);
7474
}
75+
76+
/**
77+
* Test collecting configurations from multiple XML files for adminhtml area.
78+
*
79+
* @magentoAppArea adminhtml
80+
* @return void
81+
*/
82+
public function testCollectingForAdminhtmlArea(): void
83+
{
84+
$policies = $this->collector->collect([]);
85+
86+
$mediaSrcChecked = false;
87+
$objectSrcChecked = false;
88+
$this->assertNotEmpty($policies);
89+
/** @var FetchPolicy $policy */
90+
foreach ($policies as $policy) {
91+
$this->assertFalse($policy->isNoneAllowed());
92+
$this->assertFalse($policy->isSelfAllowed());
93+
$this->assertFalse($policy->isInlineAllowed());
94+
$this->assertFalse($policy->isEvalAllowed());
95+
$this->assertFalse($policy->isDynamicAllowed());
96+
$this->assertEmpty($policy->getSchemeSources());
97+
$this->assertEmpty($policy->getNonceValues());
98+
if ($policy->getId() === 'object-src') {
99+
$this->assertInstanceOf(FetchPolicy::class, $policy);
100+
$this->assertEquals(
101+
[
102+
'https://admin.magento.com',
103+
'https://devdocs.magento.com',
104+
'example.magento.com'
105+
],
106+
$policy->getHostSources()
107+
);
108+
$this->assertEquals(
109+
[
110+
'B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256',
111+
'B3yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256',
112+
'B4yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256'
113+
],
114+
$policy->getHashes()
115+
);
116+
$objectSrcChecked = true;
117+
} elseif ($policy->getId() === 'media-src') {
118+
$this->assertInstanceOf(FetchPolicy::class, $policy);
119+
$this->assertEquals(
120+
[
121+
'https://admin.magento.com',
122+
'https://devdocs.magento.com',
123+
'example.magento.com'
124+
],
125+
$policy->getHostSources()
126+
);
127+
$this->assertEmpty($policy->getHashes());
128+
$mediaSrcChecked = true;
129+
}
130+
}
131+
$this->assertTrue($objectSrcChecked);
132+
$this->assertTrue($mediaSrcChecked);
133+
}
75134
}

0 commit comments

Comments
 (0)