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
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
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
Tinkerwell logo

Tinkerwell

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

Tinkerwell
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
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
Lucky Media logo

Lucky Media

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

Lucky Media
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
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
PhpStorm logo

PhpStorm

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

PhpStorm

The latest

View all →
Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel image

Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel

Read article
Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire image

Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire

Read article
Frontend Nation 2026 Returns June 3-4 with Laravel in the Lineup image

Frontend Nation 2026 Returns June 3-4 with Laravel in the Lineup

Read article
Use a Google Sheet as Your Laravel Database with the Google Sheets Database Driver image

Use a Google Sheet as Your Laravel Database with the Google Sheets Database Driver

Read article
Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel image

Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel

Read article
Generate HTML Password Rules Attribute in Laravel 13.9.0 image

Generate HTML Password Rules Attribute in Laravel 13.9.0

Read article