CSRF
Cross-site request forgery (CSRF) occurs when another website causes your browser to send a request you didn't intend. The target app may act on it because the browser automatically included your session cookie.
Also known as: межсайтовая подделка запроса
The key distinction
A session cookie tells a web app who is signed in. It doesn't tell the app whether that person meant to save a setting, delete a record, or update a profile. Cross-site request forgery (CSRF) takes advantage of that gap: a request initiated outside the app can arrive with credentials the browser attached automatically. The unwanted action can succeed even if the other site never sees the response.
Where the risk comes from
The main question is how the browser supplies credentials. In a cookie-based session, the browser decides whether to attach the cookie based on the destination and the cookie's rules. The server can then authenticate the user and correctly conclude that they have permission to make a change. Neither check establishes that the request came through the app's trusted interface.
For an API that only accepts a bearer token explicitly added to the Authorization header by its client, classic CSRF generally doesn't apply: the browser won't attach that token automatically. But the same token stored in a cookie can still present a CSRF risk. The credential format matters less here than how it gets onto the request.
What a protected endpoint does
Use the web framework's CSRF protection for browser endpoints that rely on session cookies. A common design issues an unpredictable token tied to the session. The app includes it in a form field or request header, and the server rejects a missing or invalid token before applying the change.
For example, a notification settings handler should not save anything until the session, permissions, and CSRF token have all passed validation. A useful defensive test asserts that a request with a valid session but no CSRF token is rejected and leaves the settings unchanged. Also keep GET endpoints read-only so ordinary navigation cannot trigger changes.
What to explain in an interview
Distinguish sending a request from reading its response. CORS controls cross-origin access to responses and requires a preflight for some requests, but it does not prevent every cross-origin request from reaching the server. HttpOnly prevents scripts from reading a cookie; it doesn't stop the browser from sending it.
SameSite is a useful additional layer, but a site and an origin are different concepts. Two subdomains can be same-site while having different origins. If you use an Origin check, compare it against an explicit list of trusted origins and define how to handle a missing header rather than treating its absence as approval.