diff --git a/apps/sim/app/api/mcp/serve/[serverId]/route.test.ts b/apps/sim/app/api/mcp/serve/[serverId]/route.test.ts index e450390e946..6f0a61fd6a1 100644 --- a/apps/sim/app/api/mcp/serve/[serverId]/route.test.ts +++ b/apps/sim/app/api/mcp/serve/[serverId]/route.test.ts @@ -19,6 +19,11 @@ import { executionLimitsMockFns, } from '@sim/testing/mocks/execution-limits.mock' import { hybridAuthMockFns } from '@sim/testing/mocks/hybrid-auth.mock' +import { + MockPublicApiNotAllowedError, + permissionCheckMock, + permissionCheckMockFns, +} from '@sim/testing/mocks/permission-check.mock' import { permissionsMock, permissionsMockFns } from '@sim/testing/mocks/permissions.mock' import { NextRequest } from 'next/server' import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' @@ -43,6 +48,8 @@ vi.mock('@/lib/core/security/encryption', () => encryptionMock) vi.mock('@/lib/billing/core/attribution', () => billingAttributionMock) +vi.mock('@/ee/access-control/utils/permission-check', () => permissionCheckMock) + const { mockAssertBillingAttributionSnapshot, mockResolveBillingAttribution, @@ -647,6 +654,31 @@ describe('MCP Serve Route', () => { expect(mockExecuteWorkflowService).not.toHaveBeenCalled() }) + it('refuses a public server tool call when public execution is governed off', async () => { + dbChainMockFns.limit.mockResolvedValueOnce([ + { + id: 'server-1', + name: 'Public Server', + workspaceId: 'ws-1', + isPublic: true, + createdBy: 'owner-1', + workspaceBilledAccountUserId: 'payer-1', + }, + ]) + permissionCheckMockFns.mockValidatePublicApiAllowed.mockRejectedValueOnce( + new MockPublicApiNotAllowedError() + ) + + const response = await POST(toolCallRequest({}), createRouteContext({ serverId: 'server-1' })) + + expect(response.status).toBe(403) + expect(permissionCheckMockFns.mockValidatePublicApiAllowed).toHaveBeenCalledWith( + 'payer-1', + 'ws-1' + ) + expect(mockExecuteWorkflowService).not.toHaveBeenCalled() + }) + it('replaces caller-supplied attribution for public workflow tools', async () => { dbChainMockFns.limit .mockResolvedValueOnce([ diff --git a/apps/sim/app/api/mcp/serve/[serverId]/route.ts b/apps/sim/app/api/mcp/serve/[serverId]/route.ts index 7bfefdb42dd..02ef61e0dbe 100644 --- a/apps/sim/app/api/mcp/serve/[serverId]/route.ts +++ b/apps/sim/app/api/mcp/serve/[serverId]/route.ts @@ -87,6 +87,10 @@ import { buildWorkflowMcpServerUrl } from '@/lib/mcp/urls' import { getMeaningfulWorkflowDescription } from '@/lib/mcp/workflow-tool-schema' import { executeWorkflowService } from '@/lib/workflows/executor/execute-service' import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' +import { + PublicApiNotAllowedError, + validatePublicApiAllowed, +} from '@/ee/access-control/utils/permission-check' import { projectResolvedSecretModelContent } from '@/executor/utils/resolved-secret-content-projection' import { refuseResolvedSecretProjection } from '@/executor/utils/resolved-secret-projection-refusal' import { @@ -453,6 +457,7 @@ async function getServer(serverId: string) { isPublic: workflowMcpServer.isPublic, createdBy: workflowMcpServer.createdBy, workspaceAllowsPersonalApiKeys: workspace.allowPersonalApiKeys, + workspaceBilledAccountUserId: workspace.billedAccountUserId, }) .from(workflowMcpServer) .innerJoin(workspace, eq(workflowMcpServer.workspaceId, workspace.id)) @@ -561,7 +566,21 @@ async function authorizeMcpServeRequest( server: WorkflowMcpServeServer, options: { requireAuthForPublic?: boolean } = {} ): Promise<{ response?: NextResponse; executeAuthContext?: ExecuteAuthContext }> { - if (server.isPublic && !options.requireAuthForPublic) return {} + if (server.isPublic) { + // The public-API gate, checked per request so a revocation reaches servers already public. + try { + await validatePublicApiAllowed(server.workspaceBilledAccountUserId, server.workspaceId) + } catch (error) { + if (!(error instanceof PublicApiNotAllowedError)) throw error + return { + response: NextResponse.json( + { error: 'Public access to this MCP server is disabled' }, + { status: 403 } + ), + } + } + if (!options.requireAuthForPublic) return {} + } const auth = await authenticateMcpServeRequest(request, server.id) if (auth === 'invalid_token') return { response: unauthorizedResponse(server.id, true) }