bicf/yii2-security-headers
Composer 安装命令:
composer require bicf/yii2-security-headers
包简介
Security oriented headers management
关键字:
README 文档
README
Introduction
Yii2 implementation of CSP - Content Security Policy
See also MDN docs
Installation
Installation is recommended to be done via composer by running:
composer require bicf/yii2-security-headers "*"
Alternatively you can add the following to the require section in your composer.json manually:
{
"bicf/yii2-security-headers": "*"
}
Run composer update afterwards.
Then proceed to configuration.
Configuration
The version 1.1 is a Module implementation.
Module securityHeader sample configuration in main.php
[
'bootstrap'=>[
'securityHeader',
],
'modules' => [
'securityHeader' => [
'class' => bicf\securityheaders\Module::class,
'modules' => [
'XContentTypeOptions'=>[
'class' => 'bicf\securityheaders\modules\HeaderXContentTypeOptions',
'value' => 'nosniff',
],
'XFrameOptions'=>[
'class' => 'bicf\securityheaders\modules\HeaderXFrameOptions',
'value' => 'SAMEORIGIN',
],
'AccessControlAllowMethods'=>[
'class' => 'bicf\securityheaders\modules\HeaderAccessControlAllowMethods',
'value' => 'GET',
],
'AccessControlAllowOrigin'=>[
'class' => 'bicf\securityheaders\modules\HeaderAccessControlAllowOrigin',
'value' => 'https://api.example.com',
],
'ContentSecurityPolicyAcl'=>[
'class' => 'bicf\securityheaders\modules\HeaderContentSecurityPolicyAcl',
'enabled' => false,
'policies' => [
'default-src' => "'self'",
'frame-src' => "'self' www.facebook.com www.youtube.com www.google.com",
'img-src' => "'self' www.google-analytics.com",
'font-src' => "'self' fonts.gstatic.com maxcdn.bootstrapcdn.com",
'media-src' => "'self'",
'script-src' => "'self' www.google-analytics.com",
'style-src' => "'self' maxcdn.bootstrapcdn.com",
'connect-src' => "'self'",
'report-uri' => "/report-csp-acl",
],
],
'ContentSecurityPolicyMonitor'=>[
'class' => 'bicf\securityheaders\modules\HeaderContentSecurityPolicyMonitor',
'policies' => [
'default-src' => "'self'",
'frame-src' => "'self' www.facebook.com www.youtube.com www.google.com",
'img-src' => "'self' www.google-analytics.com",
'font-src' => "'self' fonts.gstatic.com maxcdn.bootstrapcdn.com",
'media-src' => "'self'",
'script-src' => "'self' www.google-analytics.com",
'style-src' => "'self' maxcdn.bootstrapcdn.com",
'connect-src' => "'self'",
'report-uri' => "/report-csp-acl",
],
],
],
],
],
'components' => [
// components stuff
// no need to add anything
],
]
Yii2 integration of Content Security Policy Header
Possible integrations
CSP can work by signature or by the nonce token see:
Integration by signature
Done simply adding the signatures to CSP configuration
Example:
'style-src' => "'sha256-aqNNdDLnnrDOnTNdkJpYlAxKVJtLt9CtFLklmInuUAE=' 'sha256-6fwFCXmgb6H4XQGajtDSVG3YuKmX3dT1NkX4+z510Og=' 'sha256-ZdHxw9eWtnxUb3mk6tBS+gIiVUPE3pGM470keHPDFlE='",
This kind of integration does not require patch to the framework code but it's space wasting and hard to mantain even with a small number of signatures.
Integration by nonce token
This kind of integration require some (small) patch at framework (\yii\helpers\BaseHtml) level to take full advantage of nonce token. The nonce feature (enabled by default) don't need maintenace once integrated and has reduced footprint on the header
Here follow the patched versions of BaseHtml functions to support the nonce parameter in a transparent way.
Patch to Html::script helper
The patched \yii\helpers\BaseHtml::script() :
public static function script($content, $options = []) { if(Yii::$app->response instanceof SecureRequestInterface){ $behavior = Yii::$app->response->getBehavior(SecureRequestInterface::CSP_NONCE_BEHAVIOR); if($behavior != null){ $options = array_merge(Yii::$app->response->getContentSecurityPolicyTokenArray(),$options ); } } return static::tag('script', $content, $options); }
Tag script required by the project Assets
The patched \yii\helpers\BaseHtml::jsFile() :
public static function jsFile($url, $options = []) { $options['src'] = Url::to($url); if (isset($options['condition'])) { $condition = $options['condition']; unset($options['condition']); return self::wrapIntoCondition(static::tag('script', '', $options), $condition); } if(Yii::$app->response instanceof SecureRequestInterface){ $behavior = Yii::$app->response->getBehavior('cspBehavior'); if($behavior != null){ $options = array_merge(Yii::$app->response->getContentSecurityPolicyTokenArray(),$options ); } } return static::tag('script', '', $options); }
or (better?) call script funtion inside jsFile function:
public static function jsFile($url, $options = []) { $options['src'] = Url::to($url); if (isset($options['condition'])) { $condition = $options['condition']; unset($options['condition']); return self::wrapIntoCondition(static::tag('script', '', $options), $condition); } return static::script('', $options); }
A different approach for <script> the views
Tag script inside the views
When the <script> is explicit used in view or controllers the solution is to add the nonce parameter directly in the tag by:
Yii::$app->response->getContentSecurityPolicyTokenAttribute()
Inside a view
<script <?= Yii::$app->response->getContentSecurityPolicyTokenAttribute();?> > alert("test"); </script>
A patched \yii\debug\Module::renderToolbar function
/** * Renders mini-toolbar at the end of page body. * * @param \yii\base\Event $event */ public function renderToolbar($event) { if (!$this->checkAccess() || Yii::$app->getRequest()->getIsAjax()) { return; } /* @var $view View */ $view = $event->sender; echo $view->renderDynamic('return Yii::$app->getModule("' . $this->id . '")->getToolbarHtml();'); // echo is used in order to support cases where asset manager is not available echo '<style>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.css') . '</style>'; echo '<script '.Yii::$app->response->getContentSecurityPolicyTokenAttribute().'>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.js') . '</script>'; }
In detail the line:
echo '<script '.Yii::$app->response->getContentSecurityPolicyTokenAttribute().'>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.js') . '</script>';
Runtime disabilitation
Since no header is sent until the render call it's possible to disable one or more modules as needed.
public function actionIndex() { Yii::$app->getResponse()->modules['sample-module']->enabled=false; return $this->render("index"); }
Legacy Implementation
This is the old implementation, extending the Request Class.
IMPORTANT: If you don't setup your configuration no header will be sent.
An example of configuration:
[
'components' => [
'response' => [
'class' => 'bicf\securityheaders\components\Response',
'on afterPrepare' => ['bicf\securityheaders\components\Response','addSecurityHeaders'],
'modules' => [
'XContentTypeOptions'=>[
'class' => 'bicf\securityheaders\modules\HeaderXContentTypeOptions',
'value' => 'nosniff',
],
'AccessControlAllowMethods'=>[
'class' => 'bicf\securityheaders\modules\HeaderAccessControlAllowMethods2',
'value' => 'GET',
],
'AccessControlAllowOrigin'=>[
'class' => 'bicf\securityheaders\modules\HeaderAccessControlAllowOrigin',
'value' => 'https://api.example.com',
],
'ContentSecurityPolicyAcl'=>[
'class' => 'bicf\securityheaders\modules\HeaderContentSecurityPolicyAcl',
'enabled' => false,
'policies' => [
'default-src' => "'self'",
'frame-src' => "'self' www.facebook.com www.youtube.com www.google.com",
'img-src' => "'self' www.google-analytics.com",
'font-src' => "'self' fonts.gstatic.com maxcdn.bootstrapcdn.com",
'media-src' => "'self'",
'script-src' => "'self' www.google-analytics.com",
'style-src' => "'self' maxcdn.bootstrapcdn.com",
'connect-src' => "'self'",
'report-uri' => "/report-csp-acl",
],
],
'ContentSecurityPolicyMonitor'=>[
'class' => 'bicf\securityheaders\modules\HeaderContentSecurityPolicyMonitor',
'policies' => [
'default-src' => "'self'",
'frame-src' => "'self' www.facebook.com www.youtube.com www.google.com",
'img-src' => "'self' www.google-analytics.com",
'font-src' => "'self' fonts.gstatic.com maxcdn.bootstrapcdn.com",
'media-src' => "'self'",
'script-src' => "'self' www.google-analytics.com",
'style-src' => "'self' maxcdn.bootstrapcdn.com",
'connect-src' => "'self'",
'report-uri' => "/report-csp-acl",
],
],
],
],
],
]
bicf/yii2-security-headers 适用场景与选型建议
bicf/yii2-security-headers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.88k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bicf/yii2-security-headers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bicf/yii2-security-headers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bicf/yii2-security-headers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
SCEditor, a lightweight WYSIWYG BBCode & HTML editor extension for Yii 2.0 Framework
Redirect all not authenticated web user to login page.
React javascript library for Yii2
Yii 2 RBAC Module
Circular navigation widget for Yii 2 Framework.
统计信息
- 总下载量: 6.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-only
- 更新时间: 2018-09-01