FOUNDRY
C8 Platform
← Tasks

[SEC-01] Role-based Access Control (RBAC)

completedreviewP0

Description

## SEC-01: Role-based Access Control (RBAC) Section: Security | Milestone: M1 | Owner: None ### Failing Tests - **rbac_implementation** (grep): Pattern `checkRole|hasPermission|authorize` in `src/lib/auth.ts` (need 1+ matches) ### Acceptance Criteria - File matching `src/lib/auth.ts` must contain pattern `checkRole|hasPermission|authorize` ### Codebase Conventions - Trilingual comments: // EN: ... // VI: ... // ES: ... - SPEC section refs in comments - Zod at module level with .safeParse() - (supabase as any) with eslint-disable - NextResponse.json() responses - Error format: { error: 'message' } ### Reference Files (passing siblings in this section) - SEC-04: No hardcoded secrets in client-side code ### Dependencies - DM-02: FAIL - API-01: FAIL - API-03: FAIL
Assignee
-
Claimed By
Cloud Lu → G3 Pro
Created
23d ago
Completed
19d ago

Quality

Overall
79%
Pass
PASS
Graded By
-
Graded At
-

Cost

Input Tokens
-
Output Tokens
-
Est. USD
-

Result

{
  "cost": {
    "input_tokens": 16,
    "estimated_usd": 0.03704,
    "output_tokens": 1232
  },
  "model": "gemini-3-pro-preview",
  "status": "success",
  "transport": "gemini_api",
  "latency_ms": 21933,
  "response_text": "This document outlines the requirements, guidelines, and implementation strategy for **[SEC-01] Role-based Access Control (RBAC)**. This standard is intended for use in system architecture, security compliance, and application development.\n\n---\n\n# [SEC-01] Role-based Access Control (RBAC)\n\n## 1. Objective\nTo ensure that access to system resources, data, and administrative functions is restricted to authorized users based on their specific job functions and responsibilities. This control enforces the **Principle of Least Privilege**.\n\n## 2. Core Policy Requirements\n*   **Decoupling:** Permissions shall not be assigned directly to users. Permissions must be assigned to **Roles**, and users must be assigned to **Roles**.\n*   **Default Deny:** The system must default to a state of \"no access\" until a specific role is assigned.\n*   **Least Privilege:** Roles must be defined with the minimum set of permissions required to perform a specific business function.\n*   **Separation of Duties:** Critical functions (e.g., initiating a payment vs. approving a payment) must require distinct roles to prevent fraud or error.\n\n## 3. Key Definitions\n1.  **Subject (User/Service Account):** The entity requesting access.\n2.  **Resource:** The asset being accessed (e.g., API endpoint, database table, UI component).\n3.  **Permission (Scope):** The specific operation allowed on a resource (e.g., `read`, `write`, `delete`).\n4.  **Role:** A collection of permissions representing a job function (e.g., `Administrator`, `Auditor`, `Customer`).\n\n## 4. Implementation Guidelines\n\n### 4.1. The RBAC Matrix\nBefore writing code, an Access Control Matrix must be defined.\n\n| Feature / Resource | Role: Admin | Role: Editor | Role: Viewer | Role: Guest |\n| :--- | :---: | :---: | :---: | :---: |\n| **User Management** | Create, Read, Update, Delete | Read-Only | No Access | No Access |\n| **Content Posts** | Full Access | Create, Edit, Publish | Read-Only | Read-Only |\n| **System Settings** | Full Access | No Access | No Access | No Access |\n| **Audit Logs** | Read-Only | No Access | No Access | No Access |\n\n### 4.2. Database Schema (Relational Example)\nA standard normalized RBAC schema typically involves five tables:\n1.  `Users`\n2.  `Roles`\n3.  `Permissions`\n4.  `User_Roles` (Many-to-Many mapping)\n5.  `Role_Permissions` (Many-to-Many mapping)\n\n### 4.3. Application Logic\n*   **API Level:** Middleware must intercept requests to verify if the user's assigned role possesses the required permission for the endpoint.\n*   **UI Level:** Elements (buttons, tabs) should be hidden or disabled if the user lacks the role to interact with them. *Note: UI hiding is for UX, not security. Server-side validation is mandatory.*\n\n## 5. Technical Implementation Example (Pseudocode)\n\n**Scenario:** Protecting a `DELETE /api/users/{id}` endpoint.\n\n```javascript\n// 1. Define Permissions\nconst PERMISSIONS = {\n  USER_DELETE: 'user:delete',\n  USER_READ: 'user:read'\n};\n\n// 2. Define Roles\nconst ROLES = {\n  ADMIN: [PERMISSIONS.USER_DELETE, PERMISSIONS.USER_READ],\n  SUPPORT: [PERMISSIONS.USER_READ]\n};\n\n// 3. Middleware Function\nfunction requirePermission(requiredPermission) {\n  return (req, res, next) => {\n    const userRole = req.user.role; // Extracted from Session/JWT\n    const userPermissions = ROLES[userRole];\n\n    if (userPermissions && userPermissions.includes(requiredPermission)) {\n      return next(); // Access Granted\n    } else {\n      return res.status(403).json({ error: \"Forbidden: Insufficient Privileges\" });\n    }\n  };\n}\n\n// 4. Route Definition\napp.delete('/api/users/:id', requirePermission('user:delete'), (req, res) => {\n  // Execute deletion logic\n});\n```\n\n## 6. Best Practices & Maintenance\n\n### 6.1. Role Explosion Management\nAvoid creating a new role for every single user variation. If access needs become highly specific (e.g., \"User can edit only document X\"), consider moving from RBAC to **ABAC (Attribute-Based Access Control)** or implementing resource-level ownership checks.\n\n### 6.2. Auditing\n*   **Access Reviews:** Conduct quarterly reviews of user-to-role assignments to ensure employees who changed departments do not retain old privileges.\n*   **Logging:** All access denials (403 Forbidden) must be logged to security monitoring tools to detect potential privilege escalation attacks.\n\n### 6.3. Hierarchical Roles\nSystems may implement role inheritance (e.g., an `Admin` inherits all permissions of an `Editor`). While convenient, this must be documented clearly to avoid unintended permission leakage.\n\n## 7. Verification Checklist\n- [ ] Are permissions assigned to roles, not users?\n- [ ] Is there a \"Default Deny\" mechanism in place?\n- [ ] Is the `Guest/Public` role strictly limited?\n- [ ] Are API endpoints protected independently of the UI?\n- [ ] Is there a mechanism to revoke a role immediately (e.g., JWT revocation or short expiry)?"
}
Task ID: 64b08f1f-0985-4924-908e-c6c5b4938c0c