Skip to content

Commit 3210b98

Browse files
committed
fix(tools): pin v2 tool execution credentials to the request workspace
POST /api/v2/tools/{toolId}/execute evaluated allowedIntegrations and deniedTools against the request workspace, but the executor authorized the caller-supplied credentialId with no workflow and no workspace, so a credential from another workspace the caller belongs to ran under the request workspace's policy. Authorize the credential against the request workspace before dispatch.
1 parent 507953c commit 3210b98

5 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎apps/docs/openapi-v2-resources.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6874,7 +6874,7 @@
68746874
"post": {
68756875
"operationId": "executeTool",
68766876
"summary": "Run tool",
6877-
"description": "Run a built-in tool using published parameter IDs. Requires workspace read access. Sim resolves `credentialId`, hosted keys, and whole-value `{{VAR_NAME}}` references in `user-only` parameters; other values pass through unchanged. Send connected credentials as top-level `credentialId`, not inside `input`. Third-party refusal returns `200` with `status: \"failed\"`; API failures use the error envelope. Hidden or missing tools return `404`; disallowed integrations return `403` with `error.details.code: INTEGRATION_NOT_ALLOWED`. Hosted-key usage is billed to the workspace. A call that would use a hosted key returns `402` when the workspace exceeds usage or billing limits. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.",
6877+
"description": "Run a built-in tool using published parameter IDs. Requires workspace read access. Sim resolves `credentialId`, hosted keys, and whole-value `{{VAR_NAME}}` references in `user-only` parameters; other values pass through unchanged. Send connected credentials as top-level `credentialId`, not inside `input`. Third-party refusal returns `200` with `status: \"failed\"`; API failures use the error envelope. Hidden or missing tools return `404`; disallowed integrations return `403` with `error.details.code: INTEGRATION_NOT_ALLOWED`. A `credentialId` you cannot use in this workspace returns `403`. Hosted-key usage is billed to the workspace. A call that would use a hosted key returns `402` when the workspace exceeds usage or billing limits. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.",
68786878
"x-sim-operation": "tools.execute",
68796879
"x-oauth-scope": "api:write",
68806880
"tags": ["Catalog"],

‎apps/sim/lib/api/contracts/v2/openapi/resources.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2434,7 +2434,7 @@ const declaredRoutes = [
24342434
applicationOperation: toolExecutionOperations.execute,
24352435
operationId: 'executeTool',
24362436
summary: 'Run tool',
2437-
description: `Run a built-in tool using published parameter IDs. Requires workspace read access. Sim resolves \`credentialId\`, hosted keys, and whole-value \`{{VAR_NAME}}\` references in \`user-only\` parameters; other values pass through unchanged. Send connected credentials as top-level \`credentialId\`, not inside \`input\`. Third-party refusal returns \`200\` with \`status: "failed"\`; API failures use the error envelope. Hidden or missing tools return \`404\`; disallowed integrations return \`403\` with \`error.details.code: INTEGRATION_NOT_ALLOWED\`. Hosted-key usage is billed to the workspace. A call that would use a hosted key returns \`402\` when the workspace exceeds usage or billing limits. ${WORKSPACE_API_KEY_DENIED}`,
2437+
description: `Run a built-in tool using published parameter IDs. Requires workspace read access. Sim resolves \`credentialId\`, hosted keys, and whole-value \`{{VAR_NAME}}\` references in \`user-only\` parameters; other values pass through unchanged. Send connected credentials as top-level \`credentialId\`, not inside \`input\`. Third-party refusal returns \`200\` with \`status: "failed"\`; API failures use the error envelope. Hidden or missing tools return \`404\`; disallowed integrations return \`403\` with \`error.details.code: INTEGRATION_NOT_ALLOWED\`. A \`credentialId\` you cannot use in this workspace returns \`403\`. Hosted-key usage is billed to the workspace. A call that would use a hosted key returns \`402\` when the workspace exceeds usage or billing limits. ${WORKSPACE_API_KEY_DENIED}`,
24382438
errors: [...RESOURCE_ERRORS, 'UsageLimitExceeded'],
24392439
success: { description: 'The outcome of the tool call.' },
24402440
}),

‎apps/sim/lib/api/mcp/generated/v2-operations.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ export const V2_MCP_OPERATIONS = {
13551355
contract: v2ExecuteToolContract,
13561356
summary: 'Run tool',
13571357
description:
1358-
'Run a built-in tool using published parameter IDs. Requires workspace read access. Sim resolves `credentialId`, hosted keys, and whole-value `{{VAR_NAME}}` references in `user-only` parameters; other values pass through unchanged. Send connected credentials as top-level `credentialId`, not inside `input`. Third-party refusal returns `200` with `status: "failed"`; API failures use the error envelope. Hidden or missing tools return `404`; disallowed integrations return `403` with `error.details.code: INTEGRATION_NOT_ALLOWED`. Hosted-key usage is billed to the workspace. A call that would use a hosted key returns `402` when the workspace exceeds usage or billing limits. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.',
1358+
'Run a built-in tool using published parameter IDs. Requires workspace read access. Sim resolves `credentialId`, hosted keys, and whole-value `{{VAR_NAME}}` references in `user-only` parameters; other values pass through unchanged. Send connected credentials as top-level `credentialId`, not inside `input`. Third-party refusal returns `200` with `status: "failed"`; API failures use the error envelope. Hidden or missing tools return `404`; disallowed integrations return `403` with `error.details.code: INTEGRATION_NOT_ALLOWED`. A `credentialId` you cannot use in this workspace returns `403`. Hosted-key usage is billed to the workspace. A call that would use a hosted key returns `402` when the workspace exceeds usage or billing limits. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.',
13591359
workspaceKeyUnsupported: true,
13601360
handler: () => import('@/app/api/v2/tools/[toolId]/execute/route').then((route) => route.POST),
13611361
},

