CSRF in Modern Applications and SPAs
You'll learn how CSRF attacks have evolved beyond traditional form submissions to target APIs, single-page applications, and modern authentication patterns.
The Modern CSRF Landscape
Traditional CSRF attacks exploited cookie-based authentication with HTML forms. But modern applications—especially single-page applications (SPAs)—work differently. They communicate with APIs using JavaScript, often relying on stateless authentication like JWT tokens stored in localStorage or sessionStorage instead of cookies.
Here's the twist: where you store tokens determines CSRF risk.
SPAs and Token Storage
If your SPA stores tokens in localStorage and sends them via an Authorization header, you're protected from CSRF (but vulnerable to XSS). Cookies aren't automatically sent with API requests, so an attacker's malicious site can't trigger authenticated actions.
However, if your SPA still uses cookies for authentication (even with an API backend), you're still vulnerable. The browser automatically includes cookies with fetch/AJAX requests to the same domain, just like traditional forms.
Modern Attack Vectors
CSRF in SPAs can happen through:
- API endpoints that accept JSON but also respect cookies
- CORS misconfigurations that allow malicious origins to make credentialed requests
- Mobile apps with embedded webviews using cookie-based sessions
- GraphQL mutations that rely on cookie authentication
- Hybrid apps mixing cookie sessions with token-based APIs
The Authentication Pattern Matters
Modern patterns like OAuth 2.0 with PKCE and state parameters provide CSRF protection during login, but the application itself might still be vulnerable if it uses cookie sessions afterward.
Key Takeaway: CSRF isn't obsolete—it's evolved. Modern apps using cookie-based authentication remain vulnerable regardless of whether they're SPAs or use JSON APIs. Token-based auth in headers prevents CSRF but shifts risk to XSS.