Communitygithub.com

YepAPI/技能

CSRF 防護 — SameSite Cookie、雙重提交模式、Token 驗證。

技能 是什麼?

這項技能提供了一個全面的跨站請求偽造(CSRF)防護實現指南,適用於現代 Web 應用程式。它詳細說明了透過設定 `SameSite=Lax` 和 `SameSite=Strict` Cookie 屬性來防禦大部分 CSRF 攻擊,並針對自訂 API 實作雙重提交 Cookie 模式,要求將 CSRF Token 同時放在 Cookie 和請求標頭中進行比對。該指南還強調在所有執行狀態變更操作的 HTML 表單中包含 CSRF Token,並驗證請求的 `Origin` 和 `Referer` 標頭作為深度防禦。技能支援 Next.js、SvelteKit 等框架,並提供適用於 SPA 的 `X-CSRF-Token` 自訂標頭方案,因為瀏覽器預設會阻止跨來源的自訂標頭。此外,它也明確指出了一些常見的錯誤觀念,例如僅依賴 Cookie-based 認證、使用永不輪換的 Token,或讓 GET 請求變更狀態。

相容平台~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 Token 放在 Cookie 和請求標頭中
  • 在所有執行狀態變更操作的 HTML 表單中包含 CSRF Token
  • 驗證狀態變更請求的 Origin 和 Referer 標頭,作為深度防禦
  • Next.js Server Actions 和 SvelteKit form actions 會自動處理 CSRF — 請使用它們
  • 對於 SPA:在自訂標頭 (X-CSRF-Token) 中發送 CSRF Token — 瀏覽器預設會阻擋跨來源的自訂標頭
// 雙重提交 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 驗證失敗");
  }
}

避免

  • 僅依賴 Cookie-based 認證而沒有 CSRF 防護 — 任何網站都可以向你的端點提交表單
  • 永不輪換的 CSRF Token — 一個 Token 洩漏就會危及所有未來請求
  • 讓 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.

相關技能