Communitygithub.com

CSRF保护

实施同站点Cookie、双重提交模式及令牌验证,防御跨站请求伪造攻击。

CSRF保护 是什么?

该技能专注于为Web应用提供全面的跨站请求伪造(CSRF)防护方案。它详细说明了如何通过设置`SameSite=Lax`和`SameSite=Strict`属性来保护Cookie,从而自动阻止大多数CSRF攻击,并对账户删除、密码更改等高敏感操作提供更严格的保护。技能还指导开发者实现双重提交Cookie模式,即在Cookie和请求头中同时携带CSRF令牌进行验证,并建议在状态变更请求中检查`Origin`和`Referer`头作为深度防御。对于现代框架,它指出Next.js Server Actions和SvelteKit表单操作已内置CSRF处理,而单页应用(SPA)则可通过自定义请求头(如`X-CSRF-Token`)来防范攻击。技能附有TypeScript代码示例,展示如何生成和验证CSRF令牌,并明确警告不要仅依赖基于Cookie的身份验证而不加CSRF保护、不要使用永不轮换的令牌、不要通过GET请求改变状态,以及不要因为“只是API”就禁用CSRF防护。该技能适用于Node.js/Express应用、Next.js、SvelteKit等现代JavaScript框架,也适用于任何使用Cookie进行身份验证的API。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/csrf-protection

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

CSRF 保护

规则

  • 在所有 Cookie 上设置 SameSite=Lax——默认即可阻止大多数 CSRF 攻击
  • 针对高度敏感的操作(如删除账户、更改密码)使用 SameSite=Strict
  • 对自定义 API 实施双重提交 Cookie 模式:CSRF 令牌同时存在于 Cookie 和请求头中
  • 在所有执行状态变更操作的 HTML 表单中包含 CSRF 令牌
  • 在状态变更请求上验证 Origin 和 Referer 请求头,作为深度防御
  • Next.js Server Actions 和 SvelteKit 表单操作会自动处理 CSRF —— 请使用它们
  • 对于单页应用(SPA):在自定义请求头(X-CSRF-Token)中发送 CSRF 令牌——浏览器默认会阻止跨域自定义请求头
// 双重提交 Cookie 模式
import crypto from "crypto";

function generateCsrfToken(res) {
  const token = crypto.randomBytes(32).toString("hex");
  res.cookie("csrf_token", token, { httpOnly: false, sameSite: "strict", secure: true });
  return token;
}

function verifyCsrf(req) {
  const cookieToken = req.cookies.csrf_token;
  const headerToken = req.headers["x-csrf-token"];
  if (!cookieToken || cookieToken !== headerToken) {
    throw new Error("CSRF validation failed");
  }
}

避免

  • 仅依赖基于 Cookie 的身份验证而没有 CSRF 保护——任何站点都可以向你的端点提交表单
  • 使用永不轮换的 CSRF 令牌——一个令牌泄露将危及所有未来的请求
  • 使用 GET 请求改变状态——CSRF 保护仅对 POST/PUT/DELETE 有效,因为 GET 请求会绕过 SameSite=Lax
  • 因为“它只是一个 API”而禁用 CSRF 保护——如果涉及 Cookie,CSRF 就适用

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能