All Projects → dunglas → Dunglasangularcsrfbundle

dunglas / Dunglasangularcsrfbundle

Licence: mit
Automatic CSRF protection for JavaScript apps using a Symfony API

Projects that are alternatives of or similar to Dunglasangularcsrfbundle

Feedbundle
A Symfony bundle to build RSS feeds from your entities
Stars: ✭ 130 (-14.47%)
Mutual labels:  symfony, symfony-bundle
Webapiclientgen
Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
Stars: ✭ 134 (-11.84%)
Mutual labels:  axios, jquery
Doctrinephpcrbundle
This bundle integrates Doctrine PHPCR ODM and PHPCR backends into Symfony
Stars: ✭ 131 (-13.82%)
Mutual labels:  symfony, symfony-bundle
Webpack Bundle
Bundle to Integrate Webpack into Symfony
Stars: ✭ 124 (-18.42%)
Mutual labels:  symfony, symfony-bundle
Nelmiosolariumbundle
Integration between Symfony and the Solarium Solr client.
Stars: ✭ 141 (-7.24%)
Mutual labels:  symfony, symfony-bundle
Routing Bundle
Integrate the CMF Routing component as a Symfony2 bundle: Have the chain router and the dynamic router available in Symfony2
Stars: ✭ 124 (-18.42%)
Mutual labels:  symfony, symfony-bundle
Stofdoctrineextensionsbundle
Integration bundle for DoctrineExtensions by l3pp4rd in Symfony
Stars: ✭ 1,713 (+1026.97%)
Mutual labels:  symfony, symfony-bundle
Vichuploaderbundle
A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
Stars: ✭ 1,613 (+961.18%)
Mutual labels:  symfony, symfony-bundle
Core
The server component of API Platform: hypermedia and GraphQL APIs in minutes
Stars: ✭ 2,004 (+1218.42%)
Mutual labels:  symfony, symfony-bundle
Consolebundle
Commandline interface in browser for Symfony2
Stars: ✭ 138 (-9.21%)
Mutual labels:  symfony, symfony-bundle
Liformbundle
Symfony Bundle to render Symfony Forms to JSON Schema
Stars: ✭ 124 (-18.42%)
Mutual labels:  symfony, symfony-bundle
Symfonyconfigtest
Stars: ✭ 142 (-6.58%)
Mutual labels:  symfony, symfony-bundle
Passwordstrengthbundle
Symfony Password strength and blacklisting validator bundle
Stars: ✭ 123 (-19.08%)
Mutual labels:  symfony, symfony-bundle
Wouterjeloquentbundle
Integrates the Eloquent ORM in the Symfony framework
Stars: ✭ 126 (-17.11%)
Mutual labels:  symfony, symfony-bundle
Swiftmailer Bundle
Symfony Swiftmailer Bundle
Stars: ✭ 1,558 (+925%)
Mutual labels:  symfony, symfony-bundle
Oblog
An Auto Rendering 🔽 Blogging System Based on MarkDown & Vue.js | 自动渲染装载 MarkDown 内容的博客系统
Stars: ✭ 133 (-12.5%)
Mutual labels:  axios, jquery
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (-25%)
Mutual labels:  symfony, symfony-bundle
Mobiscroll
Cross platform UI controls for progressive web and hybrid apps (plain JS, jQuery, Angular and React)
Stars: ✭ 1,510 (+893.42%)
Mutual labels:  jquery, angularjs
Sonatanotificationbundle
Symfony SonataNotificationBundle
Stars: ✭ 136 (-10.53%)
Mutual labels:  symfony, symfony-bundle
Datatables Bundle
DataTables bundle for Symfony
Stars: ✭ 142 (-6.58%)
Mutual labels:  symfony, symfony-bundle

JavaScript CSRF Protection Bundle

Archived! Now that all modern browsers implement SameSite cookies and the Origin HTTP header, this bundle is - in most cases - not necessary anymore. Learn how to protect your Symfony APIs from CSRF attacks. If you need to maintain old applications, take a look to DneustadtCsrfCookieBundle.

This API Platform and Symfony bundle provides automatic Cross Site Request Forgery (CSRF or XSRF) protection for client-side applications.

Despite the name, it works with any client-side technology including Angular, React, Vue.js and jQuery. Actually, any JavaScript code issuing XMLHttpRequest or using the Fetch API can leverage this bundle.

Build Status SensioLabsInsight Dependency Status StyleCI

How it Works

Thanks to this bundle, the server-side application (the Symfony app) will automatically set a cookie named XSRF-Token containing a unique token during the first HTTP response sent to the browser. Subsequent asynchronous requests made by the JavaScript app with xhr or fetch send back the value of the cookie in a special HTTP header named X-XSRF-Token.

To prevent CSRF attacks, the bundle will check that the header's value match the cookie's value. This way, it will be able to detect and block CSRF attacks.

AngularJS (v1)'s ng.$http service has a built-in support for this CSRF protection system. If you use another framework or HTTP client (such as Axios), you just need to read the cookie value and add the HTTP header containing it by yourself.

This bundle provides a Symfony's Event Listener that set the cookie and another one that checks the HTTP header to block CSRF attacks.

Thanks to DunglasAngularCsrfBundle, you get CSRF security without modifying your code base.

This bundle works fine with both API Platform and FOSRestBundle.

Installation

Use Composer to install this bundle:

composer require dunglas/angular-csrf-bundle

If you use Symfony Flex, you're done.

Otherwise add the bundle in your application kernel:

// app/AppKernel.php

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

Configure URLs where the cookie must be set and that must be protected against CSRF attacks:

# 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] }
        

Your app is now secured.

Examples

  • DunglasTodoMVCBundle: an implementation of the TodoMVC app using Symfony, Backbone.js and Chaplin.js

Full Configuration

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] }

Integration with the Symfony Form Component

When using the Symfony Form Component together with DunglasAngularCsrfBundle, the bundle will automatically disable the built-in form CSRF protection only if the CSRF token provided by the header is valid.

If no CSRF header is found or if the token is invalid, the form CSRF protection will not be disabled by the bundle.

If you want your form to be validated only by the form component system, make sure to remove its URL from the config.

Credits

This bundle has been created by Kévin Dunglas.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].