⚠️ The technique(s) described in this article are for educational purposes only.

Cyber attacks are nothing new. From the early days of the internet there have been web attacks and the truth is they get more sophisticated every day. Some attacks are made to spy on users, some steal user data, some steal from users.

In this article, we are going to take a look at CSRF, a type of web attack where the attacker tries to hijack requests. In CSRF attacks, the attacker leverages already authenticated users and discreetly makes them send requests to the web server on their behalf. We will see how this is achieved and how Laravel provides you with the tools necessary to mitigate this attack.

Prerequisites

To follow this article you will need:

What is CSRF?

CSRF stands for Cross Site Request Forgery and it’s a web attack that forces a user to make unintended requests to a web application where they are previously authenticated.

When building web applications, your server would usually be able to accept requests from any client provided the page exists, however, some pages on your server require authentication before being accessed.

A CSRF attack occurs when an authenticated user is tricked into interacting with a page or a script on a foreign website that generates a malicious request to your application server without the knowledge of the user; the server only sees a regular authenticated request and then processes it as normal but in the real sense of things, it is processing it on behalf of the attacker.

CSRF attack diagram

How is a CSRF attack carried out?

Assume you have an application that is used to send money to friends and for users to send money, they need to be signed in. Assume that the action to send money is also a simple form that looks like the image below:

Demo form

The make-believe form requires the email of the recipient and the amount to be sent. When the send button is clicked, a POST request is made to the application server to send the amount to the recipient.

Everything seems okay and during testing, logged in users can send money to other users, which is what is expected.

How the attack would work

An attacker who wants to hoodwink the system will very likely study the application for a while trying to locate vulnerabilities. They note the URL where the request is sent to and they know it needs to be a POST request with the email of the recipient and the amount you want to send.

The attacker then creates a program that can be embedded in an image or in the webpage directly and executed when the image is clicked or executed when a link is clicked.

When the script is executed, the server sees it as another regular request made from the logged in user and then processes it. This means that everyone authenticated to the target site visiting the attacker’s site will be open to a CSRF attack and may indeed be sending money they didn’t intend to send.

Carrying out your own CSRF attack

Now, let’s look at how to do a simple CSRF attack on an application. CSRF attacks are more complex than these but for the sake of brevity, we will keep it simple. We are going to make a script that executes when a link is clicked. The script can be as simple as this HTML file

    <html>
    <head>
    <title>Sample Attacker</title>
    </head>
    <body>

      <!-- interesting image/content -->

      <script type="text/javascript">
        var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "http://127.0.0.1:8000/sendmoney", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("email=neo@sendmoney.com&amount=100");
      </script>
    </body>
    </html>

As seen above, the script is embedded in the page and is designed to not be detectable by the average user. It uses AJAX to make a POST request to a susceptible page and submit its form. Now all the user has to do is trick you into viewing the webpage and you’d be sending a request down to the website to send money to an attacker.

Dodgy site - user view

Dodgy site - under the hood

When the page loaded, the request was sent in the background to the server without the knowledge of the user for a money transfer to be processed.

How to protect your Laravel app from CSRF attacks

To protect your application, Laravel uses CSRF tokens. CSRF tokens are strings that are automatically generated and can be attached to a form when the form is created. They are used to uniquely identify forms generated from the server.

The idea behind it is that when the server receives POST requests, the server checks for a CSRF token. If the POST request has a token that matches the active existing CSRF token created by the framework, the form is processed. If not, the form is not processed and an error is sent back to the client making the request.

This token is also generated per request meaning it cannot be reused once it’s used once. Let’s see this in practice.

Creating a Laravel application

To show how to protect your application, let’s make a sample application that allows signed in users send money to friends. Create a new Laravel project by running the command:

    $ laravel new money-sender

Let’s set up authentication. Laravel comes with an optional Auth scaffold that we can use to set up authentication in minutes, let’s use that. Run the command below in the root of the Laravel project you created:

    $ php artisan make:auth

This generates all you will need to authenticate your user, the sign up page, login page, password reset page and a dashboard.

Page with authentication scaffolding

Open the HomeController generated from the Auth scaffolding and replace the contents with the following:

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;

    class HomeController extends Controller
    {
        public function __construct()
        {
            $this->middleware('auth');
        }

        public function index()
        {
            $user = Auth::user();

            return view('home', ['user' => $user]);
        }
    }

