born05/craft-csp

使用非ces生成内容安全策略(CSP)。

安装次数: 8,223

依赖项: 0

建议者: 0

安全: 0

星标: 10

关注者: 8

分支: 8

开放问题: 0

类型:craft-plugin

3.0.0 2024-08-20 11:44 UTC

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

查看许可