decodelabs/sanctum
Composer 安装命令:
composer require decodelabs/sanctum
包简介
Define and deploy Content Security Policies in your PHP application
README 文档
README
Content Security Policies for your PHP application.
Sanctum allows you to create and deploy Content Security Policies with ease. Take the guesswork out of this important security feature.
Installation
This package requires PHP 8.4 or higher.
Install via Composer:
composer require decodelabs/sanctum
Usage
Create your definition:
use DecodeLabs\Sanctum\Definition; class MyCsp extends Definition { // These items can be reused in other directives const SharedSrc = [ '@self', // Resolves to 'self' '*.myotherdomain.com' ]; // These items create the default-src directive const DefaultSrc = [ '@shared-src', // Import items from SharedSrc ]; // These define script sources const ScriptSrc = [ '@nonce', // Creates a unique nonce to be used in markup '@unsafe-inline', // Resolves to 'unsafe-inline' '@strict-dynamic', '@https', '@http' ]; // These define image sources const ImgSrc = [ '@shared', // Import items from SharedSrc '@data', // Resolves to data: for data URLs '*.myimagecdn.net', '!*.myotherdomain.com' // Exclude importing from SharedSrc ]; // Report endpoint const ReportUri = 'https://mydomain.com/report'; }
Please see https://content-security-policy.com/ for a full list of directives.
Then in your HTTP handler:
$csp = new MyCsp(); foreach($csp->exportHeaders() as $header => $value) { $response->setHeader($header, $value); } /* Reporting-Endpoints => sanctum-csp-report="https://mydomain.com/report" Content-Security-Policy => default-src 'self' *.myotherdomain.com; script-src nonce-98b88fa48f23911d6fc1f5092efb2e36d76423ce4f5d7ef42765a2c2501d57c9' 'unsafe-inline' 'strict-dynamic' https: http:; img-src 'self' data: *.myimagecdn.net; report-uri https://mydomain.com/report; report-to sanctum-csp-report */
Hashes
Make use of the hash feature for scripts - see https://content-security-policy.com/hash/ for explanation
/* HTML: <script>doSomething();</script> */ $script = 'doSomething();'; // Your JS // Adds sha256-xxx hash to CSP directive $hash = $csp->hashContent($script, 'script-src');
Archetype loader
Sanctum also provides an optional Archetype loader:
namespace DecodeLabs\Sanctum\Definition; use DecodeLabs\Sanctum\Definition; class MyCsp extends Definition {} $csp = Definition::load('MyCsp'); $csp->exportHeaders();
Archetype will look for implementations in the root namespace (DecodeLabs\Sanctum\Definition) by default. If you want to host your implementations in a different namespace, you should create and register a new Archetype resolver to find them.
Licensing
Sanctum is licensed under the MIT License. See LICENSE for the full license text.
decodelabs/sanctum 适用场景与选型建议
decodelabs/sanctum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.42k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「Policy」 「csp」 「content security policy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 decodelabs/sanctum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 decodelabs/sanctum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 decodelabs/sanctum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Setup CSP Headers for a website
Middleware to add the Content-Security-Policy header to the response
Simple and configurable SilverStripe module to notify users about cookie policies
SilverStripe Content Security Policy module
A module for CSP headers in Silverstripe.
统计信息
- 总下载量: 1.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-26