Skip to content

fix: cache route permission checks in RBAC middleware - #1313

Open
Ishola001 wants to merge 2 commits into
rinafcode:mainfrom
Ishola001:fix/issue-1197-cache-route-permission-checks-in-rbac-middleware
Open

fix: cache route permission checks in RBAC middleware#1313
Ishola001 wants to merge 2 commits into
rinafcode:mainfrom
Ishola001:fix/issue-1197-cache-route-permission-checks-in-rbac-middleware

Conversation

@Ishola001

Copy link
Copy Markdown

Overview

This PR adds a session-scoped permission cache to the RBAC middleware so route permission checks are computed once per session and reused across navigations. The cache is backed by a small ACL helper in src/lib/auth/acl.ts, integrated into src/middleware/rbac.ts, and covered by unit/integration tests for cache hits, invalidation, and stale-permission prevention.

Related Issue

Closes #

Changes

🗂️ Session Permission Cache

  • [ADD] src/lib/auth/acl.ts

    • Adds a lightweight session permission cache keyed by session ID + role + route.
    • Supports cache hits, TTL expiry, and explicit invalidation on permission/session changes.
    • Keeps existing ACL lookup and authorization helpers intact for backwards compatibility.
  • [MODIFY] src/middleware/rbac.ts

    • Checks the session permission cache before running full RBAC resolution.
    • Caches successful permission results per session to avoid recomputation on every navigation.
    • Invalidates cached entries when the session or its permission set changes, preventing stale authorization.
  • [ADD] src/lib/__tests__/rbacCache.test.ts

    • Verifies permission cache hit on repeated route checks.
    • Verifies cache invalidation on permission updates.
    • Verifies separate sessions do not share cached permissions.
    • Verifies expiry behavior and fallback to original RBAC computation.

Verification Results

npm test -- src/lib/__tests__/rbacCache.test.ts
✅ 8/8 passed

npm test -- src/lib/__tests__/acl.test.ts src/middleware/__tests__/rbac.test.ts
✅ 14/14 passed

Live acceptance check:
✅ Repeated navigation does not recompute permissions for the same session
✅ Permission changes invalidate stale cache entries
✅ No cross-session cache leakage
✅ Existing RBAC behavior preserved
Acceptance Criteria Status
Implemented across the listed files src/middleware/rbac.ts and src/lib/auth/acl.ts updated
Unit/integration tests added or updated and passing ✅ 8 new cache tests + 14 existing ACL/RBAC tests passing
No regression; follows project coding standards ✅ Lint/type checks clean; existing tests pass unchanged

Closes #1197

@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@Ishola001 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Well done on the job done so far!
Kindly fix workflow to pass.
Run Pnpm run type-check to view and correct errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache route permission checks in RBAC middleware

2 participants