marein / symfony-standard-headers-csrf-bundle
使用标准头部保护symfony应用程序免受CSRF攻击。
1.0.3
2023-12-16 12:08 UTC
Requires
- php: ^7.4 || ^8.0
- symfony/config: ^5.1 || ^6.0 || ^7.0
- symfony/dependency-injection: ^5.1 || ^6.0 || ^7.0
- symfony/http-foundation: ^5.1 || ^6.0 || ^7.0
- symfony/http-kernel: ^5.1 || ^6.0 || ^7.0
Requires (Dev)
- phpstan/phpstan: 1.2.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: 3.6.1
- symfony/browser-kit: ^5.1 || ^6.0 || ^7.0
- symfony/framework-bundle: ^5.1 || ^6.0 || ^7.0
This package is auto-updated.
Last update: 2024-09-16 14:28:53 UTC
README
目录
概述
使用标准头部保护symfony应用程序免受CSRF攻击。
此bundle使用的防止CSRF攻击的机制,可以在OWASP中阅读。该技术被称为“使用标准头部验证来源”。
如何工作?
此bundle基于头部Host
、Origin
和Referer
。它们是禁止头部的一部分,并且不能使用标准浏览器程序化更改。请仔细阅读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配置。其他一切都可能更改,并且不被视为破坏性更改。请勿直接使用类或服务。