prevent-csrf
Use when building any server-side endpoint that performs state-changing operations (POST, PUT, PATCH, DELETE) and is accessible by a browser session.
SKILL.mdskills/engineering/security/skills/prevent-csrf/SKILL.md
GitHubPrevent CSRF
Protect state-changing endpoints from cross-site request forgery using synchronizer tokens, SameSite cookies, or origin verification — ensuring requests originate from your own application.
Why This Is Best Practice
Adopted by: Django, Rails, Laravel, Spring Security, and ASP.NET all include CSRF protection by default. OWASP Top 10 2021 (A01:Broken Access Control) covers CSRF. PCI DSS v4.0 Requirement 6.2.4 requires protection against CSRF. GitHub, Google, and Stripe use SameSite cookies as the primary defense.
Impact: CSRF allows attackers to perform unauthorized actions on behalf of authenticated users — fund transfers, email changes, password resets — by tricking them into visiting a malicious page. Twitter, Netflix, and YouTube have all had critical CSRF vulnerabilities. Proper implementation eliminates the entire attack class.
Why best: The Synchronizer Token Pattern and SameSite cookies are complementary defenses that cover different attack vectors. Relying on Referer header alone fails because it can be stripped by browsers or proxies.
Sources: OWASP CSRF Prevention Cheat Sheet; CWE-352; OWASP Top 10 2021
Steps
Set
SameSite=Lax(orStrict) on all session cookies — this is the primary defense for modern browsers.Laxblocks CSRF for POST/PUT/DELETE while allowing cross-site GET navigation (e.g., following links).Strictblocks all cross-site requests including GET.Set-Cookie: session=abc123; SameSite=Lax; Secure; HttpOnly; Path=/Use
Strictwhen your app is not embedded in other sites. UseLaxfor general-purpose apps.Add a Synchronizer CSRF Token for defense-in-depth — generate a random, unpredictable token per session (or per request for high-security actions), store server-side, validate on every state-changing request.
<!-- Include in every HTML form --> <input type="hidden" name="csrf_token" value="{{ csrf_token }}"># On submit: verify token matches session if request.form['csrf_token'] != session['csrf_token']: abort(403)For SPAs using JSON APIs — use the Double Submit Cookie pattern or a custom request header:
- Set a non-
HttpOnlycookie with a random value. - Read it in JavaScript and send it as a custom header (e.g.,
X-CSRF-Token). - Server verifies the header matches the cookie.
- Cross-origin requests cannot set custom headers without CORS preflight, so origin is implicitly verified.
fetch('/api/transfer', { method: 'POST', headers: { 'X-CSRF-Token': getCookie('csrf') }, body: JSON.stringify(data) });- Set a non-
Verify
OriginorRefererheader as a secondary check — reject requests whereOrigindoesn't match your site's origin. Do not use this as the sole defense (it can be absent).allowed_origins = {'https://app.example.com'} if request.headers.get('Origin') not in allowed_origins: abort(403)Apply CSRF protection to all state-changing endpoints — POST, PUT, PATCH, DELETE, and any GET endpoint that performs side effects. Explicitly exempt read-only GET/HEAD/OPTIONS.
Use framework-provided CSRF middleware — don't implement from scratch. Django:
CsrfViewMiddleware(enabled by default). Rails:protect_from_forgery. Spring:CsrfFilter. Laravel:VerifyCsrfToken.
Rules
- Tokens must be unpredictable (cryptographically random, ≥128 bits), not user IDs or sequential values.
- Per-request tokens (regenerated after each state change) are stronger than per-session tokens — use them for high-value actions (fund transfers, account deletion).
- CORS is not a CSRF defense — it controls what cross-origin JS can read, not what it can send.
Common Mistakes
- Using
SameSite=Nonewithout understanding the implications — required for embedded iframes or cross-site widgets, but eliminates SameSite CSRF protection. - Protecting forms but not AJAX endpoints — both require CSRF tokens.
- Storing the CSRF token in
localStorage— fine, but XSS can steal it;HttpOnlycookies protect session tokens but not CSRF tokens (by design).