CSRF (Cross-Site Request Forgery)
What is it?β
Cross-Site Request Forgery (CSRF) occurs when an attacker tricks an authenticated userβs browser into making unintended requests to an API, leveraging the userβs existing session or credentials.
This vulnerability primarily affects APIs that rely on browser-based authentication mechanisms, such as cookies, where credentials are automatically included in requests.
Since the API cannot distinguish between legitimate user actions and forged requests, attackers can perform sensitive operations on behalf of the user.
CSRF is particularly critical for state-changing operations and can lead to unauthorized transactions, account modifications, or privilege abuse.
What are specific examples?β
- A malicious website triggering a
POST /transferrequest using the victimβs active session - APIs accepting state-changing requests without CSRF tokens
- Cookie-based authentication without
SameSiteprotections - Missing validation of
OriginorRefererheaders - Sensitive actions exposed via browser-accessible endpoints
- CSRF protections implemented only on frontend, not enforced at API level
Test case FAQsβ
When is this test case applicable?β
- APIs using cookie-based authentication
- Browser-accessible endpoints
- Endpoints performing state-changing operations (
POST,PUT,DELETE) - Applications supporting cross-origin interactions
How does it work?β
Prerequisitesβ
- Authenticated session (typically via cookies)
- Ability to simulate cross-origin requests
- Identification of state-changing endpoints
Test sequenceβ
- Identify endpoints relying on cookie-based authentication
- Send forged cross-origin requests without CSRF tokens
- Replay requests with missing or invalid:
- CSRF tokens
- Origin/Referer headers
- Analyze whether the request is accepted and executed
- Validate if sensitive state changes occur without user intent
Success/Failure indicationsβ
-
Failure (vulnerable):
- State-changing request succeeds without CSRF token
- API accepts requests from untrusted origins
- Missing or ignored Origin/Referer validation
-
Success (secure):
- Requests rejected without valid CSRF token
- Origin/Referer properly validated
- SameSite cookie protections enforced
What is the solution?β
- Implement anti-CSRF tokens for all state-changing operations
- Enforce SameSite cookie attributes (
StrictorLax) - Validate Origin and Referer headers
- Prefer token-based authentication (Authorization headers) over cookies
- Avoid exposing sensitive operations to browser contexts when unnecessary
- Ensure CSRF protections are enforced server-side
Referencesβ
Was this page helpful?