CSRF Explained
When your browser works for the attacker What CSRF is Cross-Site Request Forgery (CSRF) is an attack that makes your own browser send a request you never meant to send, to a site where you are already signed
When your browser works for the attacker
What CSRF is
Cross-Site Request Forgery (CSRF) is an attack that makes your own browser send a request you never meant to send, to a site where you are already signed in. It works because of one simple browser rule: when the browser sends a request to bank.com, it attaches the cookies it holds for bank.com, no matter which page started the request. It could be a tab on bank.com, or a page on evil.com. If the bank recognizes you by a session cookie, every request your browser sends there carries your identity, and the server can't tell "the user clicked a button on our site" from "another site made the browser send this".
Here is the classic example. You are signed in to your bank, and in another tab you open a page on evil.com. That page contains a hidden form pointing to https://bank.com/transfer, with the fields to=attacker and amount=1000, plus one line of JavaScript that submits it as soon as the page loads. Your browser sends the POST with your bank cookie, the bank sees a valid session, and the money moves. The attacker never saw your password or your cookie, and never read the bank's answer. They didn't need to: the goal was the action itself, not the data. That is also why CORS doesn't help. CORS only decides whether a page may read a response from another site; the forged request has already been sent and processed.
There is also an inverted version, called login CSRF: instead of acting inside your account, the attacker signs you into theirs, without you noticing. It sounds harmless until you think about what people do after signing in: save a credit card, upload a document, write notes. All of it lands in an account the attacker controls and can read later. This variant matters a lot for "Sign in withβ¦" buttons, which brings us to OAuth.
CSRF in OAuth
In OAuth 2.0 / OpenID Connect sign-in, the app sends you to the identity provider (Google, Microsoft, your company's login server). After you log in there, the provider redirects your browser back to the app with a one-time authorization code in the URL:
https://app.com/callback?code=ABC
The app exchanges that code for tokens and signs you in. The attack goes like this:
- The attacker starts a normal sign-in on their own machine, with their own account.
- At the last step they stop the browser from following the redirect and copy the URL, with the code still unused:
https://app.com/callback?code=ABC. - They get you to open that URL: a link, an image tag, a hidden iframe.
- The app exchanges
ABCand signs you in as the attacker.
This isn't hypothetical; the OAuth 2.0 spec itself describes it (RFC 6749, Β§10.12):
"A CSRF attack against the client's redirection URI allows an attacker to inject its own authorization code or access token, which can result in the client using an access token associated with the attacker's protected resources rather than the victim's (e.g., save the victim's bank account information to a protected resource controlled by the attacker)."
How to protect OAuth sign-in
The examples below use the simplest setup: a single-page app that does the whole sign-in itself, running in your browser. In OAuth terms that application is the client; the browser is only the user-agent that runs it (RFC 6749, Β§2.1).
The state parameter. Before sending the browser to the identity provider, the app generates a random value (crypto.getRandomValues), saves it in sessionStorage, and puts it in the authorization request. The provider sends it back unchanged on the callback. The callback page compares it with the saved one and stops if they differ.
How this stops the attack, step by step:
- The attacker starts the sign-in in their browser. The app there saves a random
state, sayX7fβ¦, in the attacker'ssessionStorage, and the provider later redirects tohttps://app.com/callback?code=ABC&state=X7fβ¦. - The attacker copies that URL and gets you to open it.
- The callback page now runs in your browser and looks in your
sessionStoragefor the savedstate. There is none (you never started a sign-in), or there is a different one (your own sign-in). Either way it doesn't matchX7fβ¦. - The app stops right there. It never sends
ABCto the provider, so you are never signed in to the attacker's account.
The attacker can't get around this by planting X7f⦠in your browser: sessionStorage lives in your browser, which keeps a separate box for each site. Only pages from app.com can read or write app.com's box, so a page from evil.com can't plant a value there (the browser's same-origin policy). And they can't guess the value you saved, because it is random and new on every sign-in.
state closes the CSRF hole in OAuth sign-in, but it is not the only thing that can go wrong in this flow: a stolen authorization code, a replayed token, a redirect sent to the wrong place. Those are different attacks with different fixes, and they are the subject of the next article.
References
- RFC 6749, The OAuth 2.0 Authorization Framework, Β§2.1 (client types) and Β§10.12 (CSRF).
- A. Barth, C. Jackson, J. C. Mitchell, Robust Defenses for Cross-Site Request Forgery, ACM CCS 2008.
- OWASP, Cross-Site Request Forgery Prevention Cheat Sheet.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.