From aad315250c8dd00b826f0a62f34abe4165536964 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 22 Nov 2026 11:59:07 -0800 Subject: [PATCH] fix(copilot): hold app tools to the turn secret authority Integration tools on the Copilot app-tool lane resolved {{VAR}} references in user-only params from the acting user's environment without consulting the turn's secret authority. A run with no secret actor now resolves no reference, and the turn's secret mount policy bounds which names may resolve, matching what function_execute already enforces for sandbox mounts. --- .../mothership/tool-executor/executor.test.ts | 26 ++++++++++++++ .../lib/mothership/tool-executor/executor.ts | 6 +++- apps/sim/tools/index.test.ts | 36 +++++++++++++++++++ apps/sim/tools/index.ts | 21 ++++++++++- 4 files changed, 87 insertions(+), 2 deletions(-) diff --git a/apps/sim/lib/mothership/tool-executor/executor.test.ts b/apps/sim/lib/mothership/tool-executor/executor.test.ts index 3afbeb914b7..92342debe39 100644 --- a/apps/sim/lib/mothership/tool-executor/executor.test.ts +++ b/apps/sim/lib/mothership/tool-executor/executor.test.ts @@ -454,6 +454,32 @@ describe('copilot tool executor fallback', () => { ) }) + it('holds app tools to the turn secret authority over a model-supplied _context', async () => { + isKnownTool.mockReturnValue(false) + isSimExecuted.mockReturnValue(false) + executeAppTool.mockResolvedValue({ success: true, output: {} }) + const modelContext = { + envReferenceMode: 'explicit-and-bare', + secretMountPolicy: { secretScope: 'all', mountedSecrets: [] }, + } + const secretMountPolicy = { secretScope: 'selected' as const, mountedSecrets: ['SLACK_TOKEN'] } + + await executeTool( + 'a2a_send_message', + { agentUrl: 'https://agent.example', apiKey: '{{OPENAI_API_KEY}}', _context: modelContext }, + { userId: 'owner-1', workflowId: '', workspaceId: 'ws-1', secretActorUserId: null } + ) + await executeTool( + 'a2a_send_message', + { agentUrl: 'https://agent.example', apiKey: '{{OPENAI_API_KEY}}', _context: modelContext }, + { userId: 'user-1', workflowId: '', workspaceId: 'ws-1', secretMountPolicy } + ) + + const [actorless, attributed] = executeAppTool.mock.calls.map(([, params]) => params._context) + expect(actorless).toMatchObject({ envReferenceMode: 'off' }) + expect(attributed).toMatchObject({ envReferenceMode: undefined, secretMountPolicy }) + }) + it('passes trace provenance out-of-band without exposing it in app tool parameters', async () => { isKnownTool.mockReturnValue(false) isSimExecuted.mockReturnValue(false) diff --git a/apps/sim/lib/mothership/tool-executor/executor.ts b/apps/sim/lib/mothership/tool-executor/executor.ts index b46063cb85b..12187c8fb75 100644 --- a/apps/sim/lib/mothership/tool-executor/executor.ts +++ b/apps/sim/lib/mothership/tool-executor/executor.ts @@ -360,7 +360,11 @@ function buildAppToolParams( requestMode: context.requestMode, currentAgentId: context.currentAgentId, enforceCredentialAccess: true, - ...(context.requestMode === 'assistant' ? { envReferenceMode: 'off' } : {}), + // The secret authority `function_execute` applies, always written so a model-supplied + // `_context` cannot set it: no secret actor resolves nothing; the policy bounds names. + envReferenceMode: + context.requestMode === 'assistant' || context.secretActorUserId === null ? 'off' : undefined, + secretMountPolicy: context.secretMountPolicy, ...(context.billingAttribution ? { billingAttribution: context.billingAttribution } : {}), } diff --git a/apps/sim/tools/index.test.ts b/apps/sim/tools/index.test.ts index 7020b8c3ced..d82ec720d64 100644 --- a/apps/sim/tools/index.test.ts +++ b/apps/sim/tools/index.test.ts @@ -7150,6 +7150,42 @@ describe('Copilot Env Variable Reference Resolution', () => { expect(sentOperationInput().apiKey).toBe('{{SENTRY_AUTH_TOKEN}}') }) + it('resolves only names the turn secret mount policy allows', async () => { + mockGetEffectiveEnvironmentSnapshot.mockResolvedValue( + environmentSnapshot({ SENTRY_AUTH_TOKEN: 'sntrys_real_token', OPENAI_API_KEY: 'sk-real' }) + ) + const _context = { + secretMountPolicy: { secretScope: 'selected', mountedSecrets: ['SENTRY_AUTH_TOKEN'] }, + } + + const refused = await executeTool( + 'test_env_ref_tool', + { apiKey: '{{OPENAI_API_KEY}}', _context }, + { executionContext: copilotContext() } + ) + expect(refused).toMatchObject({ + success: false, + error: expect.stringContaining('Secret access is not allowed for: OPENAI_API_KEY'), + }) + expect(mockExecuteInternalToolOperation).not.toHaveBeenCalled() + + const bare = await executeTool( + 'test_env_ref_tool', + { apiKey: 'OPENAI_API_KEY', _context }, + { executionContext: copilotContext() } + ) + expect(bare.success).toBe(true) + expect(sentOperationInput().apiKey).toBe('OPENAI_API_KEY') + + const allowed = await executeTool( + 'test_env_ref_tool', + { apiKey: '{{SENTRY_AUTH_TOKEN}}', _context }, + { executionContext: copilotContext() } + ) + expect(allowed.success).toBe(true) + expect(sentOperationInput().apiKey).toBe('sntrys_real_token') + }) + it('never mutates the caller-owned params object (log-leak guard)', async () => { const callerParams = { apiKey: '{{SENTRY_AUTH_TOKEN}}' } diff --git a/apps/sim/tools/index.ts b/apps/sim/tools/index.ts index 15fbf79284e..9e7f8902a1a 100644 --- a/apps/sim/tools/index.ts +++ b/apps/sim/tools/index.ts @@ -100,6 +100,11 @@ import { recordServiceCost, recordServiceMeteringFailure, } from '@/lib/mothership/billing/service-observer' +import { + applySecretMountPolicy, + filterSecretNamesByMountPolicy, + type SecretMountPolicy, +} from '@/lib/mothership/secret-mount-policy' import { CredentialRevokedError } from '@/lib/oauth/credential-revoked' import type { CredentialTokenPayload } from '@/lib/oauth/token-resolution' import { isFalAICostSource } from '@/lib/tools/falai-pricing' @@ -189,6 +194,8 @@ interface ToolExecutionScope { enforceCredentialAccess?: boolean copilotToolExecution?: boolean envReferenceMode?: ToolEnvReferenceMode + /** A Copilot turn's secret allowlist; names outside it never resolve into a param. */ + secretMountPolicy?: SecretMountPolicy billingAttribution?: BillingAttributionSnapshot } @@ -222,6 +229,7 @@ function resolveToolScope( ((executionContext?.copilotToolExecution ?? ctx?.copilotToolExecution) ? 'explicit-and-bare' : 'off'), + secretMountPolicy: ctx?.secretMountPolicy as SecretMountPolicy | undefined, billingAttribution: (executionContext?.metadata.billingAttribution ?? ctx?.billingAttribution) as BillingAttributionSnapshot | undefined, } @@ -462,7 +470,15 @@ async function resolveToolEnvReferences( const environmentScope = { userId: scope.userId, workspaceId: scope.workspaceId } const { getEffectiveEnvironmentSnapshot } = await import('@/lib/environment/utils') const environment = await getEffectiveEnvironmentSnapshot(scope.userId, scope.workspaceId) - const envVars = { ...environment.personalDecrypted, ...environment.workspaceDecrypted } + const availableEnvVars = { + ...environment.personalDecrypted, + ...environment.workspaceDecrypted, + } + const envVars = Object.fromEntries( + filterSecretNamesByMountPolicy(Object.keys(availableEnvVars), scope.secretMountPolicy).map( + (name) => [name, availableEnvVars[name]] + ) + ) for (const { paramId, value, soft } of pending) { const missingKeys: string[] = [] @@ -482,6 +498,9 @@ async function resolveToolEnvReferences( // A bare name that matches no variable is treated as the literal // credential it probably is; only explicit reference forms error. if (soft) continue + if (Object.hasOwn(availableEnvVars, missingKeys[0])) { + applySecretMountPolicy(missingKeys, scope.secretMountPolicy) + } const scopeHint = scope.workspaceId ? '' : ' (no workspace context — only personal variables are available here)'