Laravel CSRF Protection


Laravel CSRF Protection – The Laravel CSRF Protection is used to make its easy to protect your application from cross site request forgery(CSRF) attack.


Laravel CSRF Protection.

Let us understand how to use laravel CSRF protection.

Function:-

There are some followings function available in laravel CSRF protection.

  • 1. Introduction.
  • 2. Excluding URIs From CSRF Protection.
  • 3. X-CSRF-TOKEN.
  • 4. X-XSRF-TOKEN.

1. Introduction.

It makes easy to protect your application from CSRF attack. Csrf are a type harmful exploit whereby unauthorized commands are performed on behalf of an authenticated user.

Laravel automatically generates a CSRF “token” for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.

Anytime you define in your application, you should include a hidden CSRF token field in the form. you may use the csrf_field helper to generate the token field.

<form method="POST" action="/profile">
{{ csrf_field() }}
  ......
</form>

The VerifyCsrfToken middleware, which is included in web middleware group.

CSRF Tokens & Vue

If you are using the Vue JavaScript framework without the authentication scaffolding provided by the make::auth Artsian command. you will need to manually define a Laravel JavaScript object in your primary application layout.

<script>
    window.Laravel = {!! json_encode([
        'csrfToken' => csrf_token(),
    ]) !!};
</script>

2. Excluding URIs From CSRF Protection.

If you are using stripe to process payments and are utilize their webhook system. You will need to exclude your Stripe webhook handler route from CSRF protection since Stripe will not know what CSRF token to send to your routes.

You should place these kinds of routes outside of the web middleware group that the RouteServiceProvider applies to all routes in the routes/web.php file.

Let’s look at a simple example.

<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    protected $except = [
        'stripe/*',
    ];
}

3. X-CSRF-TOKEN.

In addition to checking for the CSRF token as a POST parameter, the VerifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header.

<meta name="csrf-token" content="{{ csrf_token() }}">

Then, once you have created the meta tag. you can instruct a library like jQuery to automatically add the token to all request headers. This provides simple, convenient CSRF protection for your AJAX based applications.

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

4. X-XSRF-TOKEN.

Laravel stores the current CSRF token in a XSRF-TOKEN cookie that is included with each response generated by the framework. You can use the cookie value to set the X-XSRF-TOKEN request header.

This cookie is primarilly sent as a convenience since some javascript framework, like angular, automatically place its value in the X-XSRF-TOKEN header.


Advertisements

Add Comment

📖 Read More