born05 / craft-csp
使用非ces生成内容安全策略(CSP)。
3.0.0
2024-08-20 11:44 UTC
Requires
- php: ^8.0.2
- craftcms/cms: ^4.0.0-alpha|^5.0.0-beta.1
This package is auto-updated.
Last update: 2024-09-20 12:01:36 UTC
README
使用非ces生成内容安全策略(CSP)。
目前与{% js %}{% endjs %}
块代码twig标签不兼容。
要求
- Craft 4.0.0 及以上版本
- PHP 8.0.2 及以上版本
安装
要安装此插件,请在插件商店中搜索“内容安全策略”或
composer require born05/craft-csp
设置
可以使用config/content-security-policy.php
进行配置或使用非ces。
{# Regular html #} <script src="url/of/script.js" nonce="{{ cspNonce('script-src') }}"></script> <link href="url/of/style.css" rel="stylesheet" nonce="{{ cspNonce('style-src') }}" /> {# Twig tags #} {% css inlineCSS with {nonce: cspNonce('style-src')} %} {% js 'example.js' with {nonce: cspNonce('script-src')} %}
示例config/content-security-policy.php
<?php return [ 'enabled' => true, 'reportOnly' => false, 'baseUri' => [ "'none'", ], 'defaultSrc' => [], 'scriptSrc' => [ "'self'", ], 'styleSrc' => [ "'self'", ], 'imgSrc' => [ "'self'", ], 'connectSrc' => [], 'fontSrc' => [], 'objectSrc' => [], 'mediaSrc' => [], 'frameSrc' => [], 'sandbox' => [], 'reportUri' => [], 'childSrc' => [], 'formAction' => [], 'frameAncestors' => [], 'pluginTypes' => [], 'reportTo' => [], 'workerSrc' => [], 'manifestSrc' => [], 'navigateTo' => [], ];
故障排除
如果使用SEOMatic插件,该插件添加的非ces将与此插件的配置冲突。您可以在/admin/seomatic/plugin#tags
禁用此功能并使用以下代码重新启用脚本
{% do seomatic.script.get("googleAnalytics").nonce(cspNonce('script-src')) %}
有关配置选项,请参阅:Settings.php
许可
版权所有 © Born05
查看许可