Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/sim/app/api/mcp/serve/[serverId]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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([
Expand Down
21 changes: 20 additions & 1 deletion apps/sim/app/api/mcp/serve/[serverId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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) }
Expand Down
Loading