Outsource Laravel Development Partner - $3200/Month | Bacancy

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
Laravel Code Review

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

Visit Laravel Code Review
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
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
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
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
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 →
Laravel Gets a Claude Code Simplifier Plugin image

Laravel Gets a Claude Code Simplifier Plugin

Read article
Laravel Boost Update Adds Support for the New MCP Protocol image

Laravel Boost Update Adds Support for the New MCP Protocol

Read article
Pest Adds withHost for Browser Testing Subdomains in Laravel image

Pest Adds withHost for Browser Testing Subdomains in Laravel

Read article
Run Artisan Make Commands in Laravel VS Code Extension image

Run Artisan Make Commands in Laravel VS Code Extension

Read article
Livewire 4 Is Dropping Next Week, and wire:transition Makes Animations Effortless image

Livewire 4 Is Dropping Next Week, and wire:transition Makes Animations Effortless

Read article
Laravel 12.45.1, 12.45.2, and 12.46.0 Released image

Laravel 12.45.1, 12.45.2, and 12.46.0 Released

Read article