mcpField
C
csrf-protection

csrf-protection

github.com/aj-geddesSecurity373474 installsUpdated 34w ago
Repository

About csrf-protection

A skill for implementing Cross-Site Request Forgery defenses with token patterns, cookie attributes, and request-header validation. Its references include Node.js/Express, Flask, and frontend approaches.

Key features

  1. 01Synchronizer token pattern
  2. 02Double-submit cookie pattern
  3. 03SameSite cookie attributes
  4. 04Origin and Referer validation
  5. 05Node.js, Flask, and frontend guides

Use cases

  1. 01Protecting form submissions
  2. 02Securing state-changing requests
  3. 03Hardening authentication flows
  4. 04Protecting payment and account actions

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill csrf-protection

After installing, type/csrf-protectionin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
double-submit-cookie-pattern.md
frontend-csrf-implementation.md
nodejsexpress-csrf-protection.md
origin-and-referer-validation.md
python-flask-csrf-protection.md
scripts/
security-checklist.sh

Details

From the project README

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

Comments

Markdown supported