‎apps/sim/lib/tool-execution/application/execute-tool.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import {
2121
blockVisibilityMock,
2222
blockVisibilityMockFns,
2323
} from '@sim/testing/mocks/block-visibility.mock'
24+
import {
25+
credentialsAccessMock,
26+
credentialsAccessMockFns,
27+
} from '@sim/testing/mocks/credentials-access.mock'
2428
import {
2529
customBlockOperationsMock,
2630
customBlockOperationsMockFns,
@@ -50,6 +54,8 @@ vi.mock('@sim/platform-authz/workspace', () => workspaceAuthzMock)
5054

5155
vi.mock('@sim/audit', () => auditMock)
5256

57+
vi.mock('@/lib/credentials/access', () => credentialsAccessMock)
58+
5359
vi.mock('@/lib/integrations/principal-scope.server', () => ({
5460
allowedIntegrationTypes: hoisted.allowedIntegrationTypes,
5561
principalUserId: (principal: { kind: string; userId?: string }) =>
@@ -258,6 +264,15 @@ function run(input: Partial<Parameters<typeof executeToolForCaller.execute>[0]['
258264
})
259265
}
260266

267+
function usableCredentialIn(workspaceId: string) {
268+
return {
269+
credential: { id: 'credential-1', workspaceId, type: 'service_account' },
270+
member: { role: 'member' },
271+
hasWorkspaceAccess: true,
272+
isAdmin: false,
273+
}
274+
}
275+
261276
describe('executeToolForCaller', () => {
262277
afterAll(resetEnvFlagsMock)
263278

@@ -286,6 +301,9 @@ describe('executeToolForCaller', () => {
286301
mocks.resolveBillingAttribution.mockResolvedValue({ workspaceId: WORKSPACE_ID })
287302
mocks.checkUsageLimits.mockResolvedValue({ isExceeded: false })
288303
environmentUtilsMockFns.mockGetEffectiveDecryptedEnv.mockResolvedValue({})
304+
credentialsAccessMockFns.mockGetCredentialActorContext.mockResolvedValue(
305+
usableCredentialIn(WORKSPACE_ID)
306+
)
289307
})
290308

291309
it.each<PersonalApiKeyPrincipal | SessionPrincipal>([principal, createSessionPrincipal()])(
@@ -415,6 +433,20 @@ describe('executeToolForCaller', () => {
415433
})
416434
})
417435

436+
it('refuses a credential from another workspace the caller can use', async () => {
437+
credentialsAccessMockFns.mockGetCredentialActorContext.mockResolvedValue(
438+
usableCredentialIn('workspace-a')
439+
)
440+
441+
await expect(
442+
run({ toolId: 'slack_message', credentialId: 'credential-1', input: { text: 'hi' } })
443+
).rejects.toMatchObject({
444+
code: 'forbidden',
445+
message: expect.stringContaining('credential you can use in this workspace'),
446+
})
447+
expect(mocks.executeRegistryTool).not.toHaveBeenCalled()
448+
})
449+
418450
it('requires a credential for an OAuth tool before it dispatches', async () => {
419451
await expect(run({ toolId: 'slack_message', input: { text: 'hi' } })).rejects.toMatchObject({
420452
code: 'validation',

‎apps/sim/lib/tool-execution/application/execute-tool.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { getErrorMessage } from '@sim/utils/errors'
33
import { generateId } from '@sim/utils/id'
4+
import { authorizeCredentialUseForAuth } from '@/lib/auth/credential-access'
45
import { resolveBillingAttribution, toBillingContext } from '@/lib/billing/core/attribution'
56
import { checkExecutionUsageLimits } from '@/lib/billing/core/usage-gate-cache'
67
import { recordUsage } from '@/lib/billing/core/usage-log'
@@ -361,6 +362,21 @@ export const executeToolForCaller = defineAuthorizedWorkspaceUseCase({
361362
throw new OrchestrationError('forbidden', 'Tool execution requires an acting user')
362363
}
363364

365+
// The executor authorizes credentials with no workflow, so only this ties the credential
366+
// to the workspace whose allowedIntegrations and deniedTools govern the call.
367+
if (input.credentialId) {
368+
const access = await authorizeCredentialUseForAuth(
369+
{ success: true, userId },
370+
{ credentialId: input.credentialId, workspaceId: context.workspaceId }
371+
)
372+
if (!access.ok) {
373+
throw new OrchestrationError(
374+
'forbidden',
375+
'credentialId is not a credential you can use in this workspace'
376+
)
377+
}
378+
}
379+
364380
const registry = mustClassifyContent(principal)
365381
? new ResolvedSecretTraceRegistry([], { userId, workspaceId: context.workspaceId })
366382
: undefined

0 commit comments

Comments
 (0)