stevenmaguire / laravel-middleware-csp
为Laravel响应中的头提供支持,以强制执行内容安全策略。
Requires
- php: >=5.5.9
- guzzlehttp/psr7: ^1.1
- illuminate/http: ^5.1
- stevenmaguire/middleware-csp: ^0.1
Requires (Dev)
- mockery/mockery: 0.9.*@dev
- phpunit/phpunit: 3.7.*
- squizlabs/php_codesniffer: ~2.0
This package is auto-updated.
Last update: 2024-09-14 10:38:14 UTC
README
为Laravel响应中的头提供支持,以强制执行内容安全策略。此包扩展并利用了适用于PSR 7响应的无框架内容安全策略中间件。
安装
通过Composer
$ composer require stevenmaguire/laravel-middleware-csp
使用
注册为路由中间件
// within app/Http/Kernal.php protected $routeMiddleware = [ // 'secure.content' => \Stevenmaguire\Laravel\Http\Middleware\EnforceContentSecurity::class, // ];
将内容安全策略应用于路由
以下会将所有默认配置文件应用于gallery
路由。
// within app/Http/routes.php Route::get('gallery', ['middleware' => 'secure.content'], function () { return 'pictures!'; });
以下会将所有默认配置文件和特定的flickr
配置文件应用于gallery
路由。
// within app/Http/routes.php Route::get('gallery', ['middleware' => 'secure.content:flickr'], function () { return 'pictures!'; });
将内容安全策略应用于控制器
以下会将所有默认配置文件应用于GalleryController
中的所有方法。
// within app/Http/Controllers/GalleryController.php public function __construct() { $this->middleware('secure.content'); }
以下会将所有默认配置文件和特定的google
配置文件应用于GalleryController
中的所有方法。
// within app/Http/Controllers/GalleryController.php public function __construct() { $this->middleware('secure.content:google'); }
您可以将任意数量的特定配置文件包含在任何中间件装饰中。例如,以下会将默认的、google
、flickr
和my_custom
配置文件应用于GalleryController
中的所有方法。
// within app/Http/Controllers/GalleryController.php public function __construct() { $this->middleware('secure.content:google,flickr,my_custom'); }
创建内容安全配置文件
内容安全配置文件的默认位置为security.content
。如果您希望使用此默认配置,请确保您的项目包含适当的配置文件。
您可以在owasp CSP Cheatsheet上找到所有可用选项。
此配置数组的结构非常重要。中间件期望找到具有字符串值的default
键和一个具有数组值的profiles
键。
// within config/security.php return [ 'content' => [ 'default' => '', 'profiles' => [], ], ];
profiles
数组包含应用程序的安全配置文件。每个配置文件名称必须是唯一的,并且期望有一个数组值。
// within config/security.php return [ 'content' => [ 'default' => '', 'profiles' => [ 'profile_one' => [], 'profile_two' => [], 'profile_three' => [], ], ], ];
每个配置文件数组应包含与内容安全策略指令对应的键。这些指令的值可以是字符串、以逗号分隔的字符串或字符串数组。每个字符串值应对应于与您的指令和配置文件相关的域。
// within config/security.php return [ 'content' => [ 'default' => '', 'profiles' => [ 'profile_one' => [ 'base-uri' => 'https://domain.com,http://google.com', ], 'profile_two' => [ 'font-src' => 'https://domain.com', 'base-uri' => [ "'self'", 'http://google.com' ], ], 'profile_three' => [ 'font-src' => [ "'self'" ], ], ], ], ];
default
键值应是一个字符串、以逗号分隔的字符串或字符串数组,对应于您希望在所有响应上强制执行的唯一配置文件名称,并应用最小内容安全。
// within config/security.php return [ 'content' => [ 'default' => 'profile_one', 'profiles' => [ 'profile_one' => [ 'base-uri' => 'https://domain.com,http://google.com', ], 'profile_two' => [ 'font-src' => 'https://domain.com', 'base-uri' => [ "'self'", 'http://google.com' ], ], 'profile_three' => [ 'font-src' => [ "'self'" ], ], ], ], ];
以下是一个真实世界的示例
// within config/security.php return [ 'content' => [ 'default' => 'global', 'profiles' => [ 'global' => [ 'base-uri' => "'self'", 'default-src' => "'self'", 'font-src' => [ "'self'", 'fonts.gstatic.com' ], 'img-src' => "'self'", 'script-src' => "'self'", 'style-src' => [ "'self'", "'unsafe-inline'", 'fonts.googleapis.com' ], ], 'flickr' => [ 'img-src' => [ 'https://*.staticflickr.com', ], ], ], ], ];
测试
$ ./vendor/bin/phpunit
贡献
有关详细信息,请参阅CONTRIBUTING。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。