Login CSRF

A login is a state change. If another site can sign the victim into an account the attacker controls, later activity lands in the attacker’s history.

Lesson 55 of 69 · 14 min read · practice only with authorization

Diagram

From a forged login to someone else’s account history

Follow the path. One example at a time.

Step 1 of 4

Form

The login endpoint accepts a username and a secret and issues a session.

Illustration: the login form is a state change
# A login form issues a session.
# That is a change, so it needs the same kind of check.
What changes the outcome
Request

A login is accepted with only the fields in the body.

Session

The new session is whatever account the sender chose.

Person

They may not notice which account is active.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Login is a state change

    What fails
    Issuing a session is a write. An endpoint that does it without a token can be aimed by another site.
    Who is affected
    The person whose browser is now signed into an account they did not choose.
    What you would notice
    A login route with no CSRF token, on a product that later stores searches, files, or payment methods.
    What you change
    Require the same kind of token you require on other state changes.
  2. Whose data arrives afterward

    What fails
    After a forged login, the person types real secrets and real content into the attacker’s account.
    Who is affected
    The person using the browser, and anyone whose information they paste into that session.
    What you would notice
    Support tickets that start “my history disappeared” after they used a link.
    What you change
    Show the account identity clearly, and reject logins that did not come from your form.
  3. Rotation still matters

    What fails
    A login that reuses a session id the attacker knew is fixation, next door to login CSRF. Both end with a session the person did not create alone.
    Who is affected
    The same person, plus whatever the pre-login session was tied to.
    What you would notice
    The session id is unchanged across the login response.
    What you change
    Regenerate the session id when authentication succeeds, after the token check.
  4. What you would notice

    What fails
    The login handler is missing from the list of routes that check a token, because someone thought “there is no session yet.”
    Who is affected
    People who follow a link and then use the product normally.
    What you would notice
    A test suite that posts credentials with no token and expects a session.
    What you change
    Add the token to the login form and reject the post without it. Keep the failure response generic.

A login writes a session

People describe CSRF as “the user is already signed in.” Login CSRF is the case where the attacker wants them signed in as someone else. The other site submits the attacker’s own credentials to your login endpoint. If you issue a cookie, the victim’s browser keeps it. They look authenticated. The account is not theirs.

Anything they then type — a shipping address, a search, a recovery email, the contents of a draft — is stored where the attacker can open it later. The password check did its job. It checked the attacker’s password. It did not check that the person meant to log in.

The login form needs a token too

Serve the login page with a token bound to a pre-login session, and require that token on the post. Another site can cause a post. It should not be able to read the token from your origin. This is the same control as any other state change, applied to the route teams forget because “nobody is signed in yet.”

Keep the error generic. Do not add a second bug by reflecting the username into an error page without encoding. And do not put the token in the query string.

Rotate the id when login succeeds

If the session id was planted before login and you keep it, you have session fixation as well. Regenerate the id at the moment authentication succeeds, and discard the previous one. The person who knew the old id should not know the new one.

Show who is signed in, in the interface, in a way that is hard to miss. Login CSRF gets worse when the product looks the same regardless of account. That display is not the control. The token is. The display is how a person notices.

What you look for

Include the login route, the SSO callback if it sets a session from a loose parameter, and the “remember this browser” route in the CSRF review. A test that can complete login with a bare post is a failing test, not a fixture to be proud of.

The people affected are whoever uses a browser that is still logged into the attacker’s account, and anyone whose information they enter there. Write that in the finding. The fix is the token plus rotation, not a warning in the terms of service.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

Another site causes the browser to submit a login for an account that site controls. The login endpoint has no CSRF token. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapCross-site request forgery: when the browser’s cookie is the only proofHow another site can make a signed-in browser send a state-changing request, how SameSite cookies changed the picture, where CSRF still appears, and how to verify tokens and Origin checks in a review.
  1. 01CSRF on JSON APIsA JSON API is not immune to cross-site request forgery if the browser will attach the session cookie and the server changes state without a token.

Practice

  1. LabControl benchA failure is described. Choose the control that changes the outcome.

Glossary

  1. CardCross-site request forgeryA browser that is already logged in is tricked into sending a state-changing request the user did not intend.