Communitygithub.com

Next.js 安全规范

App Router 和 Server Actions 的核心安全标准。用于保护 Next.js App Router 路由、Server Actions 或 API 端点。

Next.js 安全规范 是什么?

此技能为 Next.js 应用提供了关键的安全实施指南,特别是针对 App Router 和 Server Actions。它涵盖了从中间件中的身份验证和角色权限控制到 Server Actions 中的输入消毒等各个方面。该指南强调了使用 Zod 进行输入验证、使用 `experimental_taint` API 防止敏感数据泄露到客户端、实现速率限制以防止暴力攻击以及正确设置安全头部和 Cookie 的重要性。它还明确了常见的反模式,例如将数据库字段泄露给客户端、在客户端包中暴露环境变量以及在没有验证的情况下使用 Server Action 输入。遵循这些指南可以确保 Next.js 应用在生产环境中保持安全,并免受 XSS、CSRF 和数据泄露等常见 Web 漏洞的攻击。该技能旨在与 AI 代理配合使用(例如 Claude Code、Cursor),以在开发过程中强制执行这些安全标准。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComeOnOliver/skillshub/tree/HEAD/skills/HoangNguyen0403/agent-skills-standard/nextjs-security

在你喜欢的 AI 中提问

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

文档

Next.js 安全

优先级:P0(关键)

结构

app/
├── lib/
│   └── validation.ts   # 共享 Zod 模式
└── middleware.ts       # 身份验证和头部

实施指南

  • Next.js 中间件:使用 middleware.ts 进行边缘端身份验证、基于角色的访问控制 (RBAC) 并强制执行 安全头部(例如 Content-Security-Policy (CSP)、X-XSS-Protection)。
  • Server Actions:始终使用 Zod 对来自 FormData 或 JSON 的 所有输入进行消毒。在每个操作内部执行 身份验证检查(await auth())以验证调用者。
  • 数据污染:使用 experimental_taint API(taintObjectReference)确保敏感服务器对象(例如包含 passwordHash 的 User)永远不会泄露到客户端组件中。
  • 路由处理器 (route.ts):实施 速率限制 以防止暴力破解或 DoS 攻击。验证 Origin/Referer 头部 以缓解 CSRF(跨站请求伪造)。
  • 身份验证令牌:严格使用 HttpOnly、Secure Cookie 以及 SameSite: 'Lax' 进行会话管理。切勿将令牌存储在 localStorage 中。
  • 逻辑隔离:使用 server-only 包防止后端特定逻辑被包含在客户端包中。
  • 组件纯化:转义组件中渲染的所有用户提供的内容。切勿在没有消毒器(如 DOMPurify)的情况下使用 dangerouslySetInnerHTML。

反模式

  • 不要将数据库字段泄露给客户端:使用 DTO;切勿传递原始模型对象。
  • 不要在客户端包中使用 process.env:仅当安全暴露时,才将其标记为 NEXT_PUBLIC_。
  • 不要使用未经验证的 Server Action 输入:始终使用 Zod 模式进行验证。
  • 不要在共享布局中进行身份验证检查:在布局中进行身份验证是不安全的;请使用中间件。

参考

Individual skills in this repo

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

ComeOnOliver/skillshub

Create stunning, animation-rich HTML presentations from scratch or by converting PowerPoint files. Use when the user wants to build a presentation, convert a PPT/PPTX to web, or create slides for a talk/pitch. Helps non-designers discover their aesthetic through visual exploration rather than abstract choices.

ComeOnOliver/skillshub

Next.js 16+ 和 Turbopack — 增量打包、文件系统缓存、开发速度,以及何时使用 Turbopack 与 webpack。

ComeOnOliver/skillshub

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

ComeOnOliver/skillshub

See, Understand, Act on video and audio. See- ingest from local files, URLs, RTSP/live feeds, or live record desktop; return realtime context and playable stream links. Understand- extract frames, build visual/semantic/temporal indexes, and search moments with timestamps and auto-clips. Act- transcode and normalize (codec, fps, resolution, aspect ratio), perform timeline edits (subtitles, text/image overlays, branding, audio overlays, dubbing, translation), generate media assets (image, audio, video), and create real time alerts for events from live streams or desktop capture.

ComeOnOliver/skillshub

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

ComeOnOliver/skillshub

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

ComeOnOliver/skillshub

Provides Tamagui patterns for config v4, compiler optimization, styled context, and cross-platform styling. Must use when working with Tamagui projects (tamagui.config.ts, @tamagui imports).

ComeOnOliver/skillshub

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

ComeOnOliver/skillshub

Create high-converting, visually distinctive landing pages. Use when building marketing pages, product launches, SaaS homepages, or any single-page conversion-focused website. Guides section-by-section composition with anti-AI-slop principles.

ComeOnOliver/skillshub

Auto-loaded context for Portfolio Buddy 2 development. Use for ANY task involving: React 19 development, TypeScript, portfolio analysis features, metrics calculations, trading strategy comparison, or working with the Portfolio Buddy 2 codebase. Contains tech stack, known issues, and architectural constraints.

ComeOnOliver/skillshub

Next.js development tooling via MCP. Inspect routes, components, build info, and debug Next.js apps. Use when working on Next.js applications, debugging routing, or inspecting app structure. NOT for general React or non-Next.js projects.

ComeOnOliver/skillshub

Implement UI using Shadcn MCP (atoms/theme) and 21st.dev MCP (complex sections). Use when adding buttons, layouts, or generating landing pages.

ComeOnOliver/skillshub

A conceptual skill for building an API client in Next.js that handles JWT tokens

ComeOnOliver/skillshub

Best practices and patterns for Next.js App Router, Server Actions, and Routing in this project.

ComeOnOliver/skillshub

The Design System, Theme, and UX rules for the Physical AI Hub.

ComeOnOliver/skillshub

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

ComeOnOliver/skillshub

This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations.

ComeOnOliver/skillshub

Build Next.js 16 applications with correct patterns and distinctive design. Use when creating pages, layouts, dynamic routes, upgrading from Next.js 15, or implementing proxy.ts. Covers breaking changes (async params/searchParams, Turbopack, cacheComponents) and frontend aesthetics. NOT when building non-React or backend-only applications.

ComeOnOliver/skillshub

Comprehensive Framer Motion animation library for React. Covers motion components, variants, gestures, page transitions, and scroll animations. Use when adding animations to React/Next.js applications.

ComeOnOliver/skillshub

Build interfaces that resonate, not just render. This skill enforces bold, intentional

相关技能