csrf-protection

Implement CSRF protection with token validation and header checks for Next.js and FastAPI.

1|Updated Sep 14, 2025
One-click install
npx skills add https://github.com/CleanExpo/DR-NRPG --skill csrf-protection-cleanexpo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csrf-protection
Source: https://github.com/CleanExpo/DR-NRPG/tree/main/.skills/custom/csrf-protection
Command: npx skills add https://github.com/CleanExpo/DR-NRPG --skill csrf-protection-cleanexpo

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill prevents Cross-Site Request Forgery (CSRF) attacks, a common vulnerability where attackers trick users into performing unwanted actions on web applications they are authenticated to.

Core Features & Use Cases

  • Token-Based Protection: Implements synchroniser tokens and the double-submit cookie strategy.
  • Header & Cookie Validation: Enforces SameSite cookie attributes and validates Origin/Referer headers.
  • Use Case: Protects all state-changing API endpoints and form submissions in your Next.js or FastAPI application, ensuring that requests originate from your own application and are authorized by the user.

Quick Start

Apply the csrf-protection skill to secure your FastAPI application's API routes.

Frequently Asked Questions about csrf-protection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement CSRF protection for my FastAPI application?

To implement CSRF protection in your FastAPI application, use synchroniser token generation and the double-submit cookie strategy to validate state-changing API requests. This ensures malicious sites cannot forge authorized submissions.

What is the double-submit cookie pattern for CSRF prevention?

The double-submit cookie pattern for CSRF prevention requires setting a unique token in a cookie and requiring that same token in the request body or header. Matching both tokens validates the request originates from your own web application.

How do I secure Next.js API routes from CSRF attacks?

Secure Next.js API routes from CSRF attacks by validating Origin and Referer headers to verify request sources. Additionally, enforce SameSite cookie attributes and implement token validation for all state-changing operations.

Does SameSite cookie configuration prevent CSRF vulnerabilities?

SameSite cookie configuration prevents CSRF vulnerabilities by restricting cookies from being sent with cross-site requests. Setting the attribute to Strict or Lax stops browsers from including authentication cookies in unauthorized cross-origin form submissions.

When do I need Origin and Referer header checks for web security?

You need Origin and Referer header checks for web security when protecting endpoints from Cross-Site Request Forgery. These checks verify that incoming HTTP requests originate from your own domain, blocking forged submissions from malicious sites.

Can I use synchroniser tokens to protect form submissions in FastAPI?

Yes, you can use synchroniser tokens to protect form submissions in FastAPI. This technique generates a unique unpredictable token per session or request, validating it on the server to ensure the form was submitted intentionally by the authenticated user.