25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CSRF protection<br />

CSRF (cross-site request forgery) is a type of malicious exploit where an<br />

attacker gets a user to unknowingly perform an action on a server that they're<br />

currently logged in to. This action will change something on the server that is<br />

advantageous to the attacker, for example, transfer money, change the password<br />

to one the attacker knows, <strong>and</strong> so on.<br />

For example, an attacker might hide a script in a web page or email <strong>and</strong> direct<br />

the user to it somehow. When it executes, this script could make a POST request<br />

to importantwebsite.com/updateEmailAndPassword. If the user is logged in to this site, the<br />

request may be successful.<br />

One way to prevent this kind of attack is to embed a special token, essentially a<br />

r<strong>and</strong>om string, in any form that a user might submit. When the form is<br />

submitted, the token is checked against the user's session to make sure it<br />

matches. An attacker won't be able to forge this token in their script <strong>and</strong> should,<br />

therefore, be thwarted by this feature.<br />

In <strong>Laravel</strong>, CSRF token creation <strong>and</strong> verification is managed by the<br />

VerifyCsrfToken middleware that is added to the web routes by default:<br />

Figure 9.4. CSRF prevention process To include the CSRF token in a form you can simply add {{<br />

csrf_field() }} within the form tag. This will generate a hidden input field containing a valid CSRF<br />

token, for example: <br />

This won't work in our scenario, though, as our form is not inside a Blade view

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!