stevenmaguire/middleware-csp

为PSR 7响应中的头部提供执行内容安全策略的支持。

0.1.2 2015-08-07 18:21 UTC

This package is auto-updated.

Last update: 2024-09-14 11:23:14 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

为PSR 7响应中的头部提供执行内容安全策略的支持。

关于CSP(内容安全策略)

新的Content-Security-Policy HTTP响应头部通过声明允许通过HTTP头部加载哪些动态资源来帮助您减少现代浏览器上的XSS风险。 - 来自 content-security-policy.com

Google上的简短总结

  • 使用白名单来告诉客户端允许和不允许的内容。
  • 了解可用的指令。
  • 了解它们所使用的关键字。
  • 内联代码和eval()被认为是有害的。
  • 在执行之前向服务器报告策略违规。

安装

通过Composer

$ composer require stevenmaguire/middleware-csp

用法

框架和路由层项目可能对中间件有不同的实现。本包旨在帮助实现许多这些变化,前提是中间件模式期望提供Psr\Http\Message\ResponseInterface并返回更新后的Psr\Http\Message\ResponseInterface

通用示例

<?php namespace Stevenmaguire\Http\Middleware\Test;

use Psr\Http\Message\ResponseInterface;
use Stevenmaguire\Http\Middleware\EnforceContentSecurity;

class GenericMiddleware extends EnforceContentSecurity
{
    /**
     * Applies content security policy to given response.
     *
     * @param  ResponseInterface  $response
     * @param  array              $profiles
     *
     * @return ResponseInterface
     */
    public function handle(ResponseInterface $response, $profiles = [])
    {
        array_map(function ($profile) {
            $this->loadProfileByKey($profile);
        }, $profiles);

        return $this->addPolicyHeader($response);
    }

    /**
     * Adds profile configuration to underlying middleware.
     *
     * @param array  $profileConfig
     *
     * @return EnforceContentSecurity
     */
    public function addProfileConfiguration($profileConfig = [])
    {
        return $this->setProfiles($profileConfig);
    }

    /**
     * Encodes a given configuration into formatted directive string.
     *
     * @param  array   $config
     *
     * @return string
     */
    public function getEncodedConfiguration($config = [])
    {
        return $this->encodeConfiguration($config);
    }
}

在这个示例中,$profiles是一个数组,包含了针对middleware-csp-php的特定配置,它指导包如何装饰响应。

这是两个配置文件的示例。

// within config/security.php

return [
    'content' => [
        'default' => 'global',
        'profiles' => [
            'global' => [
                'base-uri' => "'self'",
                'default-src' => "'self'",
                'font-src' => [ // e.g. only allows fonts from your server and fonts.gstatic.com
                    "'self'",
                    'fonts.gstatic.com'
                ],
                'img-src' => "'self'",
                'script-src' => "'self'",
                'style-src' => [
                    "'self'",
                    "'unsafe-inline'",
                    'fonts.googleapis.com'
                ],
            ],
            'flickr' => [
                'img-src' => [
                    'https://*.staticflickr.com',
                ],
            ],
        ],
    ],
];

框架特定实现

定义CPS

应尽可能将内容安全策略保持得尽可能严格。最好不要允许内联脚本,只允许来自可信源文件。只添加您积极使用的源,而不是您可能将来会使用的源。

CSP 1.0规范

CSP 2.0中的新功能

浏览器支持

这是CSP在浏览器中的支持的高级总结。有关更详细的规定,请参阅Mozillacaniuse

测试

$ ./vendor/bin/phpunit

贡献

有关详细信息,请参阅CONTRIBUTING

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件