CSRF

CSRF

CSRF stand for Cross-site request forgery , it’s a web security vulnerability that allows an attacker to induce users to perform actions that they do not intend to perform.
CSRF is an attack where a malicious site tricks a user’s browser into submitting unauthorized requests to a trusted site where the user is authenticated.

CSRF Protection

Laravel protects against CSRF attacks by requiring a token in POST requests. Add the Blade directive @csrf inside your form to include a hidden token input.
Like so:

PHP
<form method="POST" action="/jobs">
    @csrf

What this does it adds a hidden input field with a specific value. So if that value doesn’t match with the one saved on the site and you submit (try to atleast) it gives a 419 error (page expired). Here’s how the hidden input file looks like:

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.

Tags: