Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Submit feedback
    • Contribute to GitLab
  • Sign in
U
upgrade-data-crawler-be
  • Project
    • Project
    • Details
    • Activity
    • Releases
    • Cycle Analytics
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Charts
  • Issues 0
    • Issues 0
    • List
    • Board
    • Labels
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
    • Charts
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • ThinhNC
  • upgrade-data-crawler-be
  • Merge Requests
  • !7

Merged
Opened Sep 03, 2026 by ThinhNC@ThinhNC
  • Report abuse
Report abuse

feat(auth): implement dynamic RBAC and privilege escalation defense

Summary of Changes: Dynamic RBAC & Privilege Escalation Defense

Overview

This pull request introduces a fully dynamic, database-driven Role-Based Access Control (RBAC) engine accompanied by comprehensive Privilege Escalation Defense mechanisms. It transitions the application from static, hard-coded role checks to granular, permission-based authorization (resource.action) while strictly maintaining 100% backward compatibility with existing legacy role workflows.


Key Features & Architectural Enhancements

  1. Database Schema & Data Model

    • Added relational models for Role, Permission, UserRoleAssignment (user_roles), and RolePermission (role_permissions).
    • Retained the legacy enum field on users to ensure zero-breakage across existing test suites and services.
    • Deployed migration and populated a deterministic seed catalog comprising 32 system permissions and 4 foundational system roles.
  2. Privilege Escalation Defense Matrix

    • Self-Escalation Barrier: Explicitly blocks any authenticated user from modifying, assigning, or revoking their own roles.
    • Super Admin Isolation: Regular administrators cannot assign the Super Admin role, tamper with Super Admin accounts, or modify Super Admin role permissions.
    • Last Active Super Admin Protection: Uses transactional atomic counting to guarantee the system never drops below at least one active Super Admin account.
    • System Role Integrity: System roles are permanently protected against deletion, renaming, or deactivation.
    • Mass Assignment & Injection Defense: Strips untrusted role or permission parameters from user update schemas; permissions are dynamically resolved server-side.
  3. Authorization Engine & Middleware

    • Implemented fail-closed (deny-by-default) middlewares:
      • requirePermission(permission)
      • requireAnyPermission(...permissions)
      • requireAllPermissions(...permissions)
    • Integrated lightweight in-memory caching for resolved user roles and permissions, with automatic cache invalidation triggered upon any role or permission mutation.
    • Dual-mode requireRole middleware supporting both legacy enum values and dynamic role slugs.
  4. RESTful Management APIs

    • Roles: Full lifecycle management (list, query, create, update, delete, view assigned users).
    • Permissions: Read-only registry inspection and system permission discovery.
    • Role-Permission Bindings: Dynamic attachment and synchronization of permissions to roles.
    • User-Role Assignments: Granular endpoints to inspect, replace, add, or revoke individual user roles.
  5. Security Audit Logging

    • Structured audit trails emitted for all role mutations, permission bindings, and blocked escalation attempts (PRIVILEGE_ESCALATION_BLOCKED, SUPER_ADMIN_ASSIGN_ATTEMPT).

Check out, review, and merge locally

Step 1. Fetch and check out the branch for this merge request

git fetch origin
git checkout -b feat/dynamic-rbac-privilege-escalation-defense origin/feat/dynamic-rbac-privilege-escalation-defense

Step 2. Review the changes locally

Step 3. Merge the branch and fix any conflicts that come up

git fetch origin
git checkout origin/develop
git merge --no-ff feat/dynamic-rbac-privilege-escalation-defense

Step 4. Push the result of the merge to GitLab

git push origin develop

Note that pushing to GitLab requires write access to this repository.

Tip: You can also checkout merge requests locally by following these guidelines.

  • Discussion 0
  • Commits 1
  • Changes 39
Assignee
No assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
0
Labels
None
Assign labels
  • View project labels
Reference: ThinhNC/upgrade-data-crawler-be!7

Revert this merge request

This will create a new commit in order to revert the existing changes.

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.

Cherry-pick this merge request

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.