Back to 🚀 Production Readiness

Secure API from CSRF

SecurityCSRFAPI
---
description: Prevent CSRF attacks
---

1. **Use SameSite Cookies**:
response.headers.set('Set-Cookie', 'token=abc; SameSite=Strict; HttpOnly');


2. **Implement CSRF Tokens**:
import { randomBytes } from 'crypto';
export function generateCSRFToken() {
return randomBytes(32).toString('hex');
}


3. **Validate Origin**:
const origin = request.headers.get('origin');
if (!allowedOrigins.includes(origin)) {
return Response.json({ error: 'Invalid origin' }, { status: 403 });
}


4. **Pro Tips**:
- Never use * in production.
- Validate both token and origin.
By Antigravity Team

How to Use This Workflow

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/workflows/
  3. Save the file as secure-api-csrf-protection-samesite-cookies.md
  4. In Antigravity, type /secure_api_csrf_protection_samesite_cookies or just describe what you want to do

Learn more about workflows →

Related Workflows

Recommended Rules

View more rules →

Recommended MCP Servers

View more MCP servers →

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide