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
22 changes: 21 additions & 1 deletion apps/sim/app/api/mcp/oauth/start/route.test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,8 +17,10 @@ import {
import { NextRequest } from 'next/server'
import { beforeEach, describe, expect, it, vi } from 'vitest'

const { mockMcpAuth } = vi.hoisted(() => ({
const { mockMcpAuth, mockCreateSsrfGuardedMcpFetch, mockGuardedFetch } = vi.hoisted(() => ({
mockMcpAuth: vi.fn(),
mockCreateSsrfGuardedMcpFetch: vi.fn(),
mockGuardedFetch: vi.fn(),
}))

vi.mock('@sim/db', () => dbChainMock)
Expand All@@ -31,6 +33,9 @@ vi.mock('drizzle-orm', () => ({
vi.mock('@modelcontextprotocol/sdk/client/auth.js', () => ({
auth: mockMcpAuth,
}))
vi.mock('@/lib/mcp/pinned-fetch', () => ({
createSsrfGuardedMcpFetch: mockCreateSsrfGuardedMcpFetch,
}))
vi.mock('@/lib/auth/hybrid', () => hybridAuthMock)
vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock)
vi.mock('@/lib/mcp/oauth', () => mcpOauthMock)
Expand DownExpand Up@@ -73,6 +78,21 @@ describe('MCP OAuth start route', () => {
})
mcpOauthMockFns.mockLoadPreregisteredClient.mockResolvedValue(undefined)
mockMcpAuth.mockRejectedValue(new McpOauthRedirectRequiredMock('https://mcp.exa.ai/authorize'))
mockCreateSsrfGuardedMcpFetch.mockReturnValue(mockGuardedFetch)
})

it('routes OAuth discovery through the SSRF-guarded fetch', async () => {
const request = new NextRequest(
'http://localhost:3000/api/mcp/oauth/start?workspaceId=workspace-1&serverId=server-1'
)

await GET(request)

expect(mockCreateSsrfGuardedMcpFetch).toHaveBeenCalledTimes(1)
expect(mockMcpAuth).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ serverUrl: 'https://mcp.exa.ai/mcp', fetchFn: mockGuardedFetch })
)
})

it('requires workspace write permission via MCP auth middleware', async () => {
Expand Down
6 changes: 5 additions & 1 deletion apps/sim/app/api/mcp/oauth/start/route.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,7 @@ import {
SimMcpOauthProvider,
setOauthRowUser,
} from '@/lib/mcp/oauth'
import { createSsrfGuardedMcpFetch } from '@/lib/mcp/pinned-fetch'
import { createMcpErrorResponse } from '@/lib/mcp/utils'

const logger = createLogger('McpOauthStartAPI')
Expand DownExpand Up@@ -129,7 +130,10 @@ export const GET = withRouteHandler(
const provider = new SimMcpOauthProvider({ row, preregistered })

try {
const result = await mcpAuth(provider, { serverUrl: server.url })
const result = await mcpAuth(provider, {
serverUrl: server.url,
fetchFn: createSsrfGuardedMcpFetch(),
})
if (result === 'AUTHORIZED') {
return NextResponse.json({ status: 'already_authorized' })
}
Expand Down
Loading