martijnc/php-csp
最新稳定版本:1.0.2
Composer 安装命令:
composer require martijnc/php-csp
包简介
Content Security Policy helper for PHP
README 文档
README
A PHP helper class to dynamically construct Content Security Policy headers as defined by the W3C in the Content Security Policy specification (http://www.w3.org/TR/CSP2/).
How to use
Add php-csp to your project and create an instance of the ContentSecurityPolicyHeaderBuilder class and use it to setup your CSP policy. Once your policy is complete, use ContentSecurityPolicyHeaderBuilder::getHeaders() to get your CSP header.
use Phpcsp\Security\ContentSecurityPolicyHeaderBuilder; $policy = new ContentSecurityPolicyHeaderBuilder(); // Set the script-src directive to 'none' $policy->addSourceExpression(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_SCRIPT_SRC, 'none'); // Enable the browsers xss blocking features $policy->setReflectedXssPolicy(ContentSecurityPolicyHeaderBuilder::REFLECTED_XSS_BLOCK); // Set a report URL $policy->setReportUri('https://example.com/csp/report.php'); // Get your CSP headers $headers = $policy->getHeaders(false);
ContentSecurityPolicy::getHeaders() returns an array of HTTP headers you should send. For the example above this results in the following array:
array (size=1) 0 => array (size=2) 'name' => string 'Content-Security-Policy' (length=23) 'value' => string 'script-src 'none'; reflected-xss block; report-uri https://example.com/csp/report.php;' (length=86) Source expressions
The most straightforward use of this class is by adding origins to the directives of your choice like in this example:
use Phpcsp\Security\ContentSecurityPolicyHeaderBuilder; $policy = new ContentSecurityPolicyHeaderBuilder(); // Set the default-src directive to 'none' $policy->addSourceExpression(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_DEFAULT_SRC, 'none'); // Add a single origin for the script-src directive $policy->addSourceExpression(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_SCRIPT_SRC, 'https://example.com/scripts/'); // Add a single origin for the style-src directive $policy->addSourceExpression(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_STYLE_SRC, 'https://example.com/style/'); foreach ($policy->getHeaders(true) as $header) { header(sprintf('%s: %s', $header['name'], $header['value'])); }
This example would output the following headers:
array (size=1) 0 => array (size=2) 'name' => string 'Content-Security-Policy' (length=23) 'value' => string 'default-src 'none'; script-src https://example.com/scripts/; style-src https://example.com/style/;' (length=98) Source sets
You can define source-sets and link them to any CSP directive you want. For example
use Phpcsp\Security\ContentSecurityPolicyHeaderBuilder; $policy = new ContentSecurityPolicyHeaderBuilder(); // Set the default-src directive to 'none' $policy->addSourceExpression(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_DEFAULT_SRC, 'none'); // Define two source sets $policy->defineSourceSet('my-scripts-cdn', [ 'https://cdn-scripts1.example.com/scripts/', 'https://cdn-scripts2.example.com/scripts/' ]); $policy->defineSourceSet('my-style-cdn', [ 'https://cdn-style1.example.com/css/', 'https://cdn-style2.example.com/css/' ]); // Add both to a directive $policy->addSourceSet(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_SCRIPT_SRC, 'my-scripts-cdn'); $policy->addSourceSet(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_STYLE_SRC, 'my-style-cdn'); $headers = $policy->getHeaders(false);
Would result in the following headers:
array (size=1) 0 => array (size=2) 'name' => string 'Content-Security-Policy' (length=23) 'value' => string 'default-src 'none'; script-src https://cdn-scripts1.example.com/scripts/ https://cdn-scripts2.example.com/scripts/; style-src https://cdn-style1.example.com/css/ https://cdn-style2.example.com/css/;' (length=198) Nonces
CSP blocks inline scripts but they can be enabled again by adding unsafe-inline to the script-src directive. Doing this would defeat the entire purpose of using CSP and therefor, the use of unsafe-inline is not recommended. If you need to inline scripts for whatever reason, you should use nonces. A nonce is a random string you add to the script-src directive and the inline script tags you allow on your webpage like in the following example:
use Phpcsp\Security\ContentSecurityPolicyHeaderBuilder; $policy = new ContentSecurityPolicyHeaderBuilder(); // Set the default-src directive to 'none' $policy->addSourceExpression(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_DEFAULT_SRC, 'none'); $myScriptNonce = 'thisShouldBeRandom'; // Add the nonce to the script-src directive $policy->addNonce(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_SCRIPT_SRC, $myScriptNonce); foreach ($policy->getHeaders(true) as $header) { header(sprintf('%s: %s', $header['name'], $header['value'])); }
Would result in the following headers:
array (size=1) 0 => array (size=2) 'name' => string 'Content-Security-Policy' (length=23) 'value' => string 'default-src 'none'; script-src 'nonce-thisShouldBeRandom';' (length=58) Your HTML should look like this:
<script> // This would be blocked because it has no nonce </script> <script nonce="thisIsAWrongNonce"> // This would be blocked because the nonce is invalid </script> <script nonce="thisShouldBeRandom"> // This would work fine </script> The nonce should be random for each request so attackers cannot predict the nonce value.
Hashes
If your application requires inline scripts you can serve the SHA256, SHA384, or SHA512 hash of the source as part of the script-src directive in your policy to allow the script to run. This way you don't need to enable unsafe-inline.
Violation reports
CSP gives you the option to receive reports about CSP violations. Each time a page loads a resource that is blocked by your CSP policy, the browser will submit a JSON object to the URL you specified in your policy. In the following example, those report will be send to https://example.com/csp/report.php:
use Phpcsp\Security\ContentSecurityPolicyHeaderBuilder; $policy = new ContentSecurityPolicyHeaderBuilder(); // Set the script-src directive to 'none' $policy->addSourceExpression(ContentSecurityPolicyHeaderBuilder::DIRECTIVE_SCRIPT_SRC, 'https://example.com/scripts/'); // Set a report URL $policy->setReportUri('https://example.com/csp/report.php');
You can also use CSP in a report-only mode. This mode is ideal if you are implenting CSP on an existing website without breaking things. Each time a resource load violates your CSP policy, the browser will send a violation report but it won't actually block the resource.
// Set a report URL $policy->setReportUri('https://example.com/csp/report.php'); // Use report only mode $policy->enforcePolicy(false);
Legacy header support
This class also provides support for some legacy headers which are being replaced by CSP. Currently it has support for the X-XSS-Protection and X-Frame-Options headers.
use Phpcsp\Security\ContentSecurityPolicyHeaderBuilder; $policy = new ContentSecurityPolicyHeaderBuilder(); // Enable the browsers xss blocking features $policy->setReflectedXssPolicy(ContentSecurityPolicyHeaderBuilder::REFLECTED_XSS_BLOCK); // Set the 'X-Frame-Options' header $policy->setFrameOptions(ContentSecurityPolicyHeaderBuilder::FRAME_OPTION_SAME_ORIGIN); // Get your CSP headers, including legacy headers $headers = $policy->getHeaders(true); foreach ($headers as $header) { header(sprintf('%s: %s', $header['name'], $header['value'])); }
This would result in the following headers:
array (size=3) 0 => array (size=2) 'name' => string 'Content-Security-Policy' (length=23) 'value' => string 'script-src 'none'; reflected-xss block; report-uri https://example.com/csp/report.php;' (length=86) 1 => array (size=2) 'name' => string 'X-XSS-Protection' (length=16) 'value' => string '1; mode=block' (length=13) 2 => array (size=2) 'name' => string 'X-Frame-Options' (length=15) 'value' => string 'SAMEORIGIN' (length=10) martijnc/php-csp 适用场景与选型建议
martijnc/php-csp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75.85k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「security」 「library」 「xss」 「content security policy」 「X-Frame-Options」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 martijnc/php-csp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 martijnc/php-csp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 martijnc/php-csp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
It's a barebone security class written on PHP
Inbox pattern process implementation for your Laravel Applications
Contao CMS integrity check for some files
Core library that defines common interfaces used by the rest of the intahwebz..
A PHP security linter to detect insecure functions like var_dump, print_r, and other dangerous functions in your codebase
统计信息
- 总下载量: 75.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 36
- 依赖项目数: 2
- 推荐数: 4
其他信息
- 授权协议: BSD
- 更新时间: 2026-01-04