Double Submit Cookie Pattern
What you'll learn: How to verify request authenticity by comparing a cookie value with a form parameter, without storing tokens on the server.
The Core Idea
The Double Submit Cookie Pattern is a stateless CSRF defense that works by setting a random value in both a cookie and a request parameter (like a hidden form field or custom header). When the server receives a request, it simply compares these two values—if they match, the request is likely legitimate.
Why This Works
Remember that attackers can't read cookies from your domain due to the Same-Origin Policy (SOP). When an attacker tries to forge a request from their site:
- The victim's browser will automatically send your site's cookies
- But the attacker cannot read that cookie value to copy it into the form parameter
- So the attacker can't make the cookie and parameter match
Analogy: It's like having a secret number written on both your ID card and a separate piece of paper you carry. An attacker might steal your ID card (cookies auto-send), but they can't see the number on it to forge the matching paper.
How It Works
- Server generates a random token (e.g.,
csrf_token=abc123) - Server sets this as a cookie:
Set-Cookie: csrf_token=abc123 - Server also includes it in the form:
<input name="csrf_token" value="abc123"> - On submission, server compares
cookie.csrf_tokenwithbody.csrf_token - Request accepted only if they match
Unlike the Synchronizer Token Pattern, no server-side session storage is required—the cookie itself carries the validation data.
Key Takeaway: Double Submit Cookie Pattern defends against CSRF by requiring attackers to know a cookie's value to forge matching request parameters—something SOP prevents them from doing.