fix(auth): resolve model permissions off the target org's role, not the session's active-org role#4944
Merged
Conversation
…he session's active-org role AI_PROVIDER_KEY_LIST, AI_PROVIDERS_LIST_MODELS, and the decopilot chat/allowed-models paths all call fetchModelPermissions(db, org.id, ctx.auth.user?.role). But ctx.auth.user.role reflects the caller's role in their session's ACTIVE organization, while `org`/`organization` here is the path-resolved target org (set by resolveOrgFromPath) — the two can differ, as already documented on OrganizationScope.role and fixed for role-assignment in member-add.ts/member-update-role.ts (#4934) and for the AccessControl bypass in mcp-clients/outbound/transports/auth.ts. Failure scenario: a user who is "admin" (unrestricted models) in their active org but only holds a custom, model-restricted role in a different org B calls AI_PROVIDER_KEY_LIST / AI_PROVIDERS_LIST_MODELS / GET /api/:org/decopilot/allowed-models scoped to org B. fetchModelPermissions resolves permissions using the stale active-org role instead of the caller's real role in B, bypassing B's model restrictions. Fix: prefer the path-resolved `org.role` / `organization.role`, falling back to ctx.auth.user?.role only when it's unset — the same pattern already used in AuthTransport's connection-scoped AccessControl construction.
pedrofrxncx
added a commit
that referenced
this pull request
Jul 21, 2026
…n dispatch-run (#4947) Follows #4943/#4944 hardening. `fetchModelPermissions` in dispatchRunAndWait's resume/automation re-entry path used `ctx.auth.user?.role` (the session's active-org role) instead of the path-resolved role for `input.organizationId`, letting an owner/admin of one org bypass model-tier restrictions when a dispatch targets a different, lower-privileged org. #4944 already fixed the same fetchModelPermissions call in routes.ts/key-list.ts/list-models.ts but missed this call site.
pedrofrxncx
enabled auto-merge (squash)
July 21, 2026 17:34
decocms Bot
pushed a commit
that referenced
this pull request
Jul 21, 2026
PR: #4944 fix(auth): resolve model permissions off the target org's role, not the session's active-org role Bump type: patch - decocms (apps/mesh/package.json): 4.83.8 -> 4.83.9 Deploy-Scope: server
This was referenced Jul 21, 2026
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.
Source: bug found while auditing remaining
ctx.auth.user?.rolecall sites after #4934/#4926/#8ece109 fixed the same active-org-vs-target-org role confusion elsewhere (member-add, member-update-role, AuthTransport's connection AccessControl).Why a maintainer wants this:
AI_PROVIDER_KEY_LIST,AI_PROVIDERS_LIST_MODELS, and the decopilot chat dispatch /GET /api/:org/decopilot/allowed-modelspaths all resolve model permissions withfetchModelPermissions(db, org.id, ctx.auth.user?.role).ctx.auth.user.rolereflects the caller's role in their session's active organization (documented onOrganizationScope.roleinstudio-context.ts), whileorg/organizationhere is the path-resolved target org set byresolveOrgFromPath— every org-scoped route, including these MCP tool calls, runs through it. The two can differ.Failure scenario: a user who is
admin(unrestricted models) in their session's active org but holds only a custom, model-restricted role in a different org B callsAI_PROVIDER_KEY_LIST/AI_PROVIDERS_LIST_MODELS/GET /api/:org/decopilot/allowed-modelsscoped to org B.fetchModelPermissionsresolves permissions using the stale active-org role instead of the caller's real role in B, silently bypassing B's model restrictions (they see/use provider keys and models their B-role should have blocked).Fix: prefer the path-resolved
org.role/organization.role, falling back toctx.auth.user?.roleonly when it's unset — the exact patternmcp-clients/outbound/transports/auth.tsalready uses for its connection-scoped AccessControl (ctx.organization?.role ?? ctx.auth.user?.role).Net change: +12/-4 across 3 files, one concern (role source for model-permission checks).
Reviewer verification:
bunx tsc --noEmitinapps/mesh; the existingbun test apps/mesh/src/api/routes/decopilot/model-permissions.test.tsstill passes (untouched pure logic, only the caller-supplied role changed). No test added — same as the precedent fix in #4934, this needs a real Postgres-backed org/member setup to exercise end-to-end and isn't unit-testable without a stubbed StudioContext (banned per TESTING.md); full e2e coverage runs in CI.Locally run:
bun run fmt,cd apps/mesh && bunx tsc --noEmit,bun test apps/mesh/src/api/routes/decopilot/model-permissions.test.ts— all green. Full CI (lint, e2e) validates the rest.Note:
dispatch-run.ts's analogous call site (line ~915) was deliberately left alone — it's shared by multiple entry points (HTTP, durable-workflow resume, legacy automation) where the relationship betweenctx.organizationandinput.organizationIdisn't uniformly guaranteed, so a blind swap there risks introducing a new mismatch rather than fixing one. Also leftorg-sso.tsuntouched — the same-tick reactive session already opened #4943 for it.Summary by cubic
Fixes model-permission checks to use the target org’s role, not the session’s active-org role, preventing cross-org permission bypass. Affects
AI_PROVIDER_KEY_LIST,AI_PROVIDERS_LIST_MODELS, andGET /api/:org/decopilot/allowed-models.organization.role ?? ctx.auth.user?.roletofetchModelPermissionsindecopilot/routes.ts,ai-providers/key-list.ts, andai-providers/list-models.ts.adminrole from seeing models/keys in another org where they have a restricted role.Written for commit 5952ebd. Summary will update on new commits.