Communitygithub.com

RBAC 認可

ミドルウェアガードによるRBAC認可 — デフォルトで拒否、リソースレベルの権限チェック。

RBAC 認可 とは?

このスキルは、役割ベースアクセス制御(RBAC)を用いた堅牢な認可システムを実装するためのガイドラインと実装例を提供します。すべてのルートに対してミドルウェアによる権限チェックを義務付け、デフォルトで拒否(deny by default)の原則を徹底します。管理者、編集者、閲覧者などの役割階層を定義し、上位の役割が下位の権限を継承する構造を推奨します。また、単なる役割チェックに留まらず、リソース所有者に基づく細粒度の権限検証(`can(user, 'edit', resource)`)を実装します。認証と認可を明確に分離し、すべての権限チェックを監査ログに記録することで、セキュリティの透過性を高めます。Express、Next.js、Fastifyなど一般的なNode.jsフレームワークで動作し、TypeScriptの型安全なヘルパー関数とミドルウェアパターンを提供します。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/rbac-authorization

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

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.

関連スキル