mulertech/csp-bundle
Composer 安装命令:
composer require mulertech/csp-bundle
包简介
Symfony bundle for Content Security Policy (CSP) header management with nonce support
README 文档
README
Symfony bundle for Content Security Policy (CSP) header management with named nonce support.
Installation
composer require mulertech/csp-bundle
Configuration
The bundle ships with secure defaults for all directives. You only need to override what differs from the defaults.
Minimal config/packages/mulertech_csp.yaml:
mulertech_csp: directives: script-src: - "'self'" - "nonce(main)" style-src: - "'self'" - "'unsafe-inline'"
Full reference
Here is the complete list of available options with their default values:
mulertech_csp: enabled: true # true by default report_only: false # false by default always_add: [] # Origins added to ALL directives report: url: ~ # External URL for report-uri/report-to route: ~ # Symfony route name (alternative to url) route_params: [] # Route parameters chance: 100 # 0-100, % of requests with reporting directives: # Only override what you need default-src: - "'self'" script-src: - "'self'" - "nonce(main)" style-src: - "'self'" - "'unsafe-inline'" img-src: - "'self'" - "data:" font-src: - "'self'" connect-src: - "'self'" media-src: - "'self'" object-src: - "'none'" frame-src: - "'none'" frame-ancestors: - "'none'" base-uri: - "'self'" form-action: - "'self'" upgrade-insecure-requests: true
Default directives
| Directive | Default |
|---|---|
default-src |
'self' |
script-src |
'self' + nonce(main) |
style-src |
'self' 'unsafe-inline' |
img-src |
'self' data: |
font-src |
'self' |
connect-src |
'self' |
media-src |
'self' |
object-src |
'none' |
frame-src |
'none' |
frame-ancestors |
'none' |
base-uri |
'self' |
form-action |
'self' |
upgrade-insecure-requests |
true |
Named nonces
Use nonce(handle) syntax in directives to create named nonces:
mulertech_csp: directives: script-src: - "'self'" - "nonce(main)" # For your main scripts - "nonce(analytics)" # For analytics scripts
Each named nonce generates a unique 256-bit (32 bytes) cryptographically secure value.
always_add
Add origins to all directives automatically (except those set to 'none'):
mulertech_csp: always_add: - "https://cdn.example.com" directives: default-src: - "'self'" object-src: - "'none'" # always_add is NOT merged here
Violation reporting
Report CSP violations to an external endpoint:
mulertech_csp: report: url: "https://report.example.com/csp" chance: 50 # Only 50% of requests
Or use a Symfony route:
mulertech_csp: report: route: "app_csp_report" route_params: {}
Report-only mode
Test your CSP policy without enforcing it:
mulertech_csp: report_only: true
This sets the Content-Security-Policy-Report-Only header instead of Content-Security-Policy.
Usage
In Twig templates
Use the csp_nonce('handle') function with a named handle:
<script nonce="{{ csp_nonce('main') }}"> // Your inline JavaScript </script> <script nonce="{{ csp_nonce('analytics') }}"> // Analytics script </script>
Dynamic CSP customization
Listen to the BuildCspHeaderEvent to customize CSP per-request:
use MulerTech\CspBundle\Event\BuildCspHeaderEvent; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; #[AsEventListener(event: BuildCspHeaderEvent::NAME)] class CspListener { public function __invoke(BuildCspHeaderEvent $event): void { if ($event->getRequest()->getPathInfo() === '/admin') { $event->setHeaderValue("default-src 'self'; script-src 'self'"); } } }
Inject the nonce generator
use MulerTech\CspBundle\CspNonceGenerator; class MyService { public function __construct( private readonly CspNonceGenerator $nonceGenerator, ) {} public function getMainNonce(): string { return $this->nonceGenerator->getNonce('main'); } }
Upgrading from v1.x
Breaking changes
- Directives format: Changed from scalar strings to arrays of sources
# v1.x mulertech_csp: directives: script-src: "'self' 'nonce-{nonce}'" # v2.0 mulertech_csp: directives: script-src: - "'self'" - "nonce(main)"
- Twig function:
csp_nonce()now requires a handle argument
{# v1.x #} <script nonce="{{ csp_nonce() }}"> {# v2.0 #} <script nonce="{{ csp_nonce('main') }}">
- Nonce placeholder:
{nonce}replaced bynonce(handle)syntax
Requirements
- PHP >= 8.2
- Symfony 6.4 or 7.x
- Twig (optional, for the
csp_nonce()function) - symfony/routing (optional, for route-based reporting)
License
MIT
mulertech/csp-bundle 适用场景与选型建议
mulertech/csp-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 235 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「security」 「nonce」 「csp」 「content-security-policy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mulertech/csp-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mulertech/csp-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mulertech/csp-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
It's a barebone security class written on PHP
Bundle Symfony DaplosBundle
Contao CMS integrity check for some files
A PHP security linter to detect insecure functions like var_dump, print_r, and other dangerous functions in your codebase
统计信息
- 总下载量: 235
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 42
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-02