Skip to content

Commit fd21651

Browse files
authored
fix(mcp): gate public workflow MCP servers on public API governance (#10056)
A public workflow MCP server runs deployed workflows for unauthenticated callers but never checked the DISABLE_PUBLIC_API kill switch or the public_api.use permission-group capability that the anonymous public-API execute routes enforce. The serve route now runs validatePublicApiAllowed with the workspace billing account on every request to a public server and refuses with 403 when public execution is disabled.
1 parent 9c68a74 commit fd21651

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎apps/sim/app/api/mcp/serve/[serverId]/route.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import {
1919
executionLimitsMockFns,
2020
} from '@sim/testing/mocks/execution-limits.mock'
2121
import { hybridAuthMockFns } from '@sim/testing/mocks/hybrid-auth.mock'
22+
import {
23+
MockPublicApiNotAllowedError,
24+
permissionCheckMock,
25+
permissionCheckMockFns,
26+
} from '@sim/testing/mocks/permission-check.mock'
2227
import { permissionsMock, permissionsMockFns } from '@sim/testing/mocks/permissions.mock'
2328
import { NextRequest } from 'next/server'
2429
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
@@ -43,6 +48,8 @@ vi.mock('@/lib/core/security/encryption', () => encryptionMock)
4348

4449
vi.mock('@/lib/billing/core/attribution', () => billingAttributionMock)
4550

51+
vi.mock('@/ee/access-control/utils/permission-check', () => permissionCheckMock)
52+
4653
const {
4754
mockAssertBillingAttributionSnapshot,
4855
mockResolveBillingAttribution,
@@ -647,6 +654,31 @@ describe('MCP Serve Route', () => {
647654
expect(mockExecuteWorkflowService).not.toHaveBeenCalled()
648655
})
649656

657+
it('refuses a public server tool call when public execution is governed off', async () => {
658+
dbChainMockFns.limit.mockResolvedValueOnce([
659+
{
660+
id: 'server-1',
661+
name: 'Public Server',
662+
workspaceId: 'ws-1',
663+
isPublic: true,
664+
createdBy: 'owner-1',
665+
workspaceBilledAccountUserId: 'payer-1',
666+
},
667+
])
668+
permissionCheckMockFns.mockValidatePublicApiAllowed.mockRejectedValueOnce(
669+
new MockPublicApiNotAllowedError()
670+
)
671+
672+
const response = await POST(toolCallRequest({}), createRouteContext({ serverId: 'server-1' }))
673+
674+
expect(response.status).toBe(403)
675+
expect(permissionCheckMockFns.mockValidatePublicApiAllowed).toHaveBeenCalledWith(
676+
'payer-1',
677+
'ws-1'
678+
)
679+
expect(mockExecuteWorkflowService).not.toHaveBeenCalled()
680+
})
681+
650682
it('replaces caller-supplied attribution for public workflow tools', async () => {
651683
dbChainMockFns.limit
652684
.mockResolvedValueOnce([

‎apps/sim/app/api/mcp/serve/[serverId]/route.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ import { buildWorkflowMcpServerUrl } from '@/lib/mcp/urls'
8787
import { getMeaningfulWorkflowDescription } from '@/lib/mcp/workflow-tool-schema'
8888
import { executeWorkflowService } from '@/lib/workflows/executor/execute-service'
8989
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
90+
import {
91+
PublicApiNotAllowedError,
92+
validatePublicApiAllowed,
93+
} from '@/ee/access-control/utils/permission-check'
9094
import { projectResolvedSecretModelContent } from '@/executor/utils/resolved-secret-content-projection'
9195
import { refuseResolvedSecretProjection } from '@/executor/utils/resolved-secret-projection-refusal'
9296
import {
@@ -453,6 +457,7 @@ async function getServer(serverId: string) {
453457
isPublic: workflowMcpServer.isPublic,
454458
createdBy: workflowMcpServer.createdBy,
455459
workspaceAllowsPersonalApiKeys: workspace.allowPersonalApiKeys,
460+
workspaceBilledAccountUserId: workspace.billedAccountUserId,
456461
})
457462
.from(workflowMcpServer)
458463
.innerJoin(workspace, eq(workflowMcpServer.workspaceId, workspace.id))
@@ -561,7 +566,21 @@ async function authorizeMcpServeRequest(
561566
server: WorkflowMcpServeServer,
562567
options: { requireAuthForPublic?: boolean } = {}
563568
): Promise<{ response?: NextResponse; executeAuthContext?: ExecuteAuthContext }> {
564-
if (server.isPublic && !options.requireAuthForPublic) return {}
569+
if (server.isPublic) {
570+
// The public-API gate, checked per request so a revocation reaches servers already public.
571+
try {
572+
await validatePublicApiAllowed(server.workspaceBilledAccountUserId, server.workspaceId)
573+
} catch (error) {
574+
if (!(error instanceof PublicApiNotAllowedError)) throw error
575+
return {
576+
response: NextResponse.json(
577+
{ error: 'Public access to this MCP server is disabled' },
578+
{ status: 403 }
579+
),
580+
}
581+
}
582+
if (!options.requireAuthForPublic) return {}
583+
}
565584

566585
const auth = await authenticateMcpServeRequest(request, server.id)
567586
if (auth === 'invalid_token') return { response: unauthorizedResponse(server.id, true) }

0 commit comments

Comments
 (0)