What is CSRF and Why Frontend Matters
You'll learn what Cross-Site Request Forgery is and why frontend developers play a crucial role in defending against it.
What is CSRF?
Cross-Site Request Forgery (CSRF, pronounced "sea-surf") is an attack where a malicious website tricks your browser into making unwanted requests to a site where you're already authenticated. Unlike XSS which injects malicious code into a trusted site, CSRF exploits the trust a site has in your browser.
The Attack Scenario
Imagine you're logged into your banking app. While still logged in, you visit a malicious site. That site contains hidden code that submits a form to yourbank.com/transfer with instructions to send money elsewhere. Your browser automatically includes your authentication cookies with the request, and boom—unauthorized transaction.
The attacker doesn't see your response or steal your data; they just make your browser perform actions you didn't intend.
Why Frontend Matters
While CSRF protection is often handled server-side (with CSRF tokens), the frontend is where the attack executes. Frontend developers must:
- Properly implement token patterns - Include CSRF tokens in forms and AJAX requests
- Respect SameSite cookie attributes - Understand how
SameSite=StrictorSameSite=Laxprevents cookies from cross-site requests - Avoid dangerous patterns - Don't use GET requests for state-changing operations
- Implement custom headers - APIs can require custom headers (like
X-Requested-With) that simple forms can't forge - Validate origins - Check referrer headers on sensitive actions
Frontend code is your first line of defense against users clicking malicious links or visiting compromised pages while authenticated to your app.
Key Takeaway: CSRF tricks authenticated users into unwittingly performing actions on trusted sites; frontend developers must implement proper token handling and avoid patterns that make these attacks easy to execute.