Repository navigation
fix(virtual-mcp): require org context for id-scoped legacy MCP requests - #5126
Merged
Merged
Conversation
pedrofrxncx
pushed a commit
that referenced
this pull request
Jul 27, 2026
#5240) Reverts #5112 (share agent sandboxes by branch), #5116 (always share agent sandboxes) and #5132 (use shared staging branch), restoring the pre-#5112 behaviour: hosted agent sandboxes are per-user again, and a new GitHub-backed thread gets a generated branch instead of `staging`. Reconstructed against current paths rather than reverted: #5120 split apps/mesh into apps/api + apps/web and hoisted branch-name.ts, runtime-defaults.ts and thread/schema.ts into packages/shared, and #5174 deleted vm-events.ts outright, so none of the three commits reverse-apply. Core change: SandboxId goes back to a flat `{ userId, projectRef }` and sandboxIdKey back to `userId:projectRef`, so every hosted claim handle changes. Live shared claims must be drained BEFORE this deploys. Also reverted, because they only exist inside the shared model: - #5225 withoutLegacyAgentSandboxEntries — left in place it would strip the sandboxMap entries the reverted server writes back, leaving shouldAutoStart permanently true (silent, compiles fine) - #5219 resolveSharedThreadVm and the shared-scope branch - #5183 stale-provisioning reset (lived inside the deleted storage) Deliberately preserved: - migrations 137/138 and their index.ts entries — Kysely's #ensureNoMissingMigrations runs unconditionally, so deleting an applied migration crash-loops every pod. The tables are dropped by a follow-up forward migration after the drain, not by this commit. - #5114/#5118 withClaimGitLock, #5126 org-context guard, #5143 analytics gate, #5171 owner-keyed thread graft, #5195/#5207/#5210/#5221 client auto-retry, the daemon status-code train and the settings-validation train - #5112's org-fs least-privilege narrowing and the start dedup key fix - #5116's squashed queue-tray pluralization (en + pt-br) - the remote-branch-name validations #5132 deleted, since this restores the dynamic origin/HEAD lookup that made them load-bearing Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Pedro França <[email protected]>
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
apps/mesh/src/api/routes/virtual-mcp.ts(the shared handler behind both/mcp/gateway/:virtualMcpIdand/mcp/virtual-mcp/:virtualMcpId, and their org-scoped/api/:org/mcp/...counterparts).Why a maintainer wants this:
handleVirtualMcpRequestonly enforces that the requested virtual MCP belongs to the caller's organization whenorganizationId(resolved fromx-org-id/x-org-slugheaders, or fromctx.organizationset byresolveOrgFromPath) is truthy — both ownership checks (organizationId && virtualMcp.organization_id !== organizationIdandctx.organization?.id && virtualMcp.organization_id !== ctx.organization.id) are skipped entirely when it's null. On the org-scoped route this never happens (resolveOrgFromPathalways setsctx.organization). But on the legacy unscoped route (/mcp/gateway/:virtualMcpId, still mounted and documented as accepting either header),context-factory.tsdeliberately returns no organization when a multi-org member's session has no deterministic active org and no header is sent (see theauth_query_membershipcomment: "return undefined ... instead of a non-deterministic pick"). In that combination — multi-org user, legacy route, no org headers, explicitvirtualMcpId—organizationIdstaysnull, both guards are skipped, andctx.storage.virtualMcps.findById(virtualId, undefined)fetches the target virtual MCP with zero org scoping, regardless of which org it actually belongs to.Failure scenario: a member of two or more orgs calls
/mcp/gateway/<virtualMcpId>directly (nox-org-id/x-org-slugheaders) with a virtual-MCP id belonging to an organization they're not a member of. The existing 404/403 ownership checks never trigger because they're gated on a resolvedorganizationId, so the request proceeds to build a live MCP server/session bound to that other org's agent (server info, instructions, tool listing) instead of failing closed.Fix: when a specific
virtualMcpIdis requested but no organization context could be resolved, return400 { error: "Organization context is required" }before the lookup, instead of silently falling through with no ownership check. This only affects the narrow legacy-route-without-headers case; the org-scoped route is unaffected sinceorganizationIdis always resolved there.Verify:
cd apps/mesh && bunx tsc --noEmit(clean). No existing test file covers this route (apps/mesh/src/api/routes/virtual-mcp.tshas no.test.ts), so this is a pure typecheck-verified guard-clause addition — CI's full suite validates the rest.Checks run locally:
bun run fmt,bunx tsc --noEmit(apps/mesh workspace). No targeted unit test exists for this file to run.Summary by cubic
Require organization context for id-scoped legacy Virtual MCP requests to close a cross-org access gap. If a specific virtual MCP id is requested on legacy routes without org headers, the API now returns 400 instead of performing an unscoped lookup.
handleVirtualMcpRequestto return 400 whenvirtualMcpIdis provided and no organization is resolved./mcp/gateway/:virtualMcpIdand/mcp/virtual-mcp/:virtualMcpId; org-scoped/api/:org/...routes are unchanged.Written for commit 4b00135. Summary will update on new commits.