Defend a Web Form Against Cross-Site Request Forgery
8 steps · about 3 h 10 min · Intermediate
- Understand the CSRF attack (guide, OWASP, 20 min)
Model how a browser can attach ambient credentials to an attacker-triggered request.
- Choose a primary defence (guide, OWASP Cheat Sheet Series, 35 min)
Select synchronizer tokens or a signed double-submit pattern that fits the application architecture.
- Set SameSite deliberately (reference, MDN Web Docs, 20 min)
Use cookie context restrictions as defence in depth without breaking required flows.
- Validate request origin (reference, MDN Web Docs, 15 min)
Use the Origin signal to reject unexpected cross-site state-changing requests.
- Add Fetch Metadata checks (reference, MDN Web Docs, 20 min)
Distinguish same-origin, same-site and cross-site requests at the server boundary.
- Build a resource-isolation policy (guide, web.dev, 25 min)
Use Fetch Metadata as a compatible extra barrier while defining explicit exceptions.
- Keep CORS separate from CSRF (reference, MDN Web Docs, 25 min)
Understand why cross-origin read controls do not by themselves prevent credentialed writes.
- Test the complete defence (guide, OWASP WSTG, 30 min)
Attempt cross-site submissions, missing and invalid tokens, and legitimate retries before release.