Laravel comes with a User model in the application. Let’s update this. Open the app/User.php file where the User model class is stored and replace the contents with the following:

    <?php

    namespace App;

    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;

    class User extends Authenticatable
    {
        use Notifiable;

        protected $hidden = ['password', 'remember_token'];

        protected $fillable = ['name', 'email', 'password', 'balance'];    

        public function charge(int $amount)
        {
            return $this->update(['balance' => $this->balance - $amount]);
        }

        public function grant(int $amount)
        {
            return $this->update(['balance' => $this->balance + $amount]);
        }

    }

Above we added two methods, first is charge which charges a user and grant which adds balance to a user.

Creating our application’s migrations

Migrations are a way to make and keep track of the changes made to the database your application is connected to. When you create a new Laravel application, you have a users table migration by default in your application.

Let’s update the migration to include a balance field. Open the create_users_table migration file in the database/migrations directory and replace the contents with the following:

    <?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateUsersTable extends Migration
    {
        public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->string('password');
                $table->integer('balance')->default(5000);
                $table->rememberToken();
                $table->timestamps();
            });
        }

        public function down()
        {
            Schema::dropIfExists('users');
        }
    }

The up() function is called when this migration is run and down() is called to revert changes made by the up() method.

The user balance was set to 5000 by default for testing purposes of the application. This means that every user signing up has a balance of 5000.

Before we run the migration we need to connect our application to a database. In this article, we will be using SQLite. As part of the prerequisites mentioned earlier, you need SQLite installed on your machine.

Create a new empty file database.sqlite in the database directory.

Next, open the .env file in the root of your project and replace the following lines:

    DB_CONNECTION=mysql
    DB_DATABASE=homestead
    DB_USERNAME=username
    DB_PASSWORD=password

With

    DB_CONNECTION=sqlite
    DB_DATABASE=/full/path/to/database.sqlite

Run the command below in your terminal to start the database migration:

    $ php artisan migrate

Creating our application’s controller

To handle the send money action, create a new controller by running the command:

    $ php artisan make:controller SendMoneyController

This creates a new SendMoneyController file in your app/Http/Controllers directory. Open this file and replace the contents with the following:

    <?php

    namespace App\Http\Controllers;

    use App\User;
    use Illuminate\Http\Request;

    class SendMoneyController extends Controller
    {
        public function __construct()
        {
            $this->middleware('auth');
        }

        public function send(Request $request)
        {
            $data = $request->validate([
                'email' => 'required|email',
                'amount' => 'required|numeric'
            ]);

            $sender = auth()->user();
            $recipient = User::where('email', $data['email'])->first();

            $sender->charge($data['amount']);
            $recipient->grant($data['amount']);

            return redirect()->action('HomeController@index')
                            ->withStatus("${$data['amount']} sent to {$recipient->name}");
        }
    }

When a request is received, the controller validates the request parameters, then the transfer is made.

Creating the send form

Let’s create the form to send money. Open the resources/views/home.blade.php file and replace the contents with he following:

@extends('layouts.app')
 @section('content')
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-md-8">
        <div class="card">
          <div class="card-header">Dashboard</div>
            <div class="card-body">
              @if (session('status'))
                <div class="alert alert-success">
                    {{ session('status') }}
                </div>
              @endif
            <p>Wallet Balance : $ {{ $user->balance}}</p>
            <form action="{{ url('/sendmoney')}}" method="post">
            <div class="form-group row">
              <label for="email" class="col-md-4 col-form-label text-md-right">Recipient's Email :</label>
              <div class="col-md-6">
                <input id="email" type="email" class="form-control" name="email" required autofocus>
              </div>
            </div>
            <div class="form-group row">
              <label for="amount" class="col-md-4 col-form-label text-md-right">Amount :</label>
              <div class="col-md-6">
                <input id="amount" type="numeric" class="form-control" name="amount"  required autofocus>
              </div>
            </div>
            <div class="form-group row mb-0">
              <div class="col-md-6 offset-md-4">
                <button type="submit" class="btn btn-primary">
                  Send Money
                </button>
              </div>
            </div>
            </form>
            </div>
          </div>
      </div>
    </div>
  </div>
    @endsection

