CSRF Protection

CSRF Protection

Guides an AI agent to defend web apps against cross-site request forgery with synchronizer tokens, double-submit cookies, SameSite attributes and origin checks.

Skill files7 files

references/

scripts/

SKILL.md98 lines
1
---
2
name: csrf-protection
3
description: >
4
Implement Cross-Site Request Forgery (CSRF) protection using tokens, SameSite
5
cookies, and origin validation. Use when building forms and state-changing
6
operations.
7
---
8
9
# CSRF Protection
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
Implement comprehensive Cross-Site Request Forgery protection using synchronizer tokens, double-submit cookies, SameSite cookie attributes, and custom headers.
22
23
## When to Use
24
25
- Form submissions
26
- State-changing operations
27
- Authentication systems
28
- Payment processing
29
- Account management
30
- Any POST/PUT/DELETE requests
31
32
## Quick Start
33
34
Minimal working example:
35
36
```javascript
37
// csrf-protection.js
38
const crypto = require("crypto");
39
const csrf = require("csurf");
40
41
class CSRFProtection {
42
constructor() {
43
this.tokens = new Map();
44
this.tokenExpiry = 3600000; // 1 hour
45
}
46
47
/**
48
* Generate CSRF token
49
*/
50
generateToken() {
51
return crypto.randomBytes(32).toString("hex");
52
}
53
54
/**
55
* Create token for session
56
*/
57
createToken(sessionId) {
58
const token = this.generateToken();
59
const expiry = Date.now() + this.tokenExpiry;
60
61
this.tokens.set(sessionId, {
62
// ... (see reference guides for full implementation)
63
```
64
65
## Reference Guides
66
67
Detailed implementations in the `references/` directory:
68
69
| Guide | Contents |
70
|---|---|
71
| [Node.js/Express CSRF Protection](references/nodejsexpress-csrf-protection.md) | Node.js/Express CSRF Protection |
72
| [Double Submit Cookie Pattern](references/double-submit-cookie-pattern.md) | Double Submit Cookie Pattern |
73
| [Python Flask CSRF Protection](references/python-flask-csrf-protection.md) | Python Flask CSRF Protection |
74
| [Frontend CSRF Implementation](references/frontend-csrf-implementation.md) | Frontend CSRF Implementation |
75
| [Origin and Referer Validation](references/origin-and-referer-validation.md) | Origin and Referer Validation |
76
77
## Best Practices
78
79
### ✅ DO
80
81
- Use CSRF tokens for all state-changing operations
82
- Set SameSite=Strict on cookies
83
- Validate Origin/Referer headers
84
- Use secure, random tokens
85
- Implement token expiration
86
- Use HTTPS only
87
- Include tokens in AJAX requests
88
- Test CSRF protection
89
90
### ❌ DON'T
91
92
- Skip CSRF for authenticated requests
93
- Use GET for state changes
94
- Trust Origin header alone
95
- Reuse tokens
96
- Store tokens in localStorage
97
- Allow credentials in CORS without validation
98

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 11,977 prompts

More by @OpenSourceBuffGuy

See all
Black ink doodle portrait with floral border prompt image

Black ink doodle portrait with floral border

Turns a reference photo into a whimsical black ink doodle portrait with a floral doodle border, keeping likeness and expression.

image-prompt, line-art, ink-drawing, doodle, sketch, portrait, face-reference, reference-image, style-transfer, nano-banana, black-and-white, flowers, whimsical, illustration, drawing, avatar, prompt-weights, limited-palette, vertical-format, for-artists, quick

Pixar 3D Character Design Sheet prompt image

Pixar 3D Character Design Sheet

Makes a Pixar-style 3D character sheet pitting a pole vaulter against his vault, with poses, stat bars, a title and a cinematic close-up strip.

image-prompt, gpt-image, pixar-style, 3d-render, character-design, character-sheet, character-consistency, multi-panel, white-background, text-rendering, sports, cartoon, vibrant-colors, cinematic, close-up, man, poster, structured-output, for-artists, for-designers, long-form, humorous

Java Pro prompt image

Java Pro

Makes the AI a Java 21+ expert for modern language features, virtual threads, Spring Boot 3 and cloud-native microservices.

sub-agent, act-as, java, spring-boot, backend, microservices, performance-optimization, best-practices, code, for-developers, unit-testing, cloud, docker, observability, expert-advice, design-patterns, long-form, advanced, kubernetes

Reviews

Loading reviews…

Discussion

Loading comments…