Vulnerability
Login CSRF
Vulnerability
Most CSRF defenses instinctively focus on actions performed by an already authenticated user.
That makes login CSRF easy to overlook.
In a conventional CSRF attack, the victim is already logged in and the attacker causes their browser to perform some action using the victim's existing authenticated session.
Login CSRF reverses that situation.
The attacker forges a request that logs the victim's browser into an account controlled by the attacker.
Traditional CSRF:
Victim already logged in
↓
Attacker forges a request
↓
Victim's account is modified
Login CSRF:
Victim not necessarily logged in
↓
Attacker forges a login request
↓
Victim's browser becomes logged into the attacker's accountA simplified attack might look like this:
Attacker creates an account
↓
Attacker knows the credentials:
username = attacker@example.com
password = attacker-password
↓
Attacker creates a page that submits those credentials to the real site's
login endpoint
↓
Victim visits attacker-controlled page
↓
Victim's browser submits login request
↓
Target application authenticates: Attacker's account
↓
Victim's browser now holds a session for the attacker's accountFor example, a vulnerable login flow might be targeted with a forged form:
<form action="https://app.example.com/login"
method="POST"
id="login-form">
<input type="hidden"
name="username"
value="attacker@example.com">
<input type="hidden"
name="password"
value="attacker-controlled-password">
</form>
<script>
document.getElementById('login-form').submit();
</script>The important point is that the attacker is not trying to authenticate as the victim.
They are deliberately authenticating the victim's browser as the attacker.
Why This Can Be Dangerous:
The victim may not immediately notice that anything unusual happened.
Depending on how the application behaves, they may begin entering information, creating content, or performing actions while authenticated to the attacker's account.
For example:
Victim thinks:
"I am using my account."
↓
Actually authenticated as: Attacker's account
↓
Victim enters:
Searches
Profile information
Messages
Saved data
Other account-associated content
↓
Data is stored in:
Attacker's account
↓
Attacker may later view that data through their own legitimate accessThe exact impact depends heavily on the application.
Some applications clearly display the active account identity, making the attack easier for the victim to notice. Others may have interfaces where the currently authenticated identity is less obvious, allowing the account confusion to persist longer.
Why Login Endpoints Are Often Missed:
Developers sometimes reason about the login endpoint like this:
CSRF protection is needed because:
An authenticated user's session could be abused
↓
Login endpoint requires no authenticated session
↓
Therefore:
"No CSRF protection needed."But that reasoning misses the actual security property being protected.
A login request changes the browser's security context.
Before request:
No authenticated identity
↓
Login request succeeds
↓
Browser now represents:
Authenticated identity XIf an attacker can choose identity X, that state transition itself can have security consequences.
Defending Against Login CSRF:
The synchronizer-token mechanism from CSRF-Tokens section can be applied to the login form just as it is to other sensitive state-changing requests.
Victim requests legitimate login page
↓
Server provides:
csrf_token=random-value
↓
Login form includes token
↓
Victim submits credentials
↓
Server validates:
Credentials
+
CSRF token
↓
Login succeedsAttacker needs:
username ✓ Known
password ✓ Known
csrf_token ✗ Cannot obtain from the target originThe forged login therefore fails.