Skip to content

fix(rbac): the UI role comes from the server role, and fails closed to viewer - #633

Open
privatejoel wants to merge 1 commit into
mainfrom
claude/blissful-banach-f23616
Open

privatejoel wants to merge 1 commit into
mainfrom
claude/blissful-banach-f23616

Conversation

@privatejoel

Copy link
Copy Markdown
Collaborator

Summary

The v3 app gates its buttons with RBAC.canDo, canWrite and canApprove (src/v3-app/lib/rbac.ts). These read the role from the localStorage key v3_role. When the key was empty, getRole() returned sales_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:

  1. GET /api/auth/profile also returns role: ctx.role. This is the role that every server gate checks for the tenant of this request. The memberships list cannot tell you this, because it lists every tenant. The shell already calls this endpoint, so there is no new route.
  2. app.tsx reads 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 new RBAC.setServerRole. The old effect skipped the read when a profile was cached. Now it does not skip it.
  3. Fail closed. If the read fails, if the role is missing or if the stored value is unknown, the role is viewer. The sales_engineer default is gone.
  4. The switcher is now "view as". It can only make permissions narrower:
    • The server lets an admin do everything (auth.js hasAction). So an admin can view as any role.
    • Every other role can view as itself or as viewer.
    • If a stored v3_role is wider than the server role, the app ignores it. setRole throws on a wider role. The menu shows only the allowed roles.
  5. A new server role clears the old view-as choice. Sign-out sets the server role to 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].js PATCH 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.js requires so.cancel = sales_manager and admin. This is the same as rbac.ts. No change.

  • Return for fix: drift. It is open and I did not change it. The UI shows the button only to so.approve roles (sales_manager, finance, admin). The server lets so.write roles do the same move too (sales_engineer, design_engineer, design_manager). They can move PENDING_REVIEW or APPROVED back to DRAFT with a correction_reason. The server records that as manager_requested_correction. I sent the owner this question with three options:

    • (A) The server requires so.approve for a return that has a reason. The UI does not change. This is my recommendation.
    • (B) No change. The UI stays stricter than the server.
    • (C) The UI changes to 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

  • No migration. No new environment variable.
  • On the first load after deploy, each user sees the app as viewer until the profile read returns. This is usually less than one second. After that, the role is cached in anvil:v3_server_role.
  • The first server-role write clears the old v3_role value for each user. Thus any widened role chosen in the old switcher is dropped.
  • If /api/auth/profile fails for a user, that user sees the app as viewer until 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. setRole refuses to widen. View-as can narrow to viewer and emits rbac: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 (renders App): 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.
  • Six test files relied on the old sales_engineer default. They now set their role with installRbac, and installRbac now sets the server role too.
  • Mutation check: 12 mutants, 11 failed the tests. The survivor removes the validation in setServerRole. It is an equivalent mutant, because getServerRole validates on read.
  • Gates: 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 in api-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 that verify writes is old, so I restored it.

Not covered

  • The return-for-fix decision (see above).
  • The SO workspace screen is not rendered in a new test. The tests prove the gate values (canDo("so.cancel")) that its buttons read.
  • Fresh browser only: if a user deep-links to a screen that viewer cannot read, the route guard can send them to home before the role read returns. On later loads, the cached role prevents this.
  • If the /connect dev screen changes the tenant while the user stays signed in, the role is not read again until the next load.
  • The pill title still says "Switch role".

🤖 Generated with Claude Code

…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]>
@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
anvil Ready Ready Preview Oct 21, 2026 3:01am UTC

This branch was successfully deployed

1 active deployment
Preview — d70b14d6 Deployed Oct 21, 2026 by vercel[bot]
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.

1 participant