Communitygithub.com

YepAPI/skills

RBAC 授权 — 中间件守卫、默认拒绝、资源级权限检查。

skills 是什么?

该技能实现了基于角色的访问控制(RBAC)授权系统,适用于 Express 等 Node.js 后端框架。其核心原则包括:对每条路由强制使用中间件进行权限检查(绝不可选)、默认拒绝访问(除非显式授权)、以及资源级权限验证(例如检查用户是否有权编辑特定资源)。技能支持角色层级(管理员 > 编辑者 > 查看者),高级角色继承低级角色的权限,并通过 `can(user, action, resource)` 辅助函数实现细粒度控制。它强调服务端强制执行,明确指出隐藏 UI 元素并非安全措施,所有权限检查必须在服务端完成。此外,技能要求记录所有权限审计日志(尤其是管理员操作),并严格分离认证(身份验证)与授权(权限验证)。避免常见陷阱如仅在前端检查权限、使用单一 `isAdmin` 布尔值、或绕过内部端点授权。适用于需要角色与资源级精细权限管理的 Web 应用开发。

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

在你喜欢的 AI 中提问

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

文档

RBAC 授权

规则

  • 在每个路由的中间件中检查权限——绝不使授权变为可选
  • 默认拒绝:如果没有明确授予权限,则拒绝访问
  • 服务端强制:隐藏 UI 元素不是安全措施,应在服务端进行检查
  • 定义角色层级:admin > editor > viewer —— 高级角色继承低级权限
  • 资源级检查:验证 can(user, "edit", resource) —— 而不仅仅是基于角色的访问
  • 审计所有权限检查 —— 记录谁在何时访问了什么,尤其是管理操作
  • 区分身份验证(你是谁?)与授权(你能做什么?)
// 权限检查辅助函数
type Action = "read" | "create" | "update" | "delete";
type Resource = { ownerId: string; [key: string]: any };

function can(user: { id: string; role: string }, action: Action, resource?: Resource): boolean {
  const permissions: Record<string, Action[]> = {
    admin: ["read", "create", "update", "delete"],
    editor: ["read", "create", "update"],
    viewer: ["read"],
  };
  const allowed = permissions[user.role];
  if (!allowed?.includes(action)) return false;
  // 资源级检查:编辑者只能修改自己的资源
  if (resource && action !== "read" && user.role !== "admin") {
    return resource.ownerId === user.id;
  }
  return true;
}
// Express 中间件守卫
function requirePermission(action: Action) {
  return (req, res, next) => {
    if (!req.user) return res.status(401).json({ error: "未认证" });
    if (!can(req.user, action, req.resource)) {
      return res.status(403).json({ error: "禁止访问" });
    }
    next();
  };
}

app.put("/api/posts/:id", loadResource, requirePermission("update"), updatePost);

避免

  • 仅在 UI 中检查权限——攻击者可以直接调用你的 API
  • 除非明确拒绝才允许访问——始终默认拒绝
  • 使用单一的 isAdmin 布尔值——角色应细粒度且可扩展
  • 忘记资源级检查——"editor" 不应意味着"可以编辑所有人的内容"
  • 在"内部"端点上跳过授权——这些端点最终会公开

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.

相关技能