Communitygithub.com

YepAPI/skills

RBAC 授權——中介軟體守衛,預設拒絕,資源級別權限檢查。

skills 是什麼?

此技能專注於基於角色的存取控制(RBAC)授權機制,提供一套嚴謹的中介軟體守衛模式。它強調「預設拒絕」原則,若未明確授予權限,則拒絕所有存取請求,並在伺服器端強制執行權限檢查,避免僅依賴前端隱藏 UI 元素。技能支援資源級別的權限驗證,確保使用者僅能在其角色與資源擁有權範圍內執行操作,例如編輯者僅能修改自己的資源。它支援常見的 Web 框架(如 Express),並透過中介軟體將身份驗證與授權分離,同時內建日誌記錄與審計功能,以便追蹤所有權限檢查事件。這項技能適用於需建立精細存取控制的開發者工具或 AI 代理場景,尤其在 Claude Code、Cursor 或 Codex 等平台上實現安全、可擴展的 API 守衛。

相容平台~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 布林值——角色應具有細緻度且可擴展
  • 忘記資源級別檢查——「編輯者」不應意味著「可以編輯所有人的內容」
  • 在「內部」端點上跳過授權——它們最終會變成公開的

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.

相關技能