定制 silverstripeltd/silverstripe-csp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

silverstripeltd/silverstripe-csp

Composer 安装命令:

composer require silverstripeltd/silverstripe-csp

包简介

A module for CSP headers in Silverstripe.

README 文档

README

Make your site like super secure with CSP headers and SRI tags on your scripts.

This module is not currently supported, it will frequently change and break APIs.

Requirements

  • SilverStripe ^5 (4.11+ available in previous tags)

Installation

First you install it, then you configure it. Like lego!

composer require silverstripeltd/silverstripe-csp

Documentation

First you're going to want to create a Policy you'll likely want to extend the Basic policy as a good starting ground.

This will look something like:

class ContentSecurityPolicy extends Basic
{
    public function configure(): void
    {
        parent::configure();
    }
}

You can then start adding in fragments. These are small pieces of code that resemble a CSP for a service. For example YouTube's is:

class YouTube implements Fragment
{
    public static function addTo(Policy $policy): void
    {
        $policy
            ->addDirective(Directive::SCRIPT, [
                'www.youtube.com',
                's.ytimg.com',
            ])
            ->addDirective(Directive::FRAME, "*.youtube.com");
    }
}

If you need to add a new fragment or update an existing one please make a pull request to the repo. You've either found a potential issue for all our projects using this, or you're adding a new service which other projects can benefit from

Adding fragments to your policy looks like this (in configure func from above):

public function configure(): void
{
    parent::configure();
    $this->addFragments([YouTube::class]);
}

Usually you'll define private const FRAGMENTS = [] and add them in there so it's clear at the beginning what fragments you're adding.

To set the report to url, we usually use an env var named CSP_REPORT_TO. The expiry time can also be set using CSP_REPORT_TO_TTL this tells the browser how long it should remember the url for.

You can also call $this->reportTo() in your policies configure func if required (perhaps you want the report URI based on the policy applied).

Reporting can be sent to multiple urls if required, CSP_REPORT_TO supports CSV, or the directive can be used with an array.

To add the policy to the list of applied policies you'll want to add some yaml config:

Silverstripe\CSP\CSPMiddleware:
  policies:
    - 'Silverstripe\CSP\Policies\CMS'
    - 'App\ContentSecurityPolicy'

In the above we've added it to be checked after the CMS policy that is included by default.

To make the policy report only you can either add the env var CSP_REPORT_ONLY='enabled' or code it in your policy, for example:

public function configure(): void
{
    parent::configure();
    if (Director::isDev()) {
        $this->reportOnly();
    }
}

Google Tag Manager / Adservices whitelist

Google uses localised regional domains for visitors for image tracker loading, which can pile up report violations with google.com|.co.nz|.com.au etc in your reporting tool. To resolve this and rather than specifying all of Google's listed support domains (see https://www.google.com/supported_domains) A white list config can be set to the GTM fragment to whitelist all https: URLs on the img-src directive, for example:

Silverstripe\CSP\Fragments\GoogleTagManager:
  whitelist_google_regional_domains: true

See also ImagesOverHTTPs::class for more basic cover of https images.

SRI

We also support SRI in this module, you can enable this via yaml:

Silverstripe\CSP\CSPMiddleware:
  sri_enabled: true

This will add SRI hashes to resources added through the requirements. It will not do this to the resources added through insertHeadTags. It will also not create this for files that are dynamically created (e.g. tinymce files)

We won't add SRI hashes for external resources, if this is required then you should be adding them in yourself after being provided them by the external resource (we don't do this as we can't control when or how often those are recalculated)

dev/build will clear the SRI records (we keep these to ensure we don't generate them per request). This has been added through the DevBuildExtension

Todo list:

  • Add the ability to hash inline scripts (this would sit in CSPBackend)
  • Add Google analytics and tag manager (this will happen shortly)
  • Add unit tests

Maintainers

Bugtracker

Bugs are tracked in the issues section of this repository. Before submitting an issue please read over existing issues to ensure yours is unique.

If the issue does look like a new bug:

  • Create a new issue
  • Describe the steps required to reproduce your issue, and the expected outcome. Unit tests, screenshots and screencasts can help here.
  • Describe your environment as detailed as possible: SilverStripe version, Browser, PHP version, Operating System, any installed SilverStripe modules.

Please report security issues to the module maintainers directly. Please don't file security issues in the bugtracker.

Development and contribution

If you would like to make contributions to the module please ensure you raise a pull request and discuss with the module maintainers.

License

See License

silverstripeltd/silverstripe-csp 适用场景与选型建议

silverstripeltd/silverstripe-csp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.94k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2021 年 07 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cms」 「silverstripe」 「csp」 「content security policy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 silverstripeltd/silverstripe-csp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 silverstripeltd/silverstripe-csp 我们能提供哪些服务?
定制开发 / 二次开发

基于 silverstripeltd/silverstripe-csp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 16
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2021-07-27