CSRF token flow
Follow a CSRF token from form to server check, then pick SameSite and server checks until four forged requests fail and your own three still work.
- Interactive lab
- Intermediate
- 5 graded steps
- 0 of 5 solved
Your goal
Your goalChoose a cookie setting and server checks so all four forged requests fail and your own three still work.
bank.example keeps you signed in with a session cookie. Any site you visit can make your browser send a request to the bank, and the browser decides whether your cookie goes with it.
Seven requests reach the bank: three are yours and four are forged by other pages. Pick the SameSite setting and the server checks until every forged request fails and yours still work.
What’s on screen
- Session cookie SameSite
- three buttons on the left: None, Lax, and Strict. They decide when the browser sends your bank cookie to the bank.
- Server
- three switches: require the token, require the Origin header, and whether a plain link can move money.
- How the token travels
- four boxes showing the hidden token going from the form to the server check.
- Requests
- the table on the right: seven requests, marked Yours or Forged, with whether the cookie and token went and the result.
- Walkthrough
- the numbered path beside the workbench (below it on a phone). Each step says what to do and what you should see. Hints open one at a time.
Words to know
- CSRF
- cross-site request forgery, tricking a browser into sending a request
Session cookie SameSite
Server
- 1
GET /transferThe server renders the form and puts a token tied to this session in a hidden field. - 2
<input type="hidden" name="csrf" value="lab-only-csrf-7f3a9c">Only pages from bank.example can read this value. The same-origin policy stops other sites from reading the response. - 3
POST /transfer csrf=lab-only-csrf-7f3a9cThe browser sends the session cookie automatically. The token is sent because the page put it in the form. - 4
Server compares the token with the one stored for the sessionA match proves the request came from a page the bank rendered, not just from a browser that has the cookie.
| Request | From | Cookie | Token | Result |
|---|---|---|---|---|
| YoursYou submit the transfer form on bank.example | POST /transferbank.example (same-origin) | sent | valid | performed The server performed the action. |
| YoursThe bank’s own page calls the API with the token in a header | POST /api/payeesbank.example (same-origin) | sent | valid | performed The server performed the action. |
| YoursYou click a link in the bank’s newsletter (sent from mail.example) to view your statements | GET /statementsmail.example (cross-site) | sent | none | page shown Read-only page shown with the session. |
| ForgedA page on prizes.example auto-submits a hidden form to /transfer | POST /transferprizes.example (cross-site) | sent | none | performed The server performed the action. |
| ForgedA forum post links to /transfer?to=lab-only-acct&amount=900 | GET /transferforum.example (cross-site) | sent | none | performed CSRF middleware skips safe methods, so this GET performed the transfer. |
| ForgedA compromised page on blog.bank.example submits a form to /transfer | POST /transferblog.bank.example (same-site) | sent | none | performed The server performed the action. |
| Forgedprizes.example submits the form with a guessed token | POST /transferprizes.example (cross-site) | sent | wrong value | performed The server performed the action. |
More in Web and API security
The whole walkthrough
Every step in order: 300 words, about 2 min. It gives away every answer, so try the steps first.