---
name: "csrf-token-samesite-cookies"
description: "Prevent Cross-Site Request Forgery with SameSite cookies and cryptographically secure tokens."
metadata:
  author: "OWASP"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "prevent csrf attack"
  - "configure samesite cookies"
  - "implement csrf token middleware"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# CSRF Prevention with Double-Submit & SameSite Cookies

Implements robust CSRF defenses: configuring cookies with SameSite=Lax/Strict, generating cryptographically random HMAC CSRF tokens, and verifying custom request headers.

## When to Invoke
Invoke this skill whenever you need to:
- prevent csrf attack
- configure samesite cookies
- implement csrf token middleware

## Execution SOP & Rules
1. Set SameSite=Lax or SameSite=Strict on all authentication and session cookies.
2. Require HttpOnly and Secure flags on session cookies to prevent JavaScript theft.
3. Validate cryptographically secure CSRF tokens on all state-changing POST, PUT, and DELETE requests.
4. Verify the Origin or Referer header matches the expected application domain on mutations.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
