Defense-in-Depth CSRF Strategy
You'll learn how to layer multiple CSRF defenses together to create a robust, resilient protection system that doesn't rely on a single point of failure.
The Core Idea
Defense-in-depth means never trusting just one countermeasure. In CSRF protection, this means combining several techniques—like synchronizer tokens, SameSite cookies, and re-authentication—so that if one layer fails or is bypassed, others still protect your users.
Think of it like securing a bank vault: you don't just use one lock. You have multiple locks, cameras, guards, time delays, and alarm systems. If someone picks one lock, they still can't get through. The same principle applies to CSRF defense.
Layered CSRF Protection
Base Layer: Always implement synchronizer tokens or double submit cookies as your primary defense. These validate that requests originate from your application.
Second Layer: Configure SameSite cookie attributes (Strict or Lax) to prevent cookies from being sent with cross-site requests automatically.
Third Layer: Use custom request headers (like X-Requested-With) for AJAX requests, which browsers won't send cross-origin without CORS permission.
Fourth Layer: Validate Origin and Referer headers when present to confirm the request came from your domain.
Critical Actions Layer: For sensitive operations (changing passwords, transferring money, deleting accounts), require re-authentication or additional confirmation beyond CSRF tokens alone.
Why Multiple Layers Matter
Each technique has potential weaknesses: tokens can be leaked through XSS, SameSite isn't supported in all browsers, custom headers only work for AJAX. By combining them, you ensure that:
- An attacker must defeat all mechanisms simultaneously
- Legacy browsers get at least token-based protection
- Modern browsers benefit from SameSite as backup
- Critical actions have extra human verification
Key Takeaway: Never rely on a single CSRF countermeasure—combine tokens, SameSite cookies, header validation, and re-authentication for critical actions to create overlapping defensive layers that protect even when one mechanism fails.