BeCuriouzLearn something new every day
Your Browser Can Be Tricked Into Acting for You: How CSRF Works
💡 CSRF works because the browser attaches your credentials to a request without asking you, so defenses make the server ask for proof the request came from its own page.
Say you're logged into your bank in one tab, and in another tab you open a random web page. That page could hold a hidden form that quietly submits to bank.com/transfer. This attack is called Cross-Site Request Forgery (CSRF). It works because browsers automatically attach cookies to any request sent to a site, no matter which page started the request. The bank sees your valid session cookie and assumes you meant to make the transfer.
The attacker never steals your password or your cookie, and never sees the bank's response either. They don't need to. They only need your browser to send one request that changes something, like a transfer, an email change or a password reset. The server can't tell a click you made on purpose from a request a hostile page set off, because both carry the same credentials.
There are two main defenses. A CSRF token is a random secret that the site puts inside its own forms and checks on every submission. The same-origin policy stops other sites from reading your bank's pages, so the attacker can't learn the token and can't build a request that passes the check. SameSite cookies tell the browser not to send a cookie on requests that come from other sites. Chrome now treats cookies as SameSite=Lax by default. Lax still sends the cookie when you follow a link to the site (a top-level GET navigation), so it only protects you if GET requests never change anything.