codebar-ag / laravel-feature-policy
将 Feature-Policy 头部添加到 Laravel 应用的响应中
Requires
- php: ^8.1|^8.2
- illuminate/http: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
- laravel/pint: ^1.6
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: 2.x-dev
- pestphp/pest-plugin-laravel: 2.x-dev
- phpunit/phpunit: ^9.0|^10.0
README
此包深受 Spaties laravel-csp 包的启发。感谢 Freek van der Herten 和 Thomas Verhelst 创建如此出色的包并承担所有繁重的工作!
使用 Permissions-Policy,您可以控制允许和拒绝哪些网络平台权限在您的网络应用中。Permissions-Policy 是一个新的安全头(类似于 Content-Security-Policy)。您可以限制的事项列表尚未最终确定,我将在规范演变时添加它们。
安装
您应该通过 composer 安装此包
$ composer require codebar-ag/laravel-feature-policy
接下来,发布配置文件
$ php artisan vendor:publish --provider="CodebarAg\FeaturePolicy\FeaturePolicyServiceProvider" --tag="config"
config/feature-policy.php
文件的 内容如下
<?php return [ /* * A policy will determine which Permissions-Policy headers will be set. * A valid policy extends `CodebarAg\FeaturePolicy\Policies\Policy` */ 'policy' => CodebarAg\FeaturePolicy\Policies\Basic::class, /* * Feature-policy headers will only be added if this is set to true */ 'enabled' => env('FPH_ENABLED', true), ];
中间件
您可以通过在 HTTP 核心中注册 CodebarAg\FeaturePolicy\AddFeaturePolicyHeaders::class
将 Feature-Policy 头部添加到所有响应中
// app/Http/Kernel.php ... protected $middlewareGroups = [ 'web' => [ ... \CodebarAg\FeaturePolicy\AddFeaturePolicyHeaders::class, ] ];
或者您可以将中间件添加到单个路由和路由组中
// in a routes file Route::get('/home', 'HomeController')->middleware(CodebarAg\FeaturePolicy\AddFeaturePolicyHeaders::class);
您甚至可以传递一个策略作为参数,并覆盖配置文件中指定的策略
// in a routes file Route::get('/home', 'HomeController')->middleware(CodebarAg\FeaturePolicy\AddFeaturePolicyHeaders::class . ':' . MyFeaturePolicy::class);
使用方法
此包允许您定义 Permissions-Policy 策略。Feature-Policy 策略确定将在响应头部中设置哪些 Permissions-Policy 指令。
Permissions-Policy 指令的一个例子是 microphone
Permissions-Policy: microphone=(self "https://spatie.be")
在上面的例子中,通过指定 microphone
并允许它为 self
,使得除我们自己的和 https://spatie.be 之外的所有来源的权限都被禁用。
指令的完整列表尚未最终确定,但以下是一些您可访问的事项
- accelerometer
- ambient-light-sensor
- autoplay
- camera
- encrypted-media
- fullscreen
- geolocation
- gyroscope
- magnetometer
- microphone
- midi
- payment
- picture-in-picture
- speaker
- usb
- vr
您可以在 https://github.com/WICG/feature-policy/blob/master/features.md 找到功能定义
您可以将多个策略选项作为数组或作为单个由空格分隔的字符串添加
// in a policy ... ->addDirective(Directive::CAMERA, [ Value::SELF, 'spatie.be', ]) ->addDirective(Directive::GYROSCOPE, 'self spatie.be') ...
创建策略
feature-policy
配置文件的 policy
键默认设置为 CodebarAg\FeaturePolicy\Policies\Basic::class
,它允许您的站点使用一些可用的功能。该类看起来如下
<?php namespace CodebarAg\FeaturePolicy\Policies; use CodebarAg\FeaturePolicy\Value; use CodebarAg\FeaturePolicy\Directive; class Basic extends Policy { public function configure() { $this->addDirective(Directive::GEOLOCATION, Value::SELF) ->addDirective(Directive::FULLSCREEN, Value::SELF); } }
假设您对允许 geolocation
和 fullscreen
满意,但还想添加 www.awesomesite.com
以获得对这些功能的访问权限,那么您可以轻松地扩展该类
<?php namespace App\Services\FeaturePolicy\Policies; use CodebarAg\FeaturePolicy\Directive; use CodebarAg\FeaturePolicy\Policies\Basic; class MyFeaturePolicy extends Basic { public function configure() { parent::configure(); $this->addDirective(Directive::GEOLOCATION, 'www.awesomesite.com') ->addDirective(Directive::FULLSCREEN, 'www.awesomesite.com'); } }
别忘了将 feature-policy
配置文件中的 policy
键更改为你的策略类名(例如 App\Services\Policies\MyFeaturePolicy
)。
测试
你可以使用以下命令运行所有测试:
$ composer test
变更日志
请参阅 CHANGELOG 了解最近发生了哪些变化。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 helpdesk@codebar.ch 联系我们,而不是使用问题跟踪器。
鸣谢
支持
如果您喜欢这个包,请随意给它加星。
许可
MIT 许可证(MIT)。请参阅 LICENSE 了解更多信息。