nieuwenhuizen/content-security-policy
Composer 安装命令:
composer require nieuwenhuizen/content-security-policy
包简介
Configure your content-security-policy header easily with yaml
README 文档
README
Nieuwenhuizen.ContentSecurityPolicy
Flow/Neos package to set your site's content security policy header easily with yaml
Usage
Import the package using composer:
composer require nieuwenhuizen/content-security-policy
The package is automatically active once imported.
By default the response header Content-Security-Policy will now be included.
It will use the default configuration which looks like this:
Nieuwenhuizen: ContentSecurityPolicy: enabled: true report-only: false content-security-policy: default: base-uri: - 'self' connect-src: - 'self' default-src: - 'self' form-action: - 'self' img-src: - 'self' media-src: - 'self' frame-src: - 'self' object-src: - 'self' script-src: - 'self' style-src: - 'self' font-src: - 'self' custom: []
Now only resources from the same origin are allowed for the most common directives. It is enabled by default and the report-only mode is disabled.
Custom directives and values
The default configuration will probably not suit your needs so you can add your own configuration by adding the array custom like this in your own yaml configuration files:
Nieuwenhuizen: ContentSecurityPolicy: content-security-policy: custom: frame-src: - 'https://www.youtube.com' - 'https://staticxx.facebook.com'
If you fully want to override the entire default config then just override the default key in yaml.
Disable or report only
To disable the header simply set enabled to false.
If you want to add it as a report only header set report-only to true.
That way you have the option to see the possible errors without breaking functionality.
Nonce
You might want to use a nonce to allow inline scripts and styles to be still secure.
To do this simply add {nonce} as an option in a directive. Like this:
Nieuwenhuizen: ContentSecurityPolicy: content-security-policy: custom: script-src: - '{nonce}'
Now the header will include a nonce-automatedgeneratedrandomstring in the script-src directive.
So inline scripts without the corresponding nonce will be blocked.
To add the nonce string in your templates use the supplied ViewHelper like this:
{namespace csp=Nieuwenhuizen\ContentSecurityPolicy\ViewHelpers}
<script nonce="{csp:nonce()}">
alert('Hello world');
</script>
Backend
Do to the current nature of the Neos backend being rendered a bit different then the frontend a separate policy is added for the backend. I currently have found no suitable way the add the nonce in the inline scripts in the Neos UI package. So the CSP for the backend looks like this:
backend: base-uri: - 'self' connect-src: - 'self' default-src: - 'self' form-action: - 'self' img-src: - 'self' media-src: - 'self' frame-src: - 'self' object-src: - 'self' script-src: - 'self' - 'unsafe-inline' style-src: - 'self' - 'unsafe-inline' font-src: - 'self' custom-backend: []
Unsafe inline scripts and styles are allowed in the backend because otherwise the backend won't work. I will try to find a nice solution for this as soon as possible.
Again you can add your own policies in the custom-backend array the same way as the custom array for the frontend.
nieuwenhuizen/content-security-policy 适用场景与选型建议
nieuwenhuizen/content-security-policy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.43k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nieuwenhuizen/content-security-policy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nieuwenhuizen/content-security-policy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-08