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,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestModuleCspUtil\Controller\Csp;
use Magento\Csp\Api\CspAwareActionInterface;
use Magento\Csp\Model\Policy\FetchPolicy;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
/**
* CSP Aware controller.
*/
class Aware extends Action implements CspAwareActionInterface
{
/**
* @inheritDoc
*/
public function execute()
{
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
}
/**
* @inheritDoc
*/
public function modifyCsp(array $appliedPolicies): array
{
$policies = [];
foreach ($appliedPolicies as $policy) {
if ($policy instanceof FetchPolicy
&& in_array('http://controller.magento.com', $policy->getHostSources(), true)
) {
$policies[] = new FetchPolicy(
'script-src',
false,
['https://controller.magento.com'],
[],
true,
false,
false,
[],
['H4RRnauTM2X2Xg/z9zkno1crqhsaY3uKKu97uwmnXXE=' => 'sha256']
);
}
}
return $policies;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestModuleCspUtil\Controller\Csp;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;
/**
* .phtml templates utilizes CSP helper.
*/
class Helper extends Action
{
/**
* @inheritDoc
*/
public function execute()
{
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
}
}
@@ -0,0 +1,21 @@
{
"name": "magento/module-csp-util",
"description": "test csp module",
"config": {
"sort-packages": true
},
"require": {
"php": "~7.4.0||~8.0.0",
"magento/framework": "*",
"magento/module-integration": "*"
},
"type": "magento2-module",
"extra": {
"map": [
[
"*",
"Magento/TestModuleCspUtil"
]
]
}
}
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp/etc/csp_whitelist.xsd">
<policies>
<policy id="script-src">
<values>
<value id="devdocs-base" type="host">https://devdocs.magento.com</value>
</values>
</policy>
</policies>
</csp_whitelist>
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="csputil" frontName="csputil">
<module name="Magento_TestModuleCspUtil" />
</route>
</router>
</config>
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_TestModuleCspUtil" active="true" />
</config>
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Framework\Component\ComponentRegistrar;
$registrar = new ComponentRegistrar();
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Magento_TestModuleCspUtil') === null) {
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestModuleCspUtil', __DIR__);
}
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template"
name="csp_helper"
cacheable="false"
template="Magento_TestModuleCspUtil::helper.phtml" />
</referenceContainer>
</body>
</page>
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template"
name="csp_helper"
cacheable="false"
template="Magento_TestModuleCspUtil::helper.phtml" />
</referenceContainer>
</body>
</page>
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Csp\Api\InlineUtilInterface $csp */
?>
<h1>Hello there!</h1>
<?= /* @noEscape */ $csp->renderTag('script', ['src' => 'http://my.magento.com/static/script.js']); ?>
<?= /* @noEscape */ $csp->renderTag('script', [], "\n let myVar = 1;\n") ?>
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<?= /* @NoEscape */ $secureRenderer->renderTag('script', ['type' => 'text/javascript'], 'let var = 1; console.log("var = " + var);', false) ?>
<?= /* @NoEscape */ $secureRenderer->renderTag('script', ['type' => 'text/javascript', 'src' => 'https://magento.com/scripts/some-script.js']) ?>