Cross-Site Request Forgery (CSRF)
# CHAPTER 6
Cross-Site Request Forgery (CSRF)
1. Introduction
In the previous chapters, we protected our application from attackers stealing data. But what if the attacker doesn't want to steal your data; what if they want to *use* your data? Cross-Site Request Forgery (CSRF) is a devious attack where a hacker forces an innocent user's browser to execute unwanted actions on a website where the user is currently authenticated. In this chapter, we will learn how the browser's automatic cookie handling creates this vulnerability and how to implement Anti-CSRF Tokens to stop it.2. Learning Objectives
By the end of this chapter, you will be able to:- Define Cross-Site Request Forgery (CSRF).
- Understand how browsers automatically attach session cookies to requests.
- Analyze the mechanics of a CSRF attack on state-changing actions.
- Implement Anti-CSRF Tokens (Synchronizer Tokens) in web forms.
-
Understand the defense-in-depth provided by the
SameSitecookie attribute.
3. Beginner-Friendly Explanation
Imagine you are at an exclusive club, and the bartender knows you by your face (Your Session Cookie).- You can say to the bartender, "Transfer $100 from my tab to Bob's tab," and the bartender does it because they recognize your face.
- The Exploit (CSRF): A thief walks up behind you, points a gun at your back, and whispers, "Tell the bartender to transfer $100 to the thief's tab." You say the words out loud.
- The bartender looks at your face, recognizes you, and processes the transfer. The bartender doesn't know you were forced to make the request.
CSRF happens when a hacker forces your browser to "say the words" to the bank, and because the bank recognizes your browser's cookie, it processes the malicious request.
4. The Anatomy of a CSRF Attack
For CSRF to work, three things must be true:-
1.
The victim must be logged into the target site (e.g.,
bank.com).
-
2.
The attacker must trick the victim into visiting a malicious site (e.g.,
hacker.com).
-
3.
The targeted action must be predictable (e.g.,
bank.com/transfer?amount=100&to=hacker).
The Attack:
The victim visits hacker.com. The hacker's HTML code contains a hidden image tag:
<img src="https://bank.com/transfer?amount=100&to=hacker" width="0" height="0">
When the browser tries to load the image, it makes a GET request to bank.com. Because the victim is logged into bank.com, the browser *automatically attaches the victim's session cookie*. The bank receives the request, sees the valid cookie, assumes the user genuinely wanted to transfer the money, and processes it.
5. The Primary Defense: Anti-CSRF Tokens
To stop CSRF, the bank must ensure the request actually originated from the bank's own HTML form, not from a hidden image onhacker.com.
We use an Anti-CSRF Token (Synchronizer Token Pattern).
-
1.
When the server loads the legitimate transfer form, it generates a long, cryptographically random, one-time token (e.g.,
8f7b2c...).
- 2. It saves this token in the user's session, and embeds it as a hidden field in the HTML form.
- 3. When the user submits the form, the token is sent back. The server compares the submitted token to the session token.
hacker.com cannot see the bank's HTML page, they cannot guess the random token. Their forced request will be missing the token, and the bank will reject it.
6. Mini Project: Add CSRF Protection to Forms
Let's conceptualize this defense in PHP.Step 1: Generate the Token on the Form Page
Step 2: Validate the Token on the Processing Page
7. Modern Browser Defense: SameSite Cookies
Modern browsers have introduced a powerful defense-in-depth attribute for cookies calledSameSite.
If a developer sets the cookie attribute to SameSite=Lax or SameSite=Strict, the browser will refuse to attach the session cookie if the request originates from a different domain (like hacker.com). This effectively kills traditional CSRF attacks at the browser level, though CSRF tokens are still mandatory for high-security applications.
8. Real-World Scenarios
In 2008, Netflix had a CSRF vulnerability. An attacker could craft a malicious webpage containing hidden requests targeting Netflix's API. If a logged-in Netflix user visited the attacker's page, the hidden request would silently add the attacker's home address to the victim's Netflix account and order DVDs to be shipped to the attacker, all billed to the victim's account. Netflix resolved this by implementing strict anti-CSRF tokens on all state-changing API endpoints.9. Best Practices
-
Never Use GET for State-Changing Actions: An HTTP GET request should only be used to retrieve data. It should never be used to delete a user, transfer money, or change a password. GET requests are trivial to exploit via CSRF (e.g., an
<img>tag). Always use POST, PUT, or DELETE for actions that change state, and protect them with CSRF tokens.
10. Legal and Ethical Notes
CSRF attacks often leave the victim feeling responsible for the action, as the logs show the request originated from the victim's IP address with their valid session. In a forensic investigation, demonstrating the absence of CSRF tokens on critical forms is vital to proving the user was a victim of forgery, not an active participant in the malicious action.11. Exercises
- 1. Explain why Cross-Site Request Forgery (CSRF) is completely dependent on the browser's automatic handling of session cookies.
- 2. How does an Anti-CSRF token prove to the server that the request originated from the legitimate web application rather than an attacker's website?
12. FAQs
Q: Do APIs using JSON Web Tokens (JWTs) need CSRF protection? A: Usually, no. If your API requires the client (like a mobile app or a React frontend) to manually attach the JWT in theAuthorization: Bearer header, you are immune to CSRF. CSRF only works when the browser *automatically* attaches credentials (like Cookies).
13. Interview Questions
- Q: Describe the mechanics of a Cross-Site Request Forgery (CSRF) attack and the standard Synchronizer Token Pattern used to mitigate it.
-
Q: A developer argues that they do not need CSRF tokens because they have configured their session cookies with the
SameSite=Strictattribute. From a defense-in-depth perspective, how do you evaluate this architecture?