zae/content-security-policy

一种非常简单的方式来构建CSP头并将其添加到响应中。

2.5.0 2020-09-21 15:53 UTC

This package is auto-updated.

Last update: 2024-09-22 00:53:22 UTC


README

Latest Version on Packagist Software License Total Downloads codecov

一种非常简单的方式来构建CSP头并将其添加到响应中。

官方支持的平台

  • Laravel: ^5.8
  • Craft: ^3.0

安装

通过Composer

$ composer require zae/content-security-policy

Laravel

中间件

将中间件添加到中间件Kernel中。

protected $middlewareGroups = [
    'web' => [
        ...
        \Zae\ContentSecurityPolicy\Laravel\Http\Middleware\ContentSecurityPolicy::class
    ],
]

配置 (config/csp.php)

return [
	BlockAllMixedContent::class,
    Sandbox::class => [
        Sandbox::ALLOW_FORMS,
        Sandbox::ALLOW_SCRIPTS,
        Sandbox::ALLOW_TOP_NAVIGATION,
        Sandbox::ALLOW_SAME_ORIGIN,
        Sandbox::ALLOW_POPUPS,
    ]
];

Craft 3

该库包括一个Craft 3模块,可以发送CSP头和一个获取当前CSP nonce的twig函数。

按如下方式注册模块

'modules' => [
    'csp' => \Zae\ContentSecurityPolicy\Craft\Module::class,
],
'bootstrap' => [
    'csp'
]

按如下方式使用twig函数

<script nonce="{{ cspnonce() }}">
    // inline javascript
</script>

配置 (config/csp.php)

return [
    'components' => [
        'builder' => Builder::class,
    ],
    'params' => [
        BlockAllMixedContent::class,
        Sandbox::class => [
            Sandbox::ALLOW_FORMS,
            Sandbox::ALLOW_SCRIPTS,
            Sandbox::ALLOW_TOP_NAVIGATION,
            Sandbox::ALLOW_SAME_ORIGIN,
            Sandbox::ALLOW_POPUPS,
        ]
    ]
];

其他

尽管目前尚未官方支持,但可以使用此库与其它框架结合,一个简单的方法是通过FluidDirectivesFactory。

Fluid工厂

<?php
$csp = new CSP();
$factory = new FluidDirectivesFactory($csp);
$factory
    ->blockAllMixedContent()
    ->defaultSrc([
        Directive::SELF,
        'https:'
    ])
    ->baseUri([
        Directive::SELF
    ]);

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献指南

安全

如果您发现任何安全相关的问题,请通过电子邮件 ezra@tsdme.nl 而不是使用问题跟踪器。

鸣谢

许可

MIT许可证(MIT)。请参阅 许可文件 获取更多信息。