Defend a Web Form Against Cross-Site Request Forgery

8 steps · about 3 h 10 min · Intermediate

  1. Understand the CSRF attack (guide, OWASP, 20 min)

    Model how a browser can attach ambient credentials to an attacker-triggered request.

  2. 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.

  3. Set SameSite deliberately (reference, MDN Web Docs, 20 min)

    Use cookie context restrictions as defence in depth without breaking required flows.

  4. Validate request origin (reference, MDN Web Docs, 15 min)

    Use the Origin signal to reject unexpected cross-site state-changing requests.

  5. Add Fetch Metadata checks (reference, MDN Web Docs, 20 min)

    Distinguish same-origin, same-site and cross-site requests at the server boundary.

  6. Build a resource-isolation policy (guide, web.dev, 25 min)

    Use Fetch Metadata as a compatible extra barrier while defining explicit exceptions.

  7. Keep CORS separate from CSRF (reference, MDN Web Docs, 25 min)

    Understand why cross-origin read controls do not by themselves prevent credentialed writes.

  8. Test the complete defence (guide, OWASP WSTG, 30 min)

    Attempt cross-site submissions, missing and invalid tokens, and legitimate retries before release.