|
| 1 | +/** |
| 2 | + * Role-Based Access Control (RBAC) for Discord commands. |
| 3 | + * Gates dangerous commands behind configurable role requirements. |
| 4 | + * |
| 5 | + * @module core/rbac |
| 6 | + */ |
| 7 | + |
| 8 | +import type { InteractionContext } from "../discord/types.ts"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Commands that require elevated permissions. |
| 12 | + * Grouped by risk category. |
| 13 | + */ |
| 14 | +const RESTRICTED_COMMANDS: Record<string, string[]> = { |
| 15 | + /** Full host access — highest risk */ |
| 16 | + shell: ['shell', 'shell-input', 'shell-list', 'shell-kill'], |
| 17 | + /** Repository modifications */ |
| 18 | + git: ['git', 'worktree', 'worktree-remove', 'worktree-bots', 'worktree-kill'], |
| 19 | + /** System information exposure */ |
| 20 | + system: ['env-vars', 'port-scan', 'system-logs'], |
| 21 | + /** Bot lifecycle */ |
| 22 | + admin: ['shutdown'], |
| 23 | +}; |
| 24 | + |
| 25 | +/** Flat set of all restricted command names for fast lookup */ |
| 26 | +const ALL_RESTRICTED = new Set( |
| 27 | + Object.values(RESTRICTED_COMMANDS).flat() |
| 28 | +); |
| 29 | + |
| 30 | +/** |
| 31 | + * RBAC configuration loaded from environment. |
| 32 | + */ |
| 33 | +interface RBACConfig { |
| 34 | + /** Whether RBAC is enabled (requires at least one role configured) */ |
| 35 | + enabled: boolean; |
| 36 | + /** Set of Discord role IDs that can run restricted commands */ |
| 37 | + allowedRoleIds: Set<string>; |
| 38 | + /** Set of Discord user IDs that always have access (bot owner) */ |
| 39 | + allowedUserIds: Set<string>; |
| 40 | +} |
| 41 | + |
| 42 | +let cachedConfig: RBACConfig | null = null; |
| 43 | + |
| 44 | +/** |
| 45 | + * Load RBAC configuration from environment variables. |
| 46 | + * |
| 47 | + * Environment variables: |
| 48 | + * - `ADMIN_ROLE_IDS`: Comma-separated Discord role IDs (e.g., "123456,789012") |
| 49 | + * - `ADMIN_USER_IDS`: Comma-separated Discord user IDs (e.g., "123456") |
| 50 | + * |
| 51 | + * If neither is set, RBAC is disabled and all commands are open. |
| 52 | + */ |
| 53 | +export function loadRBACConfig(): RBACConfig { |
| 54 | + if (cachedConfig) return cachedConfig; |
| 55 | + |
| 56 | + const roleIdsRaw = Deno.env.get("ADMIN_ROLE_IDS") ?? ""; |
| 57 | + const userIdsRaw = Deno.env.get("ADMIN_USER_IDS") ?? ""; |
| 58 | + |
| 59 | + const allowedRoleIds = new Set( |
| 60 | + roleIdsRaw.split(",").map(id => id.trim()).filter(Boolean) |
| 61 | + ); |
| 62 | + const allowedUserIds = new Set( |
| 63 | + userIdsRaw.split(",").map(id => id.trim()).filter(Boolean) |
| 64 | + ); |
| 65 | + |
| 66 | + const enabled = allowedRoleIds.size > 0 || allowedUserIds.size > 0; |
| 67 | + |
| 68 | + cachedConfig = { enabled, allowedRoleIds, allowedUserIds }; |
| 69 | + |
| 70 | + if (enabled) { |
| 71 | + console.log(`[RBAC] Enabled — ${allowedRoleIds.size} admin role(s), ${allowedUserIds.size} admin user(s)`); |
| 72 | + } else { |
| 73 | + console.log("[RBAC] Disabled — no ADMIN_ROLE_IDS or ADMIN_USER_IDS configured. All commands are open."); |
| 74 | + } |
| 75 | + |
| 76 | + return cachedConfig; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Check whether a command name is restricted. |
| 81 | + */ |
| 82 | +export function isRestrictedCommand(commandName: string): boolean { |
| 83 | + return ALL_RESTRICTED.has(commandName); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Check whether the invoking user has permission to run a restricted command. |
| 88 | + * |
| 89 | + * @returns `true` if allowed, `false` if denied |
| 90 | + */ |
| 91 | +export function hasPermission(ctx: InteractionContext): boolean { |
| 92 | + const config = loadRBACConfig(); |
| 93 | + |
| 94 | + // If RBAC is not enabled, allow everything |
| 95 | + if (!config.enabled) return true; |
| 96 | + |
| 97 | + // Check user ID allowlist |
| 98 | + const userId = ctx.getUserId(); |
| 99 | + if (userId && config.allowedUserIds.has(userId)) return true; |
| 100 | + |
| 101 | + // Check role IDs |
| 102 | + const memberRoles = ctx.getMemberRoleIds(); |
| 103 | + for (const roleId of config.allowedRoleIds) { |
| 104 | + if (memberRoles.has(roleId)) return true; |
| 105 | + } |
| 106 | + |
| 107 | + return false; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * RBAC check that can be called before executing a command. |
| 112 | + * Sends an ephemeral denial message if the user lacks permission. |
| 113 | + * |
| 114 | + * @returns `true` if the command should proceed, `false` if denied |
| 115 | + */ |
| 116 | +export async function checkCommandPermission( |
| 117 | + commandName: string, |
| 118 | + ctx: InteractionContext |
| 119 | +): Promise<boolean> { |
| 120 | + if (!isRestrictedCommand(commandName)) return true; |
| 121 | + if (hasPermission(ctx)) return true; |
| 122 | + |
| 123 | + await ctx.reply({ |
| 124 | + content: "🔒 **Access Denied** — You don't have permission to run this command. An admin role is required.", |
| 125 | + ephemeral: true |
| 126 | + }); |
| 127 | + |
| 128 | + return false; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Get a copy of the restricted commands map for display purposes. |
| 133 | + */ |
| 134 | +export function getRestrictedCommands(): Record<string, string[]> { |
| 135 | + return { ...RESTRICTED_COMMANDS }; |
| 136 | +} |
0 commit comments