bnomei / kirby3-security-headers

Kirby 插件,用于简化安全头部设置

4.0.0 2024-03-01 10:45 UTC

README

Release Downloads Build Status Coverage Status Maintainability
Twitter

Kirby 插件,用于简化安全头部设置。

🔐 为什么你应该使用这个插件?因为安全很重要。保护你或你的客户的网站及其客户数据是重要的。

  1. 自动设置
  2. 设置:头部
  3. 设置:加载器
  4. 设置:设置器
  5. 前端 Nonce
  6. 设置

商业使用


支持开源!

这个插件是免费的,但如果你在商业项目中使用它,请考虑赞助我或捐款。
如果我的工作帮助你赚了一些钱,我认为我可能也应该得到一些回报,对吗?

行善。分享一点。谢谢。

—— Bruno
 

安装

  • master.zip 解压为文件夹 site/plugins/kirby3-security-headers
  • git submodule add https://github.com/bnomei/kirby3-security-headers.git site/plugins/kirby3-security-headers
  • composer require bnomei/kirby3-security-headers

设置

自动

一个 route:before 钩子负责自动设置头部,除非以下条件之一适用

  • Kirby 的 全局 调试模式设置为 true
  • Kirby 判断这是一个 本地设置
  • 插件设置 enabled 设置为 false

头部

以下头部将默认应用。您可以在配置文件中覆盖它们。

/site/config/config.php

<?php
return [
    'bnomei.securityheaders.headers' => [
        "X-Powered-By" => "", // unset
        "X-Frame-Options" => "SAMEORIGIN",
        "X-XSS-Protection" => "1; mode=block",
        "X-Content-Type-Options" => "nosniff",
        "strict-transport-security" => "max-age=31536000; includeSubdomains",
        "Referrer-Policy" => "no-referrer-when-downgrade",
        "Permissions-Policy" => 'interest-cohort=()', // flock-off,
        // ... FEATURE POLICIES
    // other options...
];

加载器

加载器用于使用给定的一组数据初始化 CSPBuilder 对象。您可以跳过该步骤,传递一个文件进行加载,提供一个数组或使用默认的加载器文件。在添加额外的 font-src 为 google web fonts 等情况下,建议使用自定义文件。

/site/config/config.php

<?php
return [
    'bnomei.securityheaders.loader' => function () {
        // https://github.com/paragonie/csp-builder#example
        // null if you do NOT want to use default and/or just the setter
        /*
            return null;
         */
        // return path of file (json or yaml)
        // or an array of options for the cspbuilder
        /*
            return [...];
            return kirby()->roots()->site() . '/your-csp.json';
            return kirby()->roots()->site() . '/your-csp.yml';
        */
        // otherwise forward the default file from this plugin
        return __DIR__ . '/loader.json';
    },
    // other options...
];

设置器

设置器在加载器之后应用。使用它添加动态内容,如哈希和非ce。

/site/config/config.php

<?php
return [
    'bnomei.securityheaders.setter' => function (\Bnomei\SecurityHeaders $instance) {
        // https://github.com/paragonie/csp-builder#build-a-content-security-policy-programmatically
        /** @var ParagonIE\CSPBuilder\CSPBuilder $csp */
        /*
            $csp = $instance->csp();
            $nonce = $instance->setNonce('my-inline-script');
            $csp->nonce('script-src', $nonce);
        */
        // in your template retrieve it again with
        /*
            $nonce = $page->nonce('my-inline-script');
            => `THIS-IS-THE-NONCE`
            $attr = $page->nonceAttr('my-inline-script');
            => `nonce="THIS-IS-THE-NONCE"`
        */
    },
    // other options...
];

提示:nonce 在 setter 中设置,稍后使用 $page->nonce(...)$page->nonceAttr(...) 获取。

面板和前端 Nonce

此插件自动注册 Kirby 的 nonce 以供面板使用。为了方便起见,它还提供了一个单个的 前端 nonce,您可以用作 <link><style><script> 元素的属性。您可以使用 site()->nonce() 获取 nonce,使用 site()->nonceAttr() 获取完整的属性。

<?php ?>

<script nonce="<?= site()->nonce() ?>">
// ...
</script>

<style <?= site()->nonceAttr() ?>>

</style>

提示:srcset 插件 也使用该 前端 nonce

设置

依赖项

免责声明

本插件“原样”提供,不提供任何保证。自行承担使用风险,并在将其用于生产环境之前自行测试。如果您发现任何问题,请 创建新问题

许可证

MIT

不建议在宣传种族主义、性别歧视、恐同、动物虐待、暴力或任何其他仇恨言论的项目中使用此插件。