This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,213 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Csp\Model\Collector;
use Magento\Csp\Api\Data\PolicyInterface;
use Magento\Csp\Model\Policy\FetchPolicy;
use Magento\Csp\Model\Policy\FlagPolicy;
use Magento\Csp\Model\Policy\PluginTypesPolicy;
use Magento\Csp\Model\Policy\SandboxPolicy;
use Magento\Framework\App\RequestInterface;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;
/**
* Test collecting policies from Magento config.
*/
class ConfigCollectorTest extends TestCase
{
/**
* @var ConfigCollector
*/
private $collector;
/**
* @var RequestInterface
*/
private $request;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->collector = Bootstrap::getObjectManager()->get(ConfigCollector::class);
$this->request = Bootstrap::getObjectManager()->get(RequestInterface::class);
}
/**
* Create expected policy objects.
*
* @return PolicyInterface[]
*/
private function getExpectedPolicies(): array
{
return [
'child-src' => new FetchPolicy(
'child-src',
false,
['http://magento.com', 'http://devdocs.magento.com'],
['http', 'https', 'blob'],
true,
true,
false,
[],
[],
true
),
'child-src2' => new FetchPolicy('child-src', false, [], [], false, false, true),
'connect-src' => new FetchPolicy('connect-src'),
'default-src' => new FetchPolicy(
'default-src',
false,
['http://magento.com', 'http://devdocs.magento.com'],
[],
true
),
'font-src' => new FetchPolicy('font-src', false, [], ['data'], true),
'frame-src' => new FetchPolicy('frame-src', false, [], [], true, false, false, [], [], true),
'img-src' => new FetchPolicy('img-src', false, [], ['data'], true),
'manifest-src' => new FetchPolicy('manifest-src', false, [], [], true),
'media-src' => new FetchPolicy('media-src', false, [], [], true),
'object-src' => new FetchPolicy('object-src', false, [], [], true),
'script-src' => new FetchPolicy('script-src', false, [], [], true, true, true, [], [], false, true),
'style-src' => new FetchPolicy('style-src', false, [], [], true),
'base-uri' => new FetchPolicy('base-uri', false, [], [], true),
'plugin-types' => new PluginTypesPolicy(
['application/x-shockwave-flash', 'application/x-java-applet']
),
'sandbox' => new SandboxPolicy(true, true, true, true, false, false, true, true, true, true, true),
'form-action' => new FetchPolicy('form-action', false, [], [], true),
'frame-ancestors' => new FetchPolicy('frame-ancestors', false, [], [], true),
'block-all-mixed-content' => new FlagPolicy('block-all-mixed-content'),
'upgrade-insecure-requests' => new FlagPolicy('upgrade-insecure-requests')
];
}
/**
* Test initiating policies from config.
*
* @magentoAppArea frontend
* @magentoConfigFixture default_store csp/policies/storefront/default/policy_id default-src
* @magentoConfigFixture default_store csp/policies/storefront/default/none 0
* @magentoConfigFixture default_store csp/policies/storefront/default/hosts/example http://magento.com
* @magentoConfigFixture default_store csp/policies/storefront/default/hosts/example2 http://devdocs.magento.com
* @magentoConfigFixture default_store csp/policies/storefront/default/self 1
* @magentoConfigFixture default_store csp/policies/storefront/default/eval 0
* @magentoConfigFixture default_store csp/policies/storefront/default/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/children/policy_id child-src
* @magentoConfigFixture default_store csp/policies/storefront/children/none 0
* @magentoConfigFixture default_store csp/policies/storefront/children/hosts/example http://magento.com
* @magentoConfigFixture default_store csp/policies/storefront/children/hosts/example2 http://devdocs.magento.com
* @magentoConfigFixture default_store csp/policies/storefront/children/self 1
* @magentoConfigFixture default_store csp/policies/storefront/children/inline 1
* @magentoConfigFixture default_store csp/policies/storefront/children/schemes/scheme1 http
* @magentoConfigFixture default_store csp/policies/storefront/children/dynamic 1
* @magentoConfigFixture default_store csp/policies/storefront/children-2/policy_id child-src
* @magentoConfigFixture default_store csp/policies/storefront/children-2/none 0
* @magentoConfigFixture default_store csp/policies/storefront/children-2/eval 1
* @magentoConfigFixture default_store csp/policies/storefront/connections/policy_id connect-src
* @magentoConfigFixture default_store csp/policies/storefront/connections/none 1
* @magentoConfigFixture default_store csp/policies/storefront/connections/self 0
* @magentoConfigFixture default_store csp/policies/storefront/connections/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/fonts/policy_id font-src
* @magentoConfigFixture default_store csp/policies/storefront/fonts/none 0
* @magentoConfigFixture default_store csp/policies/storefront/fonts/self 1
* @magentoConfigFixture default_store csp/policies/storefront/fonts/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/frames/policy_id frame-src
* @magentoConfigFixture default_store csp/policies/storefront/frames/none 0
* @magentoConfigFixture default_store csp/policies/storefront/frames/self 1
* @magentoConfigFixture default_store csp/policies/storefront/frames/dynamic 1
* @magentoConfigFixture default_store csp/policies/storefront/frames/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/images/policy_id img-src
* @magentoConfigFixture default_store csp/policies/storefront/images/none 0
* @magentoConfigFixture default_store csp/policies/storefront/images/self 1
* @magentoConfigFixture default_store csp/policies/storefront/images/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/manifests/policy_id manifest-src
* @magentoConfigFixture default_store csp/policies/storefront/manifests/none 0
* @magentoConfigFixture default_store csp/policies/storefront/manifests/self 1
* @magentoConfigFixture default_store csp/policies/storefront/manifests/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/media/policy_id media-src
* @magentoConfigFixture default_store csp/policies/storefront/media/none 0
* @magentoConfigFixture default_store csp/policies/storefront/media/self 1
* @magentoConfigFixture default_store csp/policies/storefront/media/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/objects/policy_id object-src
* @magentoConfigFixture default_store csp/policies/storefront/objects/none 0
* @magentoConfigFixture default_store csp/policies/storefront/objects/self 1
* @magentoConfigFixture default_store csp/policies/storefront/objects/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/scripts/policy_id script-src
* @magentoConfigFixture default_store csp/policies/storefront/scripts/none 0
* @magentoConfigFixture default_store csp/policies/storefront/scripts/self 1
* @magentoConfigFixture default_store csp/policies/storefront/scripts/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/scripts/eval 0
* @magentoConfigFixture default_store csp/policies/storefront/scripts/event_handlers 1
* @magentoConfigFixture default_store csp/policies/storefront/base_uri/policy_id base-uri
* @magentoConfigFixture default_store csp/policies/storefront/base_uri/none 0
* @magentoConfigFixture default_store csp/policies/storefront/base_uri/self 1
* @magentoConfigFixture default_store csp/policies/storefront/styles/policy_id style-src
* @magentoConfigFixture default_store csp/policies/storefront/styles/none 0
* @magentoConfigFixture default_store csp/policies/storefront/styles/self 1
* @magentoConfigFixture default_store csp/policies/storefront/styles/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/forms/policy_id form-action
* @magentoConfigFixture default_store csp/policies/storefront/forms/none 0
* @magentoConfigFixture default_store csp/policies/storefront/forms/self 1
* @magentoConfigFixture default_store csp/policies/storefront/forms/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/frame-ancestors/policy_id frame-ancestors
* @magentoConfigFixture default_store csp/policies/storefront/frame-ancestors/none 0
* @magentoConfigFixture default_store csp/policies/storefront/frame-ancestors/self 1
* @magentoConfigFixture default_store csp/policies/storefront/frame-ancestors/inline 0
* @magentoConfigFixture default_store csp/policies/storefront/plugin_types/policy_id plugin-types
* @magentoConfigFixture default_store csp/policies/storefront/plugin_types/types/fl application/x-shockwave-flash
* @magentoConfigFixture default_store csp/policies/storefront/plugin_types/types/applet application/x-java-applet
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/policy_id sandbox
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/forms 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/modals 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/orientation 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/pointer 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/popup 0
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/popups_to_escape 0
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/presentation 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/same_origin 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/scripts 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/navigation 1
* @magentoConfigFixture default_store csp/policies/storefront/sandbox/navigation_by_user 1
* @magentoConfigFixture default_store csp/policies/storefront/mixed_content/policy_id block-all-mixed-content
* @magentoConfigFixture default_store csp/policies/storefront/base/policy_id base-uri
* @magentoConfigFixture default_store csp/policies/storefront/base/inline 0
* @magentoConfigFixture default_store csp/policies/storefront_checkout_index_index/scripts/policy_id script-src
* @magentoConfigFixture default_store csp/policies/storefront_checkout_index_index/scripts/self 1
* @magentoConfigFixture default_store csp/policies/storefront_checkout_index_index/scripts/inline 1
* @magentoConfigFixture default_store csp/policies/storefront_checkout_index_index/scripts/eval 1
* @magentoConfigFixture default_store csp/policies/storefront/upgrade/policy_id upgrade-insecure-requests
* @return void
*/
public function testCollecting(): void
{
$this->request->setRouteName('checkout');
$this->request->setControllerName('index');
$this->request->setActionName('index');
$policies = $this->collector->collect([new FlagPolicy('upgrade-insecure-requests')]);
$expectedPolicies = $this->getExpectedPolicies();
$this->assertNotEmpty($policies);
$defaultPolicy = array_shift($policies);
$this->assertEquals('upgrade-insecure-requests', $defaultPolicy->getId());
$expectedPolicyKeys = array_keys($expectedPolicies);
$checkedKeys = [];
foreach ($policies as $policy) {
$id = $policy->getId();
$this->assertTrue(in_array($id, $expectedPolicyKeys));
if ($id === 'child-src' && $policy->isEvalAllowed()) {
$id = 'child-src2';
}
$this->assertEquals($expectedPolicies[$id]->getValue(), $policy->getValue());
$checkedKeys[] = $id;
}
$this->assertEmpty(array_diff($expectedPolicyKeys, $checkedKeys));
}
}
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Csp\Model\Collector;
use Magento\Csp\Api\CspAwareActionInterface;
use Magento\Csp\Model\Policy\FetchPolicy;
use Magento\Csp\Model\Policy\FlagPolicy;
use Magento\Framework\Exception\NotFoundException;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test collecting policies from a CSP-aware controllers.
*/
class ControllerCollectorTest extends TestCase
{
/**
* @var ControllerCollector
*/
private $collector;
/**
* @inheritDoc
*/
public function setUp(): void
{
$this->collector = Bootstrap::getObjectManager()->create(ControllerCollector::class);
}
/**
* Test collection.
*
* @return void
*/
public function testCollect(): void
{
$controller = new class implements CspAwareActionInterface {
/**
* @inheritDoc
*/
public function execute()
{
throw new NotFoundException(__('Page not found.'));
}
/**
* @inheritDoc
*/
public function modifyCsp(array $appliedPolicies): array
{
$processed = [];
foreach ($appliedPolicies as $policy) {
if ($policy instanceof FetchPolicy && $policy->getHostSources()) {
$policy = new FetchPolicy(
'default-src',
false,
array_map(
function ($host) {
return str_replace('http://', 'https://', $host);
},
$policy->getHostSources()
)
);
}
$processed[] = $policy;
}
$processed[] = new FlagPolicy(FlagPolicy::POLICIES[0]);
return $processed;
}
};
$this->collector->setCurrentActionInstance($controller);
$collected = $this->collector->collect([new FetchPolicy('default-src', false, ['http://magento.com'])]);
$this->assertEquals(
[new FetchPolicy('default-src', false, ['https://magento.com']), new FlagPolicy(FlagPolicy::POLICIES[0])],
$collected
);
}
}
@@ -0,0 +1,136 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Csp\Model\Collector;
use Magento\Csp\Model\Policy\FetchPolicy;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test collecting csp_whitelist configurations.
*/
class CspWhitelistXmlCollectorTest extends TestCase
{
/**
* @var CspWhitelistXmlCollector
*/
private $collector;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->collector = Bootstrap::getObjectManager()->get(CspWhitelistXmlCollector::class);
}
/**
* Test collecting configurations from multiple XML files.
*
* @return void
*/
public function testCollecting(): void
{
$policies = $this->collector->collect([]);
$mediaSrcChecked = false;
$objectSrcChecked = false;
$this->assertNotEmpty($policies);
/** @var FetchPolicy $policy */
foreach ($policies as $policy) {
$this->assertFalse($policy->isNoneAllowed());
$this->assertFalse($policy->isSelfAllowed());
$this->assertFalse($policy->isInlineAllowed());
$this->assertFalse($policy->isEvalAllowed());
$this->assertFalse($policy->isDynamicAllowed());
$this->assertEmpty($policy->getSchemeSources());
$this->assertEmpty($policy->getNonceValues());
if ($policy->getId() === 'object-src') {
$this->assertInstanceOf(FetchPolicy::class, $policy);
$this->assertEquals(['http://magento.com', 'https://devdocs.magento.com'], $policy->getHostSources());
$this->assertEquals(
[
'B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256',
'B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF9=' => 'sha256'
],
$policy->getHashes()
);
$objectSrcChecked = true;
} elseif ($policy->getId() === 'media-src') {
$this->assertInstanceOf(FetchPolicy::class, $policy);
$this->assertEquals(
['*.adobe.com', 'https://magento.com', 'https://devdocs.magento.com'],
$policy->getHostSources()
);
$this->assertEmpty($policy->getHashes());
$mediaSrcChecked = true;
}
}
$this->assertTrue($objectSrcChecked);
$this->assertTrue($mediaSrcChecked);
}
/**
* Test collecting configurations from multiple XML files for adminhtml area.
*
* @magentoAppArea adminhtml
* @return void
*/
public function testCollectingForAdminhtmlArea(): void
{
$policies = $this->collector->collect([]);
$mediaSrcChecked = false;
$objectSrcChecked = false;
$this->assertNotEmpty($policies);
/** @var FetchPolicy $policy */
foreach ($policies as $policy) {
$this->assertFalse($policy->isNoneAllowed());
$this->assertFalse($policy->isSelfAllowed());
$this->assertFalse($policy->isInlineAllowed());
$this->assertFalse($policy->isEvalAllowed());
$this->assertFalse($policy->isDynamicAllowed());
$this->assertEmpty($policy->getSchemeSources());
$this->assertEmpty($policy->getNonceValues());
if ($policy->getId() === 'object-src') {
$this->assertInstanceOf(FetchPolicy::class, $policy);
$this->assertEquals(
[
'https://admin.magento.com',
'https://devdocs.magento.com',
'example.magento.com'
],
$policy->getHostSources()
);
$this->assertEquals(
[
'B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256',
'B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF9=' => 'sha256'
],
$policy->getHashes()
);
$objectSrcChecked = true;
} elseif ($policy->getId() === 'media-src') {
$this->assertInstanceOf(FetchPolicy::class, $policy);
$this->assertEquals(
[
'*.adobe.com',
'https://admin.magento.com',
'https://devdocs.magento.com',
'example.magento.com'
],
$policy->getHostSources()
);
$this->assertEmpty($policy->getHashes());
$mediaSrcChecked = true;
}
}
$this->assertTrue($objectSrcChecked);
$this->assertTrue($mediaSrcChecked);
}
}
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Csp\Model\Collector;
use Magento\Csp\Api\Data\PolicyInterface;
/**
* Helps with testing CSP policies.
*/
class DynamicCollectorMock extends DynamicCollector
{
/**
* @var PolicyInterface[]
*/
private $added = [];
/**
* @inheritDoc
*/
public function add(PolicyInterface $policy): void
{
$this->added[] = $policy;
parent::add($policy);
}
/**
* Collect added policies and start a new cycle.
*
* @return PolicyInterface[]
*/
public function consumeAdded(): array
{
$policies = $this->added;
$this->added = [];
return $policies;
}
}
@@ -0,0 +1,177 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Csp\Model;
use Magento\Csp\Api\PolicyCollectorInterface;
use Magento\Csp\Model\Policy\FetchPolicy;
use Magento\Csp\Model\Policy\FlagPolicy;
use Magento\Csp\Model\Policy\PluginTypesPolicy;
use Magento\Csp\Model\Policy\SandboxPolicy;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test that composite collector properly calls other collectors and merges results.
*/
class CompositePolicyCollectorTest extends TestCase
{
/**
* Create mock collectors that will populate policies.
*
* @return PolicyCollectorInterface[]
*/
private function createMockCollectors(): array
{
$mockCollector1 = $this->getMockForAbstractClass(PolicyCollectorInterface::class);
$mockCollector1->method('collect')
->willReturnCallback(
function (array $prevPolicies) {
return array_merge(
$prevPolicies,
[
new FetchPolicy(
'script-src',
false,
['https://magento.com'],
['https'],
true,
false,
true,
['569403695046645'],
['B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256'],
false,
true
),
new FetchPolicy('script-src', false, ['https://devdocs.magento.com']),
new FlagPolicy('upgrade-insecure-requests'),
new PluginTypesPolicy(['application/x-shockwave-flash']),
new SandboxPolicy(false, true, false, true, false, true, false, true, false, true, false)
]
);
}
);
$mockCollector2 = $this->getMockForAbstractClass(PolicyCollectorInterface::class);
$mockCollector2->method('collect')
->willReturnCallback(
function (array $prevPolicies) {
return array_merge(
$prevPolicies,
[
new FetchPolicy(
'script-src',
true,
['http://magento.com'],
['http'],
false,
false,
false,
['5694036950466451'],
['B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF7=' => 'sha256'],
true,
false
),
new FetchPolicy('default-src', false, [], [], true),
new FlagPolicy('upgrade-insecure-requests'),
new PluginTypesPolicy(['application/x-java-applet']),
new SandboxPolicy(true, false, true, false, true, false, true, false, true, false, false)
]
);
}
);
return [$mockCollector1, $mockCollector2];
}
/**
* Test collect method.
*
* Supply fake collectors, check results.
*
* @return void
*/
public function testCollect(): void
{
/** @var CompositePolicyCollector $collector */
$collector = Bootstrap::getObjectManager()->create(
CompositePolicyCollector::class,
['collectors' => $this->createMockCollectors()]
);
$collected = $collector->collect([]);
/** @var FetchPolicy[]|FlagPolicy[]|PluginTypesPolicy[]|SandboxPolicy[] $policies */
$policies = [];
/** @var \Magento\Csp\Api\Data\PolicyInterface $policy */
foreach ($collected as $policy) {
$policies[$policy->getId()] = $policy;
}
//Comparing resulting policies
$this->assertArrayHasKey('script-src', $policies);
$this->assertTrue($policies['script-src']->isNoneAllowed());
$this->assertTrue($policies['script-src']->isSelfAllowed());
$this->assertFalse($policies['script-src']->isInlineAllowed());
$this->assertTrue($policies['script-src']->isEvalAllowed());
$this->assertTrue($policies['script-src']->isDynamicAllowed());
$this->assertTrue($policies['script-src']->areEventHandlersAllowed());
$foundHosts = $policies['script-src']->getHostSources();
$hosts = ['http://magento.com', 'https://magento.com', 'https://devdocs.magento.com'];
sort($foundHosts);
sort($hosts);
$this->assertEquals($hosts, $foundHosts);
$foundSchemes = $policies['script-src']->getSchemeSources();
$schemes = ['https', 'http'];
sort($foundSchemes);
sort($schemes);
$this->assertEquals($schemes, $foundSchemes);
$foundNonceValues = $policies['script-src']->getNonceValues();
$nonceValues = ['5694036950466451', '569403695046645'];
sort($foundNonceValues);
sort($nonceValues);
$this->assertEquals($nonceValues, $foundNonceValues);
$foundHashes = $policies['script-src']->getHashes();
$hashes = [
'B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF7=' => 'sha256',
'B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256'
];
$this->assertEquals($hashes, $foundHashes);
$this->assertArrayHasKey('default-src', $policies);
$this->assertFalse($policies['default-src']->isNoneAllowed());
$this->assertTrue($policies['default-src']->isSelfAllowed());
$this->assertFalse($policies['default-src']->isInlineAllowed());
$this->assertFalse($policies['default-src']->isEvalAllowed());
$this->assertFalse($policies['default-src']->isDynamicAllowed());
$this->assertFalse($policies['default-src']->areEventHandlersAllowed());
$this->assertEmpty($policies['default-src']->getHashes());
$this->assertEmpty($policies['default-src']->getNonceValues());
$this->assertEmpty($policies['default-src']->getHostSources());
$this->assertEmpty($policies['default-src']->getSchemeSources());
$this->assertArrayHasKey('upgrade-insecure-requests', $policies);
$this->assertInstanceOf(FlagPolicy::class, $policies['upgrade-insecure-requests']);
$this->assertArrayHasKey('plugin-types', $policies);
$types = ['application/x-java-applet', 'application/x-shockwave-flash'];
$foundTypes = $policies['plugin-types']->getTypes();
sort($types);
sort($foundTypes);
$this->assertEquals($types, $foundTypes);
$this->assertArrayHasKey('sandbox', $policies);
$this->assertTrue($policies['sandbox']->isFormAllowed());
$this->assertTrue($policies['sandbox']->isModalsAllowed());
$this->assertTrue($policies['sandbox']->isOrientationLockAllowed());
$this->assertTrue($policies['sandbox']->isPointerLockAllowed());
$this->assertTrue($policies['sandbox']->isPopupsAllowed());
$this->assertTrue($policies['sandbox']->isPopupsToEscapeSandboxAllowed());
$this->assertTrue($policies['sandbox']->isScriptsAllowed());
$this->assertFalse($policies['sandbox']->isTopNavigationByUserActivationAllowed());
$this->assertTrue($policies['sandbox']->isTopNavigationAllowed());
$this->assertTrue($policies['sandbox']->isSameOriginAllowed());
$this->assertTrue($policies['sandbox']->isPresentationAllowed());
}
}
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Csp\Model\Mode;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test config manager.
*/
class ConfigManagerTest extends TestCase
{
/**
* @var ConfigManager
*/
private $manager;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->manager = Bootstrap::getObjectManager()->get(ConfigManager::class);
}
/**
* Check the default configurations of CSP.
*
* @magentoAppArea frontend
* @return void
*/
public function testStorefrontDefault(): void
{
$config = $this->manager->getConfigured();
$this->assertTrue($config->isReportOnly());
$this->assertNull($config->getReportUri());
}
/**
* Check the default configurations of CSP.
*
* @magentoAppArea adminhtml
* @return void
*/
public function testAdminDefault(): void
{
$config = $this->manager->getConfigured();
$this->assertTrue($config->isReportOnly());
$this->assertNull($config->getReportUri());
}
/**
* Check that class returns correct configurations.
*
* @magentoAppArea frontend
* @magentoConfigFixture default_store csp/mode/storefront/report_only 0
* @magentoConfigFixture default_store csp/mode/storefront/report_uri https://magento.com
* @return void
*/
public function testFrontendConfigured(): void
{
$config = $this->manager->getConfigured();
$this->assertFalse($config->isReportOnly());
$this->assertEquals('https://magento.com', $config->getReportUri());
}
/**
* Check that class returns correct configurations.
*
* @magentoAppArea adminhtml
* @magentoConfigFixture default_store csp/mode/admin/report_only 0
* @magentoConfigFixture default_store csp/mode/admin/report_uri https://magento.com
* @return void
*/
public function testAdminConfigured(): void
{
$config = $this->manager->getConfigured();
$this->assertFalse($config->isReportOnly());
$this->assertEquals('https://magento.com', $config->getReportUri());
}
}
@@ -0,0 +1,182 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Csp\Model\Policy\Renderer;
use Magento\Csp\Model\Policy\FetchPolicy;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Response\Http as HttpResponse;
/**
* Test that rendering policies via headers works.
*/
class SimplePolicyHeaderRendererTest extends TestCase
{
/**
* @var SimplePolicyHeaderRenderer
*/
private $renderer;
/**
* @var HttpResponse
*/
private $response;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->renderer = Bootstrap::getObjectManager()->get(SimplePolicyHeaderRenderer::class);
$this->response = Bootstrap::getObjectManager()->create(HttpResponse::class);
}
/**
* Test policy rendering in restrict mode.
*
* @magentoAppArea frontend
* @magentoConfigFixture default_store csp/mode/storefront/report_only 0
* @magentoConfigFixture default_store csp/mode/storefront/report_uri 0
*
* @return void
*/
public function testRenderRestrictMode(): void
{
$policy = new FetchPolicy('default-src', false, ['https://magento.com'], [], true);
$this->renderer->render($policy, $this->response);
$this->assertNotEmpty($header = $this->response->getHeader('Content-Security-Policy'));
$this->assertEmpty($this->response->getHeader('Content-Security-Policy-Report-Only'));
$this->assertEquals('default-src https://magento.com \'self\';', $header->getFieldValue());
}
/**
* Test policy rendering in restrict mode with report URL provided.
*
* @magentoAppArea frontend
* @magentoConfigFixture default_store csp/mode/storefront/report_only 0
* @magentoConfigFixture default_store csp/mode/storefront/report_uri /csp-reports/
*
* @return void
*/
public function testRenderRestrictWithReportingMode(): void
{
$policy = new FetchPolicy('default-src', false, ['https://magento.com'], [], true);
$this->renderer->render($policy, $this->response);
$this->assertNotEmpty($header = $this->response->getHeader('Content-Security-Policy'));
$this->assertEmpty($this->response->getHeader('Content-Security-Policy-Report-Only'));
$this->assertEquals(
'default-src https://magento.com \'self\'; report-uri /csp-reports/; report-to report-endpoint;',
$header->getFieldValue()
);
$this->assertNotEmpty($reportToHeader = $this->response->getHeader('Report-To'));
$this->assertNotEmpty($reportData = json_decode("[{$reportToHeader->getFieldValue()}]", true));
$this->assertEquals('report-endpoint', $reportData[0]['group']);
}
/**
* Test policy rendering in report-only mode.
*
* @magentoAppArea frontend
* @magentoConfigFixture default_store csp/mode/storefront/report_only 1
* @magentoConfigFixture default_store csp/mode/storefront/report_uri 0
*
* @return void
*/
public function testRenderReportMode(): void
{
$policy = new FetchPolicy(
'default-src',
false,
['https://magento.com'],
['https'],
true,
false,
true,
['5749837589457695'],
['B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256'],
true,
true
);
$this->renderer->render($policy, $this->response);
$this->assertNotEmpty($header = $this->response->getHeader('Content-Security-Policy-Report-Only'));
$this->assertEmpty($this->response->getHeader('Content-Security-Policy'));
$this->assertEquals(
'default-src https://magento.com https: \'self\' \'unsafe-eval\' \'strict-dynamic\''
. ' \'unsafe-hashes\' \'nonce-'.base64_encode($policy->getNonceValues()[0]).'\''
. ' \'sha256-B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=\';',
$header->getFieldValue()
);
}
/**
* Test rendering a fetch policy with inline allowed.
*
* @magentoAppArea frontend
* @magentoConfigFixture default_store csp/mode/storefront/report_only 1
* @magentoConfigFixture default_store csp/mode/storefront/report_uri 0
*
* @return void
*/
public function testFetchPolicyInlineAllowed(): void
{
$policy = new FetchPolicy(
'script-src',
false,
['https://magento.com'],
['https'],
true,
true,
true,
['5749837589457695'],
['B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=' => 'sha256'],
false,
false
);
$this->renderer->render($policy, $this->response);
$this->assertNotEmpty($header = $this->response->getHeader('Content-Security-Policy-Report-Only'));
$this->assertEmpty($this->response->getHeader('Content-Security-Policy'));
$this->assertEquals(
'script-src https://magento.com https: \'self\' \'unsafe-inline\' \'unsafe-eval\';',
$header->getFieldValue()
);
}
/**
* Test policy rendering in report-only mode with report URL provided.
*
* @magentoAppArea frontend
* @magentoConfigFixture default_store csp/mode/storefront/report_only 1
* @magentoConfigFixture default_store csp/mode/storefront/report_uri /csp-reports/
*
* @return void
*/
public function testRenderReportWithReportingMode(): void
{
$policy = new FetchPolicy('default-src', false, ['https://magento.com'], [], true);
$this->renderer->render($policy, $this->response);
$this->assertNotEmpty($header = $this->response->getHeader('Content-Security-Policy-Report-Only'));
$this->assertEmpty($this->response->getHeader('Content-Security-Policy'));
$this->assertEquals(
'default-src https://magento.com \'self\'; report-uri /csp-reports/; report-to report-endpoint;',
$header->getFieldValue()
);
$this->assertNotEmpty($reportToHeader = $this->response->getHeader('Report-To'));
$this->assertNotEmpty($reportData = json_decode("[{$reportToHeader->getFieldValue()}]", true));
$this->assertEquals('report-endpoint', $reportData[0]['group']);
}
}