w3resource

Laravel (5.7) CSRF Protection

Introduction

CSRF refers to Cross-Site Request Forgery attacks on web applications. CSRF attacks are the unauthorized activities which the authenticated users of the system perform. As such, many web applications are prone to these attacks.

Laravel offers CSRF protection in the following way −

Laravel includes an inbuilt CSRF plug-in, that generates tokens for each active user session. These tokens verify that the operations or requests are sent by the concerned authenticated user.

Implementation

The implementation of CSRF protection in Laravel is discussed in detail in this section. The following points are notable before proceeding further on CSRF protection −

  • CSRF is implemented within HTML forms declared inside the web applications. You have to include a hidden validated CSRF token in the form so that the CSRF protection middleware of Laravel can validate the request. The syntax is shown below −
<form method = "POST" action="/profile">
   {{ csrf_field() }}
   ...
</form>
  • You can conveniently build JavaScript-driven applications using JavaScript HTTP library, as this includes CSRF token to every outgoing request.
  • The file namely resources/assets/js/bootstrap.js registers all the tokens for Laravel applications and includes meta tag which stores csrf-token with Axios HTTP library.

CSRF Tokens and JavaScript

When building JavaScript-driven applications, it is convenient to have your JavaScript HTTP library automatically attach the CSRF token to every outgoing request. By default, the resources/js/bootstrap.js file registers the value of the csrf-token meta tag with the Axios HTTP library. If you are not using this library, you will need to manually configure this behavior for your application.

Form without CSRF Token

Consider the following lines of code. They show a form which takes two parameters as input: email and message.

<form>
   <label> Email </label>
      <input type = "text" name = "email"/>
      <br/>
   <label> Message </label> <input type="text" name = "message"/>
   <input type = "submit" name = "submitButton" value = "submit">
</form>

The form shown above will accept any input information from an authorized user. This may make the web application prone to various attacks.

Please note that the submit button includes functionality in the controller section. The postContact function is used in controllers for that associated views. It is shown below −

public function postContact(Request $request) {
   return $request-> all();
}

Observe that the form does not include any CSRF tokens so the sensitive information shared as input parameters are prone to various attacks.

Form with CSRF Token

The following lines of code show you the form re-designed using CSRF tokens −

<form method = "post" >
   {{ csrf_field() }}
   <label> Email </label>
   <input type = "text" name = "email"/>
   <br/>
   <label> Message </label>
   <input type = "text" name = "message"/>
   <input type = "submit" name = "submitButton" value = "submit">
</form>

The output achieved will return JSON with a token as given below −

{
   "token": "ghfleifxDSUYEW9WE67877CXNVFJKL",
   "name": "TutorialsPoint",
   "email": "[email protected]"
}

This is the CSRF token created on clicking the submit button.

Previous: Laravel (5.7) Middleware
Next: Laravel (5.7) Controllers



Share this Tutorial / Exercise on : Facebook and Twitter