marein/symfony-standard-headers-csrf-bundle

使用标准头部保护symfony应用程序免受CSRF攻击。

1.0.3 2023-12-16 12:08 UTC

This package is auto-updated.

Last update: 2024-09-16 14:28:53 UTC


README

CI

目录

概述

使用标准头部保护symfony应用程序免受CSRF攻击。

此bundle使用的防止CSRF攻击的机制,可以在OWASP中阅读。该技术被称为“使用标准头部验证来源”。

如何工作?

此bundle基于头部HostOriginReferer。它们是禁止头部的一部分,并且不能使用标准浏览器程序化更改。请仔细阅读OWASP页面,因为此技术可能不是在所有情况下都有效。

如果请求不安全,此bundle将返回状态代码403。以下至少有一个条件满足时,请求是安全的

  • http方法是安全的http方法。
  • 请求路径与配置中的allowed_paths之一匹配。
  • 来源头部与Host头部或配置中的allowed_origins之一匹配。
  • fallback_to_referer已启用,并且Referer头部与Host头部或配置中的allowed_origins之一匹配。
  • allow_null_origin已启用,并且Origin头部等于"null"

如果您的symfony应用程序中配置了可信代理,则使用X-Forwarded-Host而不是Host

安装和需求

将bundle添加到您的项目中。

composer require marein/symfony-standard-headers-csrf-bundle

在kernel中添加bundle。这可能会根据您的设置而不同。

public function registerBundles()
{
    return [
        // ...
        new \Marein\StandardHeadersCsrfBundle\MareinStandardHeadersCsrfBundle(),
        // ...
    ];
}

配置

以下是yaml格式的所有配置示例。

marein_standard_headers_csrf:
    # List of regular expressions that are used to check for allowed request paths.
    # Each entry is automatically surrounded by the delimiter #.
    #
    # Type: string[]
    # Default: []
    allowed_paths:
        - '^/api'

    # List of regular expressions that are used to check for allowed origins.
    # Each entry is automatically surrounded by the delimiter #.
    #
    # Type: string[]
    # Default: []
    allowed_origins:
        - '^https?://my-domain\.com$'
        - '^https?://.*\.my-other-domain-including-subdomains\.com$'

    # Switch to enable the comparison of the host header and allowed_origins with the referer header.
    #
    # Type: bool
    # Default: true
    fallback_to_referer: true

    # Switch to allow "null" as a valid origin header value.
    #
    # Type: bool
    # Default: false
    allow_null_origin: false

公共API

公共API仅包含bundle配置。其他一切都可能更改,并且不被视为破坏性更改。请勿直接使用类或服务。