Skip to content

fix(virtual-mcp): require org context for id-scoped legacy MCP requests - #5126

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/virtual-mcp-legacy-route-org-context-w7
Jul 23, 2026
Merged

pedrofrxncx merged 1 commit into
mainfrom
fix/virtual-mcp-legacy-route-org-context-w7

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Source: bug found while auditing apps/mesh/src/api/routes/virtual-mcp.ts (the shared handler behind both /mcp/gateway/:virtualMcpId and /mcp/virtual-mcp/:virtualMcpId, and their org-scoped /api/:org/mcp/... counterparts).

Why a maintainer wants this: handleVirtualMcpRequest only enforces that the requested virtual MCP belongs to the caller's organization when organizationId (resolved from x-org-id/x-org-slug headers, or from ctx.organization set by resolveOrgFromPath) is truthy — both ownership checks (organizationId && virtualMcp.organization_id !== organizationId and ctx.organization?.id && virtualMcp.organization_id !== ctx.organization.id) are skipped entirely when it's null. On the org-scoped route this never happens (resolveOrgFromPath always sets ctx.organization). But on the legacy unscoped route (/mcp/gateway/:virtualMcpId, still mounted and documented as accepting either header), context-factory.ts deliberately returns no organization when a multi-org member's session has no deterministic active org and no header is sent (see the auth_query_membership comment: "return undefined ... instead of a non-deterministic pick"). In that combination — multi-org user, legacy route, no org headers, explicit virtualMcpId — organizationId stays null, both guards are skipped, and ctx.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 (no x-org-id/x-org-slug headers) 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 resolved organizationId, 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 virtualMcpId is requested but no organization context could be resolved, return 400 { 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 since organizationId is 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.ts has 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.

  • Bug Fixes
    • Added a guard in handleVirtualMcpRequest to return 400 when virtualMcpId is provided and no organization is resolved.
    • Prevents unscoped lookups for multi-org users on /mcp/gateway/:virtualMcpId and /mcp/virtual-mcp/:virtualMcpId; org-scoped /api/:org/... routes are unchanged.

Written for commit 4b00135. Summary will update on new commits.

Review in cubic

@pedrofrxncx
pedrofrxncx merged commit d345ae9 into main Jul 23, 2026
15 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/virtual-mcp-legacy-route-org-context-w7 branch July 23, 2026 19:50
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]>
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