Concept
Synchronizer Tokens
The synchronizer token pattern is one of the foundational defenses against CSRF.
The basic idea is simple: the application generates a random, unpredictable value and gives it to the legitimate page, typically by embedding it in a hidden form field. The server maintains enough state or otherwise uses a validation mechanism to verify that the submitted token is the one associated with the user's session or request.
Every protected state-changing request must include that token.
User requests legitimate page
β
Server generates or retrieves a CSRF token associated with
that authenticated context
β
Token embedded in the page
β
User submits form
β
Token returned with request
β
Server validates token
βββ Valid β Process request
βββ Missing / Invalid β Reject requestFor example:
<form action="/api/account/password" method="POST">
<input type="hidden"
name="csrf_token"
value="a9f3e7c1b2d84f...">
<input type="password"
name="newPassword">
</form>Why the Attacker Cannot Simply Copy the Token:
This is the part that connects directly back to Same-Origin Policy and ambient authority from section what is csrf
An attacker-controlled page may be able to cause the victim's browser to send a request to the target application, andβif the applicable cookie rules permit itβthe browser may automatically attach the victim's session cookie.
But the attacker still needs the matching CSRF token.
Attacker controls:
evil.example.com
β
Can construct:POST /api/account/password
newPassword=attacker-choice
β
Browser may provide:session=victim-session
β
But attacker also needs: csrf_token=???The attacker cannot simply fetch the legitimate form and read its token:
fetch("https://app.example.com/account/password")Even if the browser sends a request in some circumstances, JavaScript running on evil.example.com cannot normally read the response body from app.example.com unless the target explicitly permits that cross-origin access.
The attacker therefore cannot extract:
<input type="hidden"
name="csrf_token"
value="a9f3e7c1b2d84f...">from the legitimate page.
This creates the critical asymmetry behind the defense:
Attacker can control:
β Destination
β HTTP method
β Request parameters
β Request timing
Browser may provide:
β Victim's session cookie when applicable
Attacker cannot obtain: Valid CSRF tokenThe forged request therefore becomes:
Authenticated?
Possibly yes.
β
CSRF token valid? No.
β
Request rejectedCSRF exists because authentication cookies can act as ambient authority: the browser may automatically provide the credential when a request is sent to the appropriate destination.
The synchronizer token adds something that is not ambient.
Session cookie
Automatically supplied by browser when applicable
+
CSRF token
Must be explicitly included and validated
β
Authenticated request with proof tied to the legitimate
application contextThe central principle is:
A cross-site attacker may be able to make the victim's browser send an authenticated request, but they cannot complete that request successfully if the server requires an additional unpredictable value that the attacker cannot read, predict, or otherwise obtain.