Polyscope - The agent-first dev environment for Laravel

Cloudflare Turnstile versus CSRF tokens

Last updated on by

Cloudflare Turnstile versus CSRF tokens image

I'm on a quest to improve the load times for laravelshift.com. I reached over 90% caching by focusing on my highest traffic content pages. Now I have a few relatively high traffic forms I want to cache. These are the Can I Upgrade Laravel and Convert curl to Http.

As forms within a Laravel application, I naturally add a CSRF token via the @csrf Blade directive. Underneath this inserts a hidden <input> field on the page with a unique, random value. This value is also added to the user's session.

Since it relies on the session, the CSRF token creates dynamic content on the page. As such, it's not cacheable. Or at least if I did cache it, users would get a 419 error upon submission. In order to make the page cacheable, I'd need to remove the CSRF token.

This got me asking, Do I need the CSRF token? Part of me thought CSRF tokens help prevent SPAM. But that's not really their intent. CSRF stands for Cross-Site Request Forgery. This token aims to prevent another site from submitting data or taking actions on your user's behalf. For example, submitting a request to deleted one of your Forge servers.

That's the theory. In code, it means we add a token to the form data. Upon submission, this token is validated against the one in the user's session. If it doesn't, Laravel rejects the submission. What if we could add something else to the form. Something that didn't rely on the session, but still provided some kind of security.

Enter Cloudflare Turnstile. Turnstile adds a challenge to the page. It can be visible to the user or invisible. When invisible it does some "work" in the background with JavaScript. Most bots can't perform this work. So they fail the challenge.

Since Turnstile uses JavaScript, it loads client side. Therefore making the page cacheable. This was a win-win for me. First, I can cache the high traffic forms. Second, I gained some SPAM prevention. Turnstile is much better at this using a challenge, and also with its collective intelligence.

Now, as always, there is a tradeoff. CSRF and Turnstile serve different purposes. For my case, these are public forms (unauthenticated). So I don't care if someone forges their submission from another site. Furthermore, Turnstile is configured by domain. So the challenge wouldn't run from another site. Meaning the form data would have a missing or incorrect value.

In the end, while Turnstile likely increased my SPAM protection, I accept any risk I may have introduced by removing the CSRF token. Having these forms cacheable was more important. In doing so, laravelshift.com is now 98% cached.

If you're interested in implementing Cloudflare Turnstile I used ryangjchandler/laravel-cloudflare-turnstile. There's a good write up in a previous Laravel News article.

If you're interested in making your Laravel apps respond crazy fast, check out my Fast Laravel video course.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Browse and Execute Artisan Commands from an Interactive TUI image

Browse and Execute Artisan Commands from an Interactive TUI

Read article
Laravel Pint Now Replaces Fully Qualified Class Names with Imports image

Laravel Pint Now Replaces Fully Qualified Class Names with Imports

Read article
Inertia v3 Upgrade Prompt and JSON Log Support in Laravel Boost v2.3.0 image

Inertia v3 Upgrade Prompt and JSON Log Support in Laravel Boost v2.3.0

Read article
Laracon AU Returns to Brisbane - Call for Speakers Now Open image

Laracon AU Returns to Brisbane - Call for Speakers Now Open

Read article
Detecting and Fixing Race Conditions in Laravel Applications image

Detecting and Fixing Race Conditions in Laravel Applications

Read article
LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI image

LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI

Read article