State-Changing Requests and CSRF Risk
You'll learn which types of HTTP operations in your frontend are vulnerable to CSRF attacks and why.
Not all HTTP requests pose the same CSRF risk. Understanding which operations are vulnerable is essential to protecting your application effectively.
What Makes a Request CSRF-Vulnerable?
A request is vulnerable to CSRF if it changes state on the server and relies solely on browser-attached credentials (like cookies). State-changing operations include:
- POST, PUT, PATCH, DELETE requests that modify data
- Form submissions that update user profiles
- API calls that transfer funds or change permissions
- Actions that create, update, or delete resources
Safe vs Unsafe Methods
Safe methods like GET and HEAD should only read data, never modify it. If implemented correctly, they're not CSRF targets because an attacker reading your data doesn't change anything server-side.
Unsafe methods (POST, PUT, PATCH, DELETE) are CSRF-vulnerable because they perform actions. Even if an attacker can't see the response, they've already triggered the harmful operation.
The Cookie Credential Problem
// CSRF-vulnerable: cookies attach automatically
fetch('/api/transfer-money', {
method: 'POST',
body: JSON.stringify({ to: 'attacker', amount: 1000 })
// Session cookie attaches automatically!
});
If this request uses cookie-based authentication, a malicious site can trigger it and your browser will dutifully attach your credentials.
Think Like This
Imagine a button in your house (the browser) that automatically hands out your ID card (cookie) to anyone who knocks (any website). Safe if they're just asking for directions (GET), dangerous if they're withdrawing money (POST).
Key Takeaway: State-changing operations using POST, PUT, PATCH, or DELETE are CSRF-vulnerable when authenticated via cookies—these are the requests you must protect with CSRF tokens or alternative authentication methods.