Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
aj-geddes avatar

Csrf Protection

  • 427 installs
  • 305 repo stars
  • Updated March 4, 2026
  • aj-geddes/useful-ai-prompts

csrf-protection is an agent skill that helps developers design and verify CSRF defenses using synchronizer tokens, double-submit cookies, SameSite attributes, and Origin validation for state-changing web requests.

About

csrf-protection is an aj-geddes/useful-ai-prompts skill for securing cookie-authenticated forms and state-changing HTTP operations against cross-site request forgery. The quick-start demonstrates a Node.js CSRFProtection class generating crypto.randomBytes(32) hex tokens with one-hour expiry, paired with guidance on csurf middleware integration. Five reference guides cover Node.js and Express CSRF protection, double-submit cookie pattern, Python Flask CSRF protection, frontend token wiring, and Origin or Referer header validation. Best practices require CSRF tokens on all POST, PUT, and DELETE operations, SameSite=Strict cookies, HTTPS-only transport, secure random tokens with expiration, and explicit AJAX header inclusion while warning against skipping protection for authenticated routes or storing tokens in localStorage. Developers reach for csrf-protection when building login forms, account settings, payment actions, or reviewing SPA fetch calls that mutate server state with session cookies.

  • Token generation and validation patterns
  • SameSite and cookie attribute guidance
  • Safe POST, PUT, and DELETE endpoint checks
  • Double-submit and synchronizer token options
  • Framework-specific CSRF middleware setup

Csrf Protection by the numbers

  • 427 all-time installs (skills.sh)
  • Ranked #538 of 2,203 Security skills by installs in the Skillselion catalog
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill csrf-protection

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs427
repo stars305
Last updatedMarch 4, 2026
Repositoryaj-geddes/useful-ai-prompts

How do you secure forms against CSRF attacks?

Design and verify CSRF defenses for cookie-authenticated forms and APIs, including tokens, SameSite settings, and safe state-changing endpoint patterns.

Who is it for?

Web engineers shipping cookie-session authentication who must protect forms, account actions, and payment endpoints from forged cross-site requests.

Skip if: Pure Bearer-token APIs with no browser cookies or form posts, where CSRF token infrastructure adds no meaningful threat reduction.

When should I use this skill?

A developer implements login or payment forms, reviews POST endpoints with session cookies, or asks about CSRF tokens, SameSite, or double-submit cookies.

What you get

CSRF token middleware, double-submit cookie setup, SameSite cookie config, frontend token headers, and Origin validation checklist.

  • CSRF middleware configuration
  • Frontend token header wiring
  • Origin validation checklist

By the numbers

  • Quick-start generates 32-byte CSRF tokens with 1-hour expiry
  • Includes 5 reference guides in the references directory

Files

SKILL.mdMarkdownGitHub ↗

CSRF Protection

Table of Contents

Overview

Implement comprehensive Cross-Site Request Forgery protection using synchronizer tokens, double-submit cookies, SameSite cookie attributes, and custom headers.

When to Use

  • Form submissions
  • State-changing operations
  • Authentication systems
  • Payment processing
  • Account management
  • Any POST/PUT/DELETE requests

Quick Start

Minimal working example:

// csrf-protection.js
const crypto = require("crypto");
const csrf = require("csurf");

class CSRFProtection {
  constructor() {
    this.tokens = new Map();
    this.tokenExpiry = 3600000; // 1 hour
  }

  /**
   * Generate CSRF token
   */
  generateToken() {
    return crypto.randomBytes(32).toString("hex");
  }

  /**
   * Create token for session
   */
  createToken(sessionId) {
    const token = this.generateToken();
    const expiry = Date.now() + this.tokenExpiry;

    this.tokens.set(sessionId, {
// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Node.js/Express CSRF ProtectionNode.js/Express CSRF Protection
Double Submit Cookie PatternDouble Submit Cookie Pattern
Python Flask CSRF ProtectionPython Flask CSRF Protection
Frontend CSRF ImplementationFrontend CSRF Implementation
Origin and Referer ValidationOrigin and Referer Validation

Best Practices

✅ DO

  • Use CSRF tokens for all state-changing operations
  • Set SameSite=Strict on cookies
  • Validate Origin/Referer headers
  • Use secure, random tokens
  • Implement token expiration
  • Use HTTPS only
  • Include tokens in AJAX requests
  • Test CSRF protection

❌ DON'T

  • Skip CSRF for authenticated requests
  • Use GET for state changes
  • Trust Origin header alone
  • Reuse tokens
  • Store tokens in localStorage
  • Allow credentials in CORS without validation

Related skills

How it compares

Use csrf-protection for cookie-session web apps; OAuth-only Bearer APIs without browser cookies rarely need synchronizer-token CSRF flows.

FAQ

What CSRF patterns does csrf-protection document?

csrf-protection covers synchronizer tokens, double-submit cookies, SameSite cookie attributes, and Origin or Referer validation with five framework-specific reference guides for Express, Flask, and frontend AJAX integration.

What token format does the csrf-protection quick-start use?

csrf-protection demonstrates crypto.randomBytes(32) converted to hex strings with a one-hour tokenExpiry stored per session, plus middleware integration guidance for state-changing routes.

Securityappseccompliancesecrets

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.