4 Laravel 11 Basics: CSRF Protection

Cross-site request forgery (CSRF) is a type of malicious exploit where unauthorized commands are performed on behalf of an authenticated user. Laravel makes it easy to protect your application from CSRF attacks.
Introduction to CSRF Protection
CSRF attacks exploit the trust that a site has in the user's browser. Laravel automatically generates a CSRF token for each active user session. This token is used to verify that the authenticated user is the person actually making the requests.
Preventing CSRF Requests
Laravel automatically generates a CSRF "token" for each active user session. This token is used to verify that the authenticated user is the person actually making the requests to the application.
You can access the current session's CSRF token via the request's session or the csrf_token
helper function:
use Illuminate\Http\Request;
Route::get('/token', function (Request $request) {
$token = $request->session()->token();
$token = csrf_token();
// ...
});
Anytime you define a "POST", "PUT", "PATCH", or "DELETE" HTML form in your application, you should include a hidden CSRF _token
field in the form so that the CSRF protection middleware can validate the request. For convenience, you may use the @csrf
Blade directive to generate the hidden token input field:
<form method="POST" action="/profile">
@csrf
<!-- Form fields -->
</form>
The ValidateCsrfToken
middleware, which is included in the web
middleware group by default, will automatically verify that the token in the request input matches the token stored in the session.
CSRF Tokens & SPAs
If you are building an SPA that uses Laravel as an API backend, consult the Laravel Sanctum documentation for information on authenticating with your API and protecting against CSRF vulnerabilities.
Excluding URIs From CSRF Protection
Sometimes you may wish to exclude certain URIs from CSRF protection. For example, if you are using Stripe to process payments and utilizing their webhook system, you will need to exclude your Stripe webhook handler route from CSRF protection. You can do this by providing the URIs to the validateCsrfTokens
method in your application's bootstrap/app.php
file:
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(except: [
'stripe/*',
'http://example.com/foo/bar',
'http://example.com/foo/*',
]);
});
X-CSRF-TOKEN
In addition to checking for the CSRF token as a POST parameter, the ValidateCsrfToken
middleware will also check for the X-CSRF-TOKEN
request header. You could, for example, store the token in an HTML meta tag:
<meta name="csrf-token" content="{{ csrf_token() }}">
Then, 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 using legacy JavaScript technology:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
X-XSRF-TOKEN
Laravel stores the current CSRF token in an encrypted 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 primarily sent as a developer convenience since some JavaScript frameworks and libraries, like Angular and Axios, automatically place its value in the X-XSRF-TOKEN
header on same-origin requests.
Learn more about CSRF protection from the official Laravel documentation.
Conclusion
CSRF protection is essential for securing web applications. This tutorial covered how to use CSRF tokens in Laravel 11 to protect your application from CSRF attacks. For more detailed information, refer to the official Laravel documentation.
Happy coding with Laravel 11!
Discuss Your Project with Us
We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.
Let's find the best solutions for your needs.