ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Csp;
|
||||
|
||||
use Magento\Csp\Model\Collector\DynamicCollector;
|
||||
use Magento\Csp\Model\Collector\DynamicCollectorMock;
|
||||
use Magento\Framework\Math\Random;
|
||||
use Magento\Framework\View\LayoutInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test that inline util works fine with cached blocks.
|
||||
*/
|
||||
class CachedBlockTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var LayoutInterface
|
||||
*/
|
||||
private $layout;
|
||||
|
||||
/**
|
||||
* @var DynamicCollectorMock
|
||||
*/
|
||||
private $dynamicCollected;
|
||||
|
||||
/**
|
||||
* @var Random
|
||||
*/
|
||||
private $random;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
Bootstrap::getObjectManager()->configure([
|
||||
'preferences' => [
|
||||
DynamicCollector::class => DynamicCollectorMock::class
|
||||
]
|
||||
]);
|
||||
$this->layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
|
||||
$this->dynamicCollected = Bootstrap::getObjectManager()->get(DynamicCollector::class);
|
||||
$this->random = Bootstrap::getObjectManager()->get(Random::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate policies preserved when reading block from cache.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
* @magentoCache block_html enabled
|
||||
*/
|
||||
public function testCachedPolicies(): void
|
||||
{
|
||||
/** @var Template $block */
|
||||
$block = $this->layout->createBlock(
|
||||
Template::class,
|
||||
'test-block',
|
||||
['data' => ['cache_lifetime' => 3600, 'cache_key' => $this->random->getRandomString(32)]]
|
||||
);
|
||||
$block->setTemplate('Magento_TestModuleCspUtil::secure.phtml');
|
||||
//Clearing previously added just in case.
|
||||
$this->dynamicCollected->consumeAdded();
|
||||
|
||||
$block->toHtml();
|
||||
$dynamic = $this->dynamicCollected->consumeAdded();
|
||||
$this->assertNotEmpty($dynamic);
|
||||
|
||||
//From cache
|
||||
$block->toHtml();
|
||||
$cached = $this->dynamicCollected->consumeAdded();
|
||||
$this->assertEquals($dynamic, $cached);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Csp;
|
||||
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Test that controllers can modify CSPs for a page.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
class CspAwareActionTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Check that a CSP aware action can modify CSPs after ALL other policies had been gathered.
|
||||
*
|
||||
* @return void
|
||||
* @magentoConfigFixture default_store csp/mode/storefront/report_only 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script/policy_id script-src
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script/hosts/example http://controller.magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script/self 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script/inline 0
|
||||
*/
|
||||
public function testAwareAction(): void
|
||||
{
|
||||
$this->getRequest()->setMethod('GET');
|
||||
$this->dispatch('csputil/csp/aware');
|
||||
$header = $this->getResponse()->getHeader('Content-Security-Policy');
|
||||
$this->assertNotEmpty($header);
|
||||
|
||||
$this->assertStringContainsString(
|
||||
'script-src https://controller.magento.com'
|
||||
.' \'self\' \'sha256-H4RRnauTM2X2Xg/z9zkno1crqhsaY3uKKu97uwmnXXE=\'',
|
||||
$header->getFieldValue()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Csp;
|
||||
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Test CSP being rendered when Magento processes an HTTP request.
|
||||
*/
|
||||
class CspTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Search the whole response for a string.
|
||||
*
|
||||
* @param \Magento\Framework\App\ResponseInterface|\Magento\Framework\App\Response\Http $response
|
||||
* @param string $search
|
||||
* @return bool
|
||||
*/
|
||||
private function searchInResponse($response, string $search): bool
|
||||
{
|
||||
foreach ($response->getHeaders() as $header) {
|
||||
if (mb_stripos(mb_strtolower($header->toString()), mb_strtolower($search)) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that configured policies are rendered on frontend.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/policy_id default-src
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/hosts/example http://magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/hosts/example2 http://devdocs.magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/self 1
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script_src/policy_id script-src
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script_src/self 1
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/script_src/inline 1
|
||||
* @magentoConfigFixture default_store csp/policies/admin/font_src/policy_id font-src
|
||||
* @magentoConfigFixture default_store csp/policies/admin/font_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/admin/font_src/self 1
|
||||
* @return void
|
||||
*/
|
||||
public function testStorefrontPolicies(): void
|
||||
{
|
||||
$this->dispatch('/');
|
||||
$response = $this->getResponse();
|
||||
|
||||
$this->assertTrue($this->searchInResponse($response, 'Content-Security-Policy'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'default-src'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://devdocs.magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, '\'self\''));
|
||||
$this->assertFalse($this->searchInResponse($response, '\'none\''));
|
||||
$this->assertTrue($this->searchInResponse($response, 'script-src'));
|
||||
$this->assertTrue($this->searchInResponse($response, '\'unsafe-inline\''));
|
||||
$this->assertTrue($this->searchInResponse($response, 'font-src'));
|
||||
//Policies configured in cps_whitelist.xml files
|
||||
$this->assertTrue($this->searchInResponse($response, 'object-src'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'media-src'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that configured policies are rendered on backend.
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoConfigFixture default_store csp/policies/admin/default_src/policy_id default-src
|
||||
* @magentoConfigFixture default_store csp/policies/admin/default_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/admin/default_src/hosts/example http://magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/admin/default_src/hosts/example2 http://devdocs.magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/admin/default_src/self 1
|
||||
* @magentoConfigFixture default_store csp/policies/admin/script_src/policy_id script-src
|
||||
* @magentoConfigFixture default_store csp/policies/admin/script_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/admin/default_src/self 1
|
||||
* @magentoConfigFixture default_store csp/policies/admin/default_src/inline 1
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/font_src/policy_id font-src
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/font_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/font_src/self 1
|
||||
* @return void
|
||||
*/
|
||||
public function testAdminPolicies(): void
|
||||
{
|
||||
$this->dispatch('backend/');
|
||||
$response = $this->getResponse();
|
||||
|
||||
$this->assertTrue($this->searchInResponse($response, 'Content-Security-Policy'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'default-src'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://devdocs.magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, '\'self\''));
|
||||
$this->assertFalse($this->searchInResponse($response, '\'none\''));
|
||||
$this->assertTrue($this->searchInResponse($response, 'script-src'));
|
||||
$this->assertTrue($this->searchInResponse($response, '\'unsafe-inline\''));
|
||||
$this->assertTrue($this->searchInResponse($response, 'font-src'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that CSP mode is considered when rendering policies.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/policy_id default-src
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/hosts/example http://magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/hosts/example2 http://devdocs.magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/self 1
|
||||
* @magentoConfigFixture default_store csp/mode/storefront/report_only 1
|
||||
* @magentoConfigFixture default_store csp/mode/storefront/report_uri /cspEndpoint/
|
||||
* @magentoConfigFixture default_store csp/mode/admin/report_only 0
|
||||
* @return void
|
||||
*/
|
||||
public function testReportOnlyMode(): void
|
||||
{
|
||||
$this->dispatch('/');
|
||||
$response = $this->getResponse();
|
||||
|
||||
$this->assertTrue($this->searchInResponse($response, 'Content-Security-Policy-Report-Only'));
|
||||
$this->assertTrue($this->searchInResponse($response, '/cspEndpoint/'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'default-src'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://devdocs.magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, '\'self\''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that CSP reporting options are rendered for 'restrict' mode as well.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/policy_id default-src
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/none 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/hosts/example http://magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/hosts/example2 http://devdocs.magento.com
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/default_src/self 1
|
||||
* @magentoConfigFixture default_store csp/mode/storefront/report_only 0
|
||||
* @magentoConfigFixture default_store csp/mode/storefront/report_uri /cspEndpoint/
|
||||
* @magentoConfigFixture default_store csp/mode/admin/report_only 0
|
||||
* @return void
|
||||
*/
|
||||
public function testRestrictMode(): void
|
||||
{
|
||||
$this->dispatch('/');
|
||||
$response = $this->getResponse();
|
||||
|
||||
$this->assertFalse($this->searchInResponse($response, 'Content-Security-Policy-Report-Only'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'Content-Security-Policy'));
|
||||
$this->assertTrue($this->searchInResponse($response, '/cspEndpoint/'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'default-src'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, 'http://devdocs.magento.com'));
|
||||
$this->assertTrue($this->searchInResponse($response, '\'self\''));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Csp;
|
||||
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Test CSP util use cases.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
class CspUtilTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Test that CSP helper for templates works.
|
||||
*
|
||||
* @return void
|
||||
* @magentoConfigFixture default_store csp/mode/storefront/report_only 0
|
||||
* @magentoConfigFixture default_store csp/policies/storefront/scripts/inline 0
|
||||
*/
|
||||
public function testPhtmlHelper(): void
|
||||
{
|
||||
$this->getRequest()->setMethod('GET');
|
||||
$this->dispatch('csputil/csp/helper');
|
||||
$content = $this->getResponse()->getContent();
|
||||
|
||||
$this->assertStringContainsString(
|
||||
'<script src="http://my.magento.com/static/script.js"></script>',
|
||||
$content
|
||||
);
|
||||
$this->assertStringContainsString("<script>\n let myVar = 1;\n</script>", $content);
|
||||
$header = $this->getResponse()->getHeader('Content-Security-Policy');
|
||||
$this->assertNotEmpty($header);
|
||||
$this->assertStringContainsString('http://my.magento.com', $header->getFieldValue());
|
||||
$this->assertStringContainsString('\'sha256-H4RRnauTM2X2Xg/z9zkno1crqhsaY3uKKu97uwmnXXE=\'', $header->getFieldValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Csp\Helper;
|
||||
|
||||
use Magento\Csp\Api\Data\PolicyInterface;
|
||||
use Magento\Csp\Model\Collector\DynamicCollector;
|
||||
use Magento\Csp\Model\Collector\DynamicCollectorMock;
|
||||
use Magento\Csp\Model\Policy\FetchPolicy;
|
||||
use Magento\Framework\View\Helper\SecureHtmlRenderer;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Cover CSP util use cases.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
class InlineUtilTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var InlineUtil
|
||||
*/
|
||||
private $util;
|
||||
|
||||
/**
|
||||
* @var SecureHtmlRenderer
|
||||
*/
|
||||
private $secureHtmlRenderer;
|
||||
|
||||
/**
|
||||
* @var DynamicCollectorMock
|
||||
*/
|
||||
private $dynamicCollector;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
Bootstrap::getObjectManager()->configure([
|
||||
'preferences' => [
|
||||
DynamicCollector::class => DynamicCollectorMock::class
|
||||
]
|
||||
]);
|
||||
$this->util = Bootstrap::getObjectManager()->get(InlineUtil::class);
|
||||
$this->secureHtmlRenderer = Bootstrap::getObjectManager()->get(SecureHtmlRenderer::class);
|
||||
$this->dynamicCollector = Bootstrap::getObjectManager()->get(DynamicCollector::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->util = null;
|
||||
$this->secureHtmlRenderer = null;
|
||||
$this->dynamicCollector->consumeAdded();
|
||||
$this->dynamicCollector = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test tag rendering.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param array $attributes
|
||||
* @param string|null $content
|
||||
* @param string $result Expected result.
|
||||
* @param PolicyInterface[] $policiesExpected
|
||||
* @return void
|
||||
*
|
||||
* @dataProvider getTags
|
||||
* @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/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
|
||||
*/
|
||||
public function testRenderTag(
|
||||
string $tagName,
|
||||
array $attributes,
|
||||
?string $content,
|
||||
string $result,
|
||||
array $policiesExpected
|
||||
): void {
|
||||
$this->assertEquals($result, $this->util->renderTag($tagName, $attributes, $content));
|
||||
$this->assertEquals($policiesExpected, $this->dynamicCollector->consumeAdded());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test data for tag rendering test.
|
||||
*
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function getTags(): array
|
||||
{
|
||||
return [
|
||||
'remote-script' => [
|
||||
'script',
|
||||
['src' => 'http://magento.com/static/some-script.js'],
|
||||
null,
|
||||
'<script src="http://magento.com/static/some-script.js"></script>',
|
||||
[new FetchPolicy('script-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'inline-script' => [
|
||||
'script',
|
||||
['type' => 'text/javascript'],
|
||||
"\n let someVar = 25;\n document.getElementById('test').innerText = someVar;\n",
|
||||
"<script type=\"text/javascript\">\n let someVar = 25;"
|
||||
."\n document.getElementById('test').innerText = someVar;\n</script>",
|
||||
[
|
||||
new FetchPolicy(
|
||||
'script-src',
|
||||
false,
|
||||
[],
|
||||
[],
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
[],
|
||||
['U+SKpEef030N2YgyKKdIBIvPy8Fmd42N/JcTZgQV+DA=' => 'sha256']
|
||||
)
|
||||
]
|
||||
],
|
||||
'remote-style' => [
|
||||
'link',
|
||||
['rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'http://magento.com/static/style.css'],
|
||||
null,
|
||||
'<link rel="stylesheet" type="text/css"'
|
||||
. ' href="http://magento.com/static/style.css"/>',
|
||||
[new FetchPolicy('style-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'inline-style' => [
|
||||
'style',
|
||||
[],
|
||||
"\n h1 {color: red;}\n p {color: green;}\n",
|
||||
"<style>\n h1 {color: red;}\n p {color: green;}\n</style>",
|
||||
[
|
||||
new FetchPolicy(
|
||||
'style-src',
|
||||
false,
|
||||
[],
|
||||
[],
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
[],
|
||||
['KISO7smrk+XdGrEsiPvVjX6qx4wNef/UKjNb26RaKGM=' => 'sha256']
|
||||
)
|
||||
]
|
||||
],
|
||||
'remote-image' => [
|
||||
'img',
|
||||
['src' => 'http://magento.com/static/my.jpg'],
|
||||
null,
|
||||
'<img src="http://magento.com/static/my.jpg"/>',
|
||||
[new FetchPolicy('img-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'remote-font' => [
|
||||
'style',
|
||||
['type' => 'text/css'],
|
||||
"\n @font-face {\n font-family: \"MyCustomFont\";"
|
||||
."\n src: url(\"http://magento.com/static/font.ttf\");\n }\n"
|
||||
." @font-face {\n font-family: \"MyCustomFont2\";"
|
||||
."\n src: url('https://magento.com/static/font-2.ttf'),"
|
||||
."\n url(static/font.ttf),"
|
||||
."\n url(https://devdocs.magento.com/static/another-font.woff),"
|
||||
."\n url(http://devdocs.magento.com/static/font.woff);\n }\n",
|
||||
"<style type=\"text/css\">"
|
||||
."\n @font-face {\n font-family: \"MyCustomFont\";"
|
||||
."\n src: url(\"http://magento.com/static/font.ttf\");\n }\n"
|
||||
." @font-face {\n font-family: \"MyCustomFont2\";"
|
||||
."\n src: url('https://magento.com/static/font-2.ttf'),"
|
||||
."\n url(static/font.ttf),"
|
||||
."\n url(https://devdocs.magento.com/static/another-font.woff),"
|
||||
."\n url(http://devdocs.magento.com/static/font.woff);\n }\n"
|
||||
."</style>",
|
||||
[
|
||||
new FetchPolicy(
|
||||
'style-src',
|
||||
false,
|
||||
[
|
||||
'http://magento.com',
|
||||
'https://magento.com',
|
||||
'https://devdocs.magento.com',
|
||||
'http://devdocs.magento.com'
|
||||
]
|
||||
),
|
||||
new FetchPolicy(
|
||||
'style-src',
|
||||
false,
|
||||
[],
|
||||
[],
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
[],
|
||||
['TP6Ulnz1kstJ8PYUKvowgJm0phHhtqJnJCnWxKLXkf0=' => 'sha256']
|
||||
)
|
||||
]
|
||||
],
|
||||
'cross-origin-form' => [
|
||||
'form',
|
||||
['action' => 'https://magento.com/submit', 'method' => 'post'],
|
||||
"\n <input type=\"text\" name=\"test\" /><input type=\"submit\" value=\"Submit\" />\n",
|
||||
"<form action=\"https://magento.com/submit\" method=\"post\">"
|
||||
."\n <input type=\"text\" name=\"test\" /><input type=\"submit\" value=\"Submit\" />\n"
|
||||
."</form>",
|
||||
[new FetchPolicy('form-action', false, ['https://magento.com'])]
|
||||
],
|
||||
'cross-origin-iframe' => [
|
||||
'iframe',
|
||||
['src' => 'http://magento.com/some-page'],
|
||||
null,
|
||||
'<iframe src="http://magento.com/some-page"></iframe>',
|
||||
[new FetchPolicy('frame-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'remote-track' => [
|
||||
'track',
|
||||
['src' => 'http://magento.com/static/track.vtt', 'kind' => 'subtitles'],
|
||||
null,
|
||||
'<track src="http://magento.com/static/track.vtt" kind="subtitles"/>',
|
||||
[new FetchPolicy('media-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'remote-source' => [
|
||||
'source',
|
||||
['src' => 'http://magento.com/static/track.ogg', 'type' => 'audio/ogg'],
|
||||
null,
|
||||
'<source src="http://magento.com/static/track.ogg" type="audio/ogg"/>',
|
||||
[new FetchPolicy('media-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'remote-video' => [
|
||||
'video',
|
||||
['src' => 'https://magento.com/static/video.mp4'],
|
||||
null,
|
||||
'<video src="https://magento.com/static/video.mp4"></video>',
|
||||
[new FetchPolicy('media-src', false, ['https://magento.com'])]
|
||||
],
|
||||
'remote-audio' => [
|
||||
'audio',
|
||||
['src' => 'https://magento.com/static/audio.mp3'],
|
||||
null,
|
||||
'<audio src="https://magento.com/static/audio.mp3"></audio>',
|
||||
[new FetchPolicy('media-src', false, ['https://magento.com'])]
|
||||
],
|
||||
'remote-object' => [
|
||||
'object',
|
||||
['data' => 'http://magento.com/static/flash.swf'],
|
||||
null,
|
||||
'<object data="http://magento.com/static/flash.swf"></object>',
|
||||
[new FetchPolicy('object-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'remote-embed' => [
|
||||
'embed',
|
||||
['src' => 'http://magento.com/static/flash.swf'],
|
||||
null,
|
||||
'<embed src="http://magento.com/static/flash.swf"/>',
|
||||
[new FetchPolicy('object-src', false, ['http://magento.com'])]
|
||||
],
|
||||
'remote-applet' => [
|
||||
'applet',
|
||||
['code' => 'SomeApplet.class', 'archive' => 'https://magento.com/applet/my-applet.jar'],
|
||||
null,
|
||||
'<applet code="SomeApplet.class" '
|
||||
. 'archive="https://magento.com/applet/my-applet.jar"></applet>',
|
||||
[new FetchPolicy('object-src', false, ['https://magento.com'])]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that inline event listeners are rendered properly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderEventListener(): void
|
||||
{
|
||||
$result = $this->util->renderEventListener('onclick', 'alert()');
|
||||
$this->assertEquals('onclick="alert()"', $result);
|
||||
$this->assertEquals(
|
||||
[new FetchPolicy('script-src', false, [], [], false, true)],
|
||||
$this->dynamicCollector->consumeAdded()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that CSP logic was added to SecureHtmlRenderer
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSecureHtmlRenderer(): void
|
||||
{
|
||||
$scriptTag = $this->secureHtmlRenderer->renderTag(
|
||||
'script',
|
||||
['src' => 'https://test.magento.com/static/script.js']
|
||||
);
|
||||
$eventListener = $this->secureHtmlRenderer->renderEventListener('onclick', 'alert()');
|
||||
|
||||
$this->assertEquals(
|
||||
'<script src="https://test.magento.com/static/script.js"></script>',
|
||||
$scriptTag
|
||||
);
|
||||
$this->assertEquals(
|
||||
'onclick="alert()"',
|
||||
$eventListener
|
||||
);
|
||||
$policies = $this->dynamicCollector->consumeAdded();
|
||||
$this->assertTrue(in_array(new FetchPolicy('script-src', false, ['https://test.magento.com']), $policies));
|
||||
$this->assertTrue(in_array(new FetchPolicy('script-src', false, [], [], false, true), $policies));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that hashes are not calculated when inline sources are allowed.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @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 1
|
||||
* @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/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 1
|
||||
*/
|
||||
public function testRenderWithInline(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
'<script>alert(1);</script>',
|
||||
$this->util->renderTag('script', [], 'alert(1);')
|
||||
);
|
||||
$this->assertEmpty($this->dynamicCollector->consumeAdded());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
<?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 PHPUnit\Framework\TestCase;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test collecting policies from Magento config.
|
||||
*/
|
||||
class ConfigCollectorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ConfigCollector
|
||||
*/
|
||||
private $collector;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->collector = Bootstrap::getObjectManager()->get(ConfigCollector::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, false, false, [], [], 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/upgrade/policy_id upgrade-insecure-requests
|
||||
* @return void
|
||||
*/
|
||||
public function testCollecting(): void
|
||||
{
|
||||
$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));
|
||||
}
|
||||
}
|
||||
+85
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
+136
@@ -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());
|
||||
}
|
||||
}
|
||||
+182
@@ -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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user