dunglas/angular-csrf-bundle

此包已被废弃,不再维护。未建议替代包。

使用 AngularJS 和 Symfony2 时的 CSRF 保护

安装次数: 787,639

依赖项: 4

建议者: 0

安全: 0

星标: 152

关注者: 5

分支: 30

开放问题: 0

类型:symfony-bundle

v1.2.0 2019-05-06 16:23 UTC

This package is auto-updated.

Last update: 2021-01-28 14:57:28 UTC


README

已存档! 由于所有现代浏览器都实现了 SameSite cookies 和 Origin HTTP 头,因此此包在大多数情况下已不再必要。 了解如何保护您的 Symfony API 免受 CSRF 攻击 如果您需要维护旧应用程序,请查看 DneustadtCsrfCookieBundle

API PlatformSymfony 包为客户端应用程序提供自动的 跨站请求伪造 (CSRF 或 XSRF) 保护。

尽管名称如此,但它与任何客户端技术兼容,包括 AngularReactVue.jsjQuery。实际上,任何发送 XMLHttpRequest 或使用 Fetch API 的 JavaScript 代码都可以利用此包。

Build Status SensioLabsInsight Dependency Status StyleCI

工作原理

借助此包,服务器端应用程序(即 Symfony 应用程序)将在向浏览器发送的第一个 HTTP 响应中自动设置一个名为 XSRF-Token 的 cookie,其中包含一个唯一的令牌。JavaScript 应用程序随后的异步请求通过 xhrfetch 发送时,会将 cookie 的值发送回一个名为 X-XSRF-Token 的特殊 HTTP 头中。

为了防止 CSRF 攻击,该包将检查头部的值是否与 cookie 的值匹配。这样,它就可以检测并阻止 CSRF 攻击。

AngularJS (v1) 的 ng.$http 服务 支持此 CSRF 保护系统。如果您使用另一个框架或 HTTP 客户端(如 Axios),您只需要读取 cookie 的值,并自己添加包含该值的 HTTP 头。

此包提供了一个 Symfony 的 Event Listener,用于设置 cookie,另一个用于检查 HTTP 头以阻止 CSRF 攻击。

得益于 DunglasAngularCsrfBundle,您可以在不修改代码库的情况下获得 CSRF 安全性。

此包与 API PlatformFOSRestBundle 一起正常工作。

安装

使用 Composer 安装此包

composer require dunglas/angular-csrf-bundle

如果您使用 Symfony Flex,则已完成。

否则,请将包添加到您的应用程序内核中

// app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new Dunglas\AngularCsrfBundle\DunglasAngularCsrfBundle(),
        // ...
    );
}

配置必须设置 cookie 的 URL 以及必须防止 CSRF 攻击的 URL

# app/config/security.yml
dunglas_angular_csrf:
    # Collection of patterns where to set the cookie
    cookie:
        set_on:
            - { path: ^/$ }
            - { route: ^app_, methods: [GET, HEAD] }
            - { host: example.com }
    # Collection of patterns to secure
    secure:
        - { path: ^/api, methods: [POST, PUT, PATCH, LINK] }
        - { route: ^api_v2_ }
        - { host: example.com, methods: [POST, PUT, PATCH, DELETE, LINK] }
    # Collection of patterns to exclude
    exclude:
        - { path: ^/api/exclude, methods: [POST, PUT, PATCH, LINK] }
        - { route: ^api_v2_exclude }
        - { host: exclude-example.com, methods: [POST, PUT, PATCH, DELETE, LINK] }
        

您的应用程序现在已安全。

示例

完整配置

dunglas_angular_csrf:
    token:
        # The CSRF token id
        id: angular
    header:
        # The name of the HTTP header to check (default to the AngularJS default)
        name: X-XSRF-TOKEN
    cookie:
        # The name of the cookie to set (default to the AngularJS default)
        name: XSRF-TOKEN
        # Expiration time of the cookie
        expire: 0
        # Path of the cookie
        path: /
        # Domain of the cookie
        domain: ~
        # If true, set the cookie only on HTTPS connection
        secure: false
        # Patterns of URLs to set the cookie
        set_on:
            - { path: "^/url-pattern", route: "^route_name_pattern$", host: "example.com", methods: [GET, POST] }
    # Patterns of URLs to check for a valid CSRF token
    secure:
        - { path: "^/url-pattern", route: "^route_name_pattern$", host: "example.com", methods: [GET, POST] }
    # Patterns to exclude from secure routes
    exclude:
        - { path: "^/url-pattern/exclude", route: "^route_name_pattern$", host: "example.com", methods: [GET, POST] }

与Symfony表单组件的集成

当与DunglasAngularCsrfBundle一起使用Symfony表单组件时,只有当由头部提供的CSRF令牌有效时,包才会自动禁用内置表单CSRF保护。

如果没有找到CSRF头部或令牌无效,包不会禁用表单CSRF保护。

如果您想仅通过表单组件系统验证您的表单,请确保将其URL从配置中删除。

鸣谢

此包由Kévin Dunglas创建。