Repository navigation
fix(rbac): the UI role comes from the server role, and fails closed to viewer - #633
Open
privatejoel wants to merge 1 commit into
Open
privatejoel wants to merge 1 commit into
privatejoel wants to merge 1 commit into
Conversation
…o viewer RBAC.getRole() read the localStorage key v3_role and used sales_engineer when it was empty. Only the top-bar switcher wrote that key, so on a fresh browser an admin saw "cancel" and "return for fix" disabled on an order that the server lets them cancel. - GET /api/auth/profile also returns ctx.role (the role that every server gate checks for this request's tenant). - app.tsx reads the profile each time the session becomes valid and calls RBAC.setServerRole. A failed read or a missing role sets viewer. - The switcher is now "view as": an admin may view as any role, every other role may view as itself or viewer. A stored view-as role that is wider than the server role is ignored. - Sign-out sets the server role to viewer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This was referenced Oct 22, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The v3 app gates its buttons with
RBAC.canDo,canWriteandcanApprove(src/v3-app/lib/rbac.ts). These read the role from the localStorage keyv3_role. When the key was empty,getRole()returnedsales_engineer. Only the top-bar role switcher wrote the key. No code set it from the server role. Result: on a fresh browser, an admin saw "cancel" and "return for fix" disabled on a DRAFT order, with the tooltip "needs sales_manager / admin". The server allows that cancel (seen on 2026-10-21).This PR does these things:
GET /api/auth/profilealso returnsrole: ctx.role. This is the role that every server gate checks for the tenant of this request. Themembershipslist cannot tell you this, because it lists every tenant. The shell already calls this endpoint, so there is no new route.app.tsxreads the profile each time the session becomes valid. This includes the first load and a sign-in in the same tab. Then it calls the newRBAC.setServerRole. The old effect skipped the read when a profile was cached. Now it does not skip it.viewer. Thesales_engineerdefault is gone.auth.js hasAction). So an admin can view as any role.viewer.v3_roleis wider than the server role, the app ignores it.setRolethrows on a wider role. The menu shows only the allowed roles.viewer. Thus the next user on the same browser does not get the role of the previous user.shortcut: a role that is not admin can view only as itself or viewer. There is no subset table between the other roles. Add one if a manager must view as a rep.
Drift check: SO status changes (owner decision needed)
Cancel: no drift now. The brief said that
orders/[id].jsPATCH lets any writer set CANCELLED. That was true before fix(rbac): register so.write and so.admin (plan PR 5) #606. Now_lib/order-write-gate.jsrequiresso.cancel= sales_manager and admin. This is the same asrbac.ts. No change.Return for fix: drift. It is open and I did not change it. The UI shows the button only to
so.approveroles (sales_manager, finance, admin). The server letsso.writeroles do the same move too (sales_engineer, design_engineer, design_manager). They can move PENDING_REVIEW or APPROVED back to DRAFT with acorrection_reason. The server records that asmanager_requested_correction. I sent the owner this question with three options:so.approvefor a return that has a reason. The UI does not change. This is my recommendation.so.write.There is no answer yet, so this PR changes neither side. The UI is the stricter side, so no user sees a button that the server refuses.
Deploy notes
vieweruntil the profile read returns. This is usually less than one second. After that, the role is cached inanvil:v3_server_role.v3_rolevalue for each user. Thus any widened role chosen in the old switcher is dropped./api/auth/profilefails for a user, that user sees the app asvieweruntil the read succeeds. The server gates do not change.Tests
lib/rbac.test.ts: fails closed to viewer when no server role is known. An unknown or missing role is viewer. A server admin on a fresh browser can cancel and approve. A stored wider view-as role is ignored.setRolerefuses to widen. View-as can narrow to viewer and emitsrbac:change. An admin can view as any role. A new server role drops the old view-as choice. The matrix tests now set the server role.app-server-role.test.tsx(rendersApp): an admin gets admin from the profile. The role is read again even when a profile is cached. A failed read over a cached admin gives viewer. A profile without a role gives viewer. A sign-in in the same tab reads the role. The switcher shows 2 roles to a sales_engineer and all roles to an admin.api-auth-profile-role.test.js: GET returns the role of the request tenant, not the role from another membership. An anonymous caller gets 401 and no role.lib/session.test.ts: sign-out drops the role to viewer.sales_engineerdefault. They now set their role withinstallRbac, andinstallRbacnow sets the server role too.setServerRole. It is an equivalent mutant, becausegetServerRolevalidates on read.npm run check,lint(0 errors),build,verify,eval:golden(score 1.000) all pass. Full vitest: 7382 passed, 1 failed. The failure was a 5 s timeout inapi-source-pos-ack-extract.test.js(router import under load). That file passes 21/21 when it runs alone, and this PR does not touch it. The audit docs drift thatverifywrites is old, so I restored it.Not covered
canDo("so.cancel")) that its buttons read./connectdev screen changes the tenant while the user stays signed in, the role is not read again until the next load.🤖 Generated with Claude Code