Communitygithub.com

YepAPI/skills

CSRF 보호 — SameSite 쿠키, 이중 제출 패턴, 토큰 검증.

skills란 무엇인가요?

이 스킬은 CSRF(Cross-Site Request Forgery) 공격으로부터 웹 애플리케이션을 보호하기 위한 규칙과 코드 예제를 제공합니다. SameSite 쿠키 속성(Lax/Strict)을 활용한 기본 방어, 더블 서브밋 쿠키 패턴을 통한 사용자 정의 API 보호, 폼과 SPA에서의 CSRF 토큰 구현 방법을 다룹니다. Next.js Server Actions나 SvelteKit form actions처럼 CSRF를 자동 처리하는 프레임워크를 권장하며, 방어 심층화를 위해 Origin/Referer 헤더 검증도 포함합니다. Node.js/Express 환경에서 동작하는 코드 예제와 함께, CSRF 보호를 우회하거나 무력화하는 흔한 실수를 피하는 방법도 설명합니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/csrf-protection

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

CSRF 보호

규칙

  • 모든 쿠키에 SameSite=Lax 설정 — 기본적으로 대부분의 CSRF 공격 차단
  • 매우 민감한 작업(계정 삭제, 비밀번호 변경)에는 SameSite=Strict 사용
  • 사용자 정의 API에 이중 제출 쿠키 패턴 구현: 쿠키 속 CSRF 토큰 + 요청 헤더
  • 상태 변경을 수행하는 모든 HTML 폼에 CSRF 토큰 포함
  • 상태 변경 요청 시 Origin 및 Referer 헤더 검증 — 방어 심층화
  • Next.js Server Actions와 SvelteKit form actions는 CSRF를 자동 처리함 — 사용 권장
  • SPA의 경우: CSRF 토큰을 사용자 정의 헤더(X-CSRF-Token)로 전송 — 브라우저는 기본적으로 교차 출처 사용자 정의 헤더 차단
// 이중 제출 쿠키 패턴
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 검증 실패");
  }
}

피해야 할 것

  • CSRF 보호 없이 쿠키 기반 인증만 사용하는 것 — 어떤 사이트든 엔드포인트에 폼을 제출할 수 있음
  • CSRF 토큰을 절대 갱신하지 않는 것 — 하나의 토큰이 유출되면 모든 미래 요청이 위험해짐
  • 상태를 변경하는 GET 요청 — CSRF 보호는 POST/PUT/DELETE에서만 작동하며 GET 요청은 SameSite=Lax를 우회
  • "그냥 API니까"라고 CSRF 보호를 비활성화하는 것 — 쿠키가 관여된다면 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.

관련 스킬