Above we have the view for the dashboard for a logged in user.

Logged in user dashboard

Using the CSRF token

Laravel already prevents itself from CSRF attacks by default whether you do anything or not. So, if you make a request without adding the CSRF Token, the request will be rejected.

If you go to the file app/Http/Kernel.php you will see the VerifyCsrfToken middleware defined:

    <?php

    [...]

    class Kernel extends HttpKernel
    {
        [...]

        protected $middlewareGroups = [
            'web' => [
                [...]
                \App\Http\Middleware\VerifyCsrfToken::class,
                [...]
            ],

            [...]
        ];

        [...]
    }

If you go to the app/Providers/RouteServiceProvider.php file you’ll see that in the mapWebRoutes() method the web group middleware is added to all web routes. This means that by default Laravel will check for a valid CSRF token using the VerifyCsrfToken class.

💡 You can disable this functionality by commenting out the VerifyCsrfToken class from the list of classes in the middlewareGroups array in the app/Http/Kernel.php file.

Back to our example form, if you fill the form and hit the Send Money button, you’ll get an error page because the CSRF token is missing. To add the CSRF token to the form, you need to update the home.blade.php to add a @csrf blade directive as seen below:

    [...]
      <form action="{{ url('/sendmoney')}}" method="post">
          @csrf   
         [...]
      </form>
    [...]

💡 Blade directives are defined shortcuts or functions which expand to longer functions. It can help us abstract logic away from the blade views. Read more about blade directives.

When the @csrf blade directive is used, an input tag of type='hidden``' is added to the form. The value will be the CSRF token which will be sent as part of the form data when the form is submitted.

How the VerifyCsrfToken works

When a POST request is made, the VerifyCSRFToken middleware handles the request. This middleware is a part of Laravel and its class extension is defined here: app/Http/Middleware/VerifyCsrfToken.php.

    <?php

    namespace App\Http\Middleware;

    use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

    class VerifyCsrfToken extends Middleware
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        protected $except = [
            //
        ];
    }

We can see from the above, that is VerifyCsrfToken class extends the Illuminate\Foundation\Http\Middleware\VerifyCsrfToken class. This is where the full logic for the VerifyCsrfToken token is defined.

Let’s take a quick look at a couple of methods in it to see how it works.

    [...]
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     *
     * @throws \Illuminate\Session\TokenMismatchException
     */
    public function handle($request, Closure $next)
    {
        if (
            $this->isReading($request) ||
            $this->runningUnitTests() ||
            $this->inExceptArray($request) ||
            $this->tokensMatch($request)
        ) {
            return $this->addCookieToResponse($request, $next($request));
        }
        throw new TokenMismatchException;
    }
    [...]

The method handle() takes the incoming request and checks if:

  • The incoming request is a POST request.
  • The request was not generated from a unit test.
  • The route visited was not exempted by the developer in the except array.
  • The token is correct.

If all these criteria are not met, then a TokenMismatchException is raised and is sent to the user.

Token expired page

Custom CSRF token error handling

The error Laravel returns is not quite ideal for many applications and you may want to display a custom error page. To do this, you will need to update the render() method in the app/Exceptions/Handler.php file. This file as the name suggests handles exceptions in your application.

Update the render() method as seen below:

    [...]

    public function render($request, Exception $exception)
    {
        if( $exception instanceof TokenMismatchException){
            return response()
                ->view('errors.401', ['error' => 'Page expired, go back and try again'], 401);
        }

        return parent::render($request, $exception);
    }

    [...]

Above we checked if the exception being handled is TokenMismatchException and override the response first before Laravel does.

Create the error view file 401.blade.php in the resources/views/errors directory and add the code below:

    @extends('layouts.app')

    @section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Error</div>
                    <div class="card-body">
                        @if ($error)
                            <div class="alert alert-danger">
                                {{ $error }}
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endsection

Now, when a request is made without a CSRF Token, this is the result:

No token error

Looks a lot better.

When the CSRF token is added to the view and money is sent, we get the response:

With token

Conclusion

In this article, we took a critical look at CSRF attacks, the damage they can cause if not checked and how to prevent CSRF attacks in your Laravel applications.

The source code for this article is available on Github.