hasanparasteh/helmet
Composer 安装命令:
composer require hasanparasteh/helmet
包简介
Helmet.js-style security headers middleware for ReactPHP HTTP.
README 文档
README
A fully modular, ReactPHP-native re-implementation of Helmet.js.
Each security feature is implemented as a separate middleware class, and HelmetMiddleware acts as
the aggregator—just like the real Helmet.
✨ Features
- CSP (Content Security Policy)
- Cross-Origin Policies (COOP / COEP / CORP)
- Strict-Transport-Security (HSTS)
- Referrer-Policy
- X-Frame-Options
- X-Content-Type-Options
- X-DNS-Prefetch-Control
- X-Download-Options
- X-Permitted-Cross-Domain-Policies
- X-Powered-By removal
- X-XSS-Protection (disabled by default, following Helmet.js)
- All middleware is async, non-blocking, and designed for ReactPHP HTTP servers
📦 Installation
composer require hasanparasteh/helmet
🚀 Usage with ReactPHP
use HP\Helmet\Middleware\Security\Helmet\HelmetMiddleware; use HP\Helmet\Http\MiddlewareDispatcher; use React\Http\HttpServer; use React\Http\Message\Response; $helmet = new HelmetMiddleware([ 'contentSecurityPolicy' => [ 'directives' => [ "default-src" => ["'self'"], "script-src" => ["'self'", "https://cdn.example.com"], ] ], 'referrerPolicy' => ['policy' => 'no-referrer'], 'xPoweredBy' => true ]); $dispatcher = new MiddlewareDispatcher( [$helmet], fn() => new Response(200, ['Content-Type' => 'text/plain'], "Hello secure world") ); $server = new HttpServer($dispatcher);
⚙️ Configuration Options (Full Documentation)
Configuration follows Helmet.js semantics as closely as possible.
1. contentSecurityPolicy
Enable or configure CSP.
Example
'contentSecurityPolicy' => [ 'directives' => [ "default-src" => ["'self'"], "script-src" => ["'self'", "cdn.example.com"], ], 'reportOnly' => false ]
Options
| Key | Type | Default | Description |
|---|---|---|---|
directives |
array<string,array | string | null> |
reportOnly |
bool | false | Sets Content-Security-Policy-Report-Only instead of enforcing |
Default CSP Directives
default-src 'self';
base-uri 'self';
font-src 'self' https: data:;
form-action 'self';
frame-ancestors 'self';
img-src 'self' data:;
object-src 'none';
script-src 'self';
script-src-attr 'none';
style-src 'self' https: 'unsafe-inline';
upgrade-insecure-requests;
2. crossOriginEmbedderPolicy
Controls resource isolation (COEP).
Example
'crossOriginEmbedderPolicy' => [ 'policy' => 'require-corp' ]
Options
| Key | Type | Default |
|---|---|---|
policy |
string | null |
Produces:
Cross-Origin-Embedder-Policy: require-corp
3. crossOriginOpenerPolicy
Isolation protection (COOP).
Example
'crossOriginOpenerPolicy' => [ 'policy' => 'same-origin' ]
Options
| Key | Type | Default |
|---|---|---|
policy |
string | null |
Produces:
Cross-Origin-Opener-Policy: same-origin
4. crossOriginResourcePolicy
Restrict which origins can load your resources (CORP).
Example
'crossOriginResourcePolicy' => [ 'policy' => 'same-origin' ]
Options
| Key | Type | Default |
|---|---|---|
policy |
string | null |
5. originAgentCluster
Enables browser origin-keyed agent clusters.
Example
'originAgentCluster' => true
Produces:
Origin-Agent-Cluster: ?1
6. referrerPolicy
Example
'referrerPolicy' => [ 'policy' => 'no-referrer' ]
Options
| Key | Type | Default |
|---|---|---|
policy |
string | null |
7. strictTransportSecurity / hsts
HSTS config.
Example:
'strictTransportSecurity' => [ 'maxAge' => 31536000, 'includeSubDomains' => true, 'preload' => false ]
Options:
| Key | Type | Default |
|---|---|---|
maxAge |
int | 15552000 (180 days) |
includeSubDomains |
bool | true |
preload |
bool | false |
Produces:
Strict-Transport-Security: max-age=15552000; includeSubDomains
Aliases:
hstsstrictTransportSecurity(Only one allowed—both → error)
8. xContentTypeOptions / noSniff
Control MIME type sniffing.
Examples:
'xContentTypeOptions' => true // or 'noSniff' => true
Output:
X-Content-Type-Options: nosniff
Alias rules:
- Only one of
xContentTypeOptionsornoSniffallowed.
9. xDnsPrefetchControl / dnsPrefetchControl
Example:
'dnsPrefetchControl' => ['allow' => false]
Options:
| Key | Type | Default |
|---|---|---|
allow |
bool | false |
Output:
X-DNS-Prefetch-Control: off
10. xDownloadOptions / ieNoOpen
Prevents file download attacks in IE.
Enable:
'xDownloadOptions' => true
Output:
X-Download-Options: noopen
11. xFrameOptions / frameguard
Example:
'xFrameOptions' => [ 'action' => 'DENY' ]
Options:
| Key | Type | Default |
|---|---|---|
action |
"DENY" |
"SAMEORIGIN" |
Output:
X-Frame-Options: SAMEORIGIN
12. xPermittedCrossDomainPolicies
Example:
'xPermittedCrossDomainPolicies' => [ 'policy' => 'none' ]
Options:
| Key | Type | Default |
|---|---|---|
policy |
string | "none" |
Output:
X-Permitted-Cross-Domain-Policies: none
13. xPoweredBy / hidePoweredBy
True = remove “X-Powered-By”.
Example:
'xPoweredBy' => true
Removes:
X-Powered-By: PHP/8.x
If you disable:
'xPoweredBy' => false
It will NOT remove the header.
14. xXssProtection / xssFilter
Modern Helmet disables this (it's deprecated/broken in browsers).
Example:
'xXssProtection' => true
Always outputs:
X-XSS-Protection: 0
Alias rules same as Helmet.js.
🧩 Full Option Map
| Helmet.js Option | HP Helmet Option | Default |
|---|---|---|
| contentSecurityPolicy | contentSecurityPolicy | enabled |
| crossOriginOpenerPolicy | crossOriginOpenerPolicy | enabled |
| crossOriginEmbedderPolicy | crossOriginEmbedderPolicy | disabled |
| crossOriginResourcePolicy | crossOriginResourcePolicy | enabled |
| originAgentCluster | originAgentCluster | enabled |
| referrerPolicy | referrerPolicy | enabled |
| strictTransportSecurity / hsts | strictTransportSecurity | enabled |
| noSniff | xContentTypeOptions | enabled |
| dnsPrefetchControl | xDnsPrefetchControl | enabled |
| ieNoOpen | xDownloadOptions | enabled |
| frameguard | xFrameOptions | enabled |
| permittedCrossDomainPolicies | xPermittedCrossDomainPolicies | enabled |
| hidePoweredBy | xPoweredBy | enabled |
| xssFilter | xXssProtection | enabled (sets to 0) |
🧱 Architecture Overview
HelmetMiddleware
↳ ContentSecurityPolicyMiddleware
↳ CrossOriginOpenerPolicyMiddleware
↳ CrossOriginEmbedderPolicyMiddleware
↳ CrossOriginResourcePolicyMiddleware
↳ OriginAgentClusterMiddleware
↳ ReferrerPolicyMiddleware
↳ StrictTransportSecurityMiddleware
↳ XContentTypeOptionsMiddleware
↳ XDnsPrefetchControlMiddleware
↳ XDownloadOptionsMiddleware
↳ XFrameOptionsMiddleware
↳ XPermittedCrossDomainPoliciesMiddleware
↳ XPoweredByMiddleware
↳ XXssProtectionMiddleware
Each sub-middleware:
- Accepts
(ServerRequestInterface $req, callable $next) - Returns
Promise<ResponseInterface> - Mutates headers only in the response
hasanparasteh/helmet 适用场景与选型建议
hasanparasteh/helmet 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「middleware」 「headers」 「reactphp」 「csp」 「hsts」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hasanparasteh/helmet 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hasanparasteh/helmet 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hasanparasteh/helmet 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Permissions-Policy (Feature-Policy) header builder and middleware for Laravel
Provide a way to secure accesses to all routes of an symfony application.
PHP polyfill for the Apache Functions. The aim is to provides functions as the PHP way in order to get them available even if PHP is not running as an Apache module.
Helper bundle, configure the request headers from a given base URL.
It's a barebone security class written on PHP
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-07