What is CSRF?
CSRF (Cross-Site Request Forgery) is an attack in which the attacker makes a user's browser perform an action on a site without their knowledge, using an already authorized session.
In simple terms: the site thinks the user is performing the action, but it was actually triggered by the attacker.
How CSRF works in simple terms
- The user is logged in to a site (for example, online banking)
- The session is active (cookies are stored in the browser)
- The user visits a malicious site
- That site quietly sends a request to the trusted site
- The browser automatically attaches the cookies
- The server performs the action, thinking it is the user
The core problem: the site's trust in the user's browser.
What a CSRF attack can do
- change a password;
- change an email address;
- transfer money;
- change account settings;
- perform any action without confirmation.
CSRF does not steal data directly, it forces actions to be performed.
A CSRF example in simple terms
You:
- are logged in to a bank,
- open another site,
- and that site "presses the button" in the bank for you.
How CSRF differs from XSS
Important for the interview.
| Criterion | CSRF | XSS |
|---|---|---|
| Where the code runs | On the attacker's side | In the victim's browser |
| Uses the victim's session | Yes | Yes |
| Script injection | No | Yes |
| Main goal | Perform an action | Steal data |
Which security properties are violated
- Integrity - unauthorized data changes;
- sometimes Confidentiality - indirectly.
How to defend against CSRF
Main defense measures:
- CSRF tokens (the main one);
- checking Origin / Referer;
- SameSite cookies;
- confirming critical actions;
- re-authentication.
A CSRF token is the key defense.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.