softspring/response-headers

此组件为 Symfony 项目提供响应头配置


README

Latest Stable Latest Unstable License PHP Version Downloads CI Coverage

此组件专为 Symfony 开发,允许通过配置来设置响应头。

安装

使用 Symfony Flex 的应用程序

打开命令行控制台,进入您的项目目录并执行

$ composer require softspring/response-headers

基本配置

创建配置文件

# config/packages/response_headers.yaml
parameters:
    response_headers:
        X-Frame-Options: "SAMEORIGIN"
        X-Content-Type-Options: "nosniff"

services:
    Softspring\Component\ResponseHeaders\EventListener\ResponseHeadersListener:
        tags: ['kernel.event_subscriber']
        arguments:
            $headers: '%response_headers%'

使用条件

您可以为应用响应头设置一些条件。

配置服务

此功能需要表达式语言组件

$ composer require symfony/expression-language

然后您必须配置表达式语言服务

# config/packages/response_headers.yaml
parameters:
    response_headers_global_conditions: []
    response_headers:
        ...

services:
    softspring.response_headers.expression_language:
        class: Symfony\Component\ExpressionLanguage\ExpressionLanguage
        arguments:
            - '@?Psr\Cache\CacheItemPoolInterface'

    Softspring\Component\ResponseHeaders\EventListener\ResponseHeadersListener:
        tags: ['kernel.event_subscriber']
        arguments:
            $headers: '%response_headers%'
            $expressionLanguage: '@softspring.response_headers.expression_language'
            $globalConditions: '%response_headers_global_conditions%'

定义条件

现在您可以在应用响应头之前设置一个条件

# config/packages/response_headers.yaml
parameters:
    response_headers:
        X-Frame-Options: 
            value: "SAMEORIGIN"
            condition: "request.getPathInfo() matches '^/admin'"
        Access-Control-Allow-Origin:
            value: "*"
            condition: "request.getPathInfo() matches '^/api'"

定义全局条件

您还可以为所有头部设置全局条件

# config/packages/response_headers.yaml
parameters:
    response_headers_global_conditions:
        - 'isMainRequest'

此全局条件建议用于避免为子请求设置头部,但这不是强制的。

构建条件

对于条件,可用 requestresponse 对象。还定义了一个 isMainRequest 变量。

查看 Symfony expression-language 文档

头部配置参考

定义头部有几种方法

单值头部

# config/packages/response_headers.yaml
parameters:
    response_headers:
        X-Frame-Options: "SAMEORIGIN" 

此代码生成 x-frame-options: "SAMEORIGIN" 头部。

多值头部

多值头部将被合并为单个由分号分隔的字符串

# config/packages/response_headers.yaml
parameters:
    response_headers:
        Feature-Policy:
            - "geolocation 'self'"
            - "vibrate 'none'" 

此代码生成 feature-policy: "geolocation 'self'; vibrate 'none'" 头部。

值字段

您还可以在 value 字段中定义值

# config/packages/response_headers.yaml
parameters:
    response_headers:
        X-Frame-Options: 
            value: "SAMEORIGIN" 
        Feature-Policy:
            value:
                - "geolocation 'self'"
                - "vibrate 'none'" 

如果想要设置条件或替换行为,则此 value 字段是必须的。

条件

如前所述,头部可以通过条件进行限制

# config/packages/response_headers.yaml
parameters:
    response_headers:
        X-Frame-Options: 
            value: "SAMEORIGIN"
            condition: "request.getHost() == 'api.mydomain.com"

替换行为

Symfony 响应允许定义是否必须替换之前定义的头部值。

默认情况下,此替换行为被定义为 true。但您可以使用以下方式禁用它

# config/packages/response_headers.yaml
parameters:
    response_headers:
        X-Frame-Options: 
            value: "SAMEORIGIN"
            replace: false

常见安全头部

这是一个定义常见安全头部的示例

# config/packages/response_headers.yaml
parameters:
    response_headers_global_conditions:
        - 'isMainRequest'
    response_headers:
        X-XSS-Protection:
            - "1"
            - "mode=block"
        X-Frame-Options: "SAMEORIGIN"
        X-Content-Type-Options: "nosniff"
        Strict-Transport-Security:
            - "max-age=31536000"
            - "includeSubDomains"
        Referrer-Policy: "same-origin"
        Feature-Policy:
            - "geolocation 'self'"
            - "vibrate 'none'"
            # ... include every feature the application uses.
        Content-Security-Policy:
            - "default-src 'none'"
            - "img-src 'self'"
            - "font-src 'self'"
            - "manifest-src 'self'"
            - "frame-src 'self'"
            - "script-src 'self' 'unsafe-inline'"
            - "style-src 'self' 'unsafe-inline'"
            - "connect-src 'self'"

查看 Content-Security-Policy 以包含您使用的每个基础 URL 的服务。还尝试避免 unsafe-inline 配置,这取决于您的项目。

许可证

此包在 MIT 许可证下。请参阅包中的完整许可证文件。