Skip to content

Commit 461d49c

Browse files
authored
fix(copilot): hold app tools to the turn secret authority (#10320)
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.
1 parent 683064b commit 461d49c

4 files changed

Lines changed: 87 additions & 2 deletions

File tree

‎apps/sim/lib/mothership/tool-executor/executor.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,32 @@ describe('copilot tool executor fallback', () => {
454454
)
455455
})
456456

457+
it('holds app tools to the turn secret authority over a model-supplied _context', async () => {
458+
isKnownTool.mockReturnValue(false)
459+
isSimExecuted.mockReturnValue(false)
460+
executeAppTool.mockResolvedValue({ success: true, output: {} })
461+
const modelContext = {
462+
envReferenceMode: 'explicit-and-bare',
463+
secretMountPolicy: { secretScope: 'all', mountedSecrets: [] },
464+
}
465+
const secretMountPolicy = { secretScope: 'selected' as const, mountedSecrets: ['SLACK_TOKEN'] }
466+
467+
await executeTool(
468+
'a2a_send_message',
469+
{ agentUrl: 'https://agent.example', apiKey: '{{OPENAI_API_KEY}}', _context: modelContext },
470+
{ userId: 'owner-1', workflowId: '', workspaceId: 'ws-1', secretActorUserId: null }
471+
)
472+
await executeTool(
473+
'a2a_send_message',
474+
{ agentUrl: 'https://agent.example', apiKey: '{{OPENAI_API_KEY}}', _context: modelContext },
475+
{ userId: 'user-1', workflowId: '', workspaceId: 'ws-1', secretMountPolicy }
476+
)
477+
478+
const [actorless, attributed] = executeAppTool.mock.calls.map(([, params]) => params._context)
479+
expect(actorless).toMatchObject({ envReferenceMode: 'off' })
480+
expect(attributed).toMatchObject({ envReferenceMode: undefined, secretMountPolicy })
481+
})
482+
457483
it('passes trace provenance out-of-band without exposing it in app tool parameters', async () => {
458484
isKnownTool.mockReturnValue(false)
459485
isSimExecuted.mockReturnValue(false)

‎apps/sim/lib/mothership/tool-executor/executor.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ function buildAppToolParams(
360360
requestMode: context.requestMode,
361361
currentAgentId: context.currentAgentId,
362362
enforceCredentialAccess: true,
363-
...(context.requestMode === 'assistant' ? { envReferenceMode: 'off' } : {}),
363+
// The secret authority `function_execute` applies, always written so a model-supplied
364+
// `_context` cannot set it: no secret actor resolves nothing; the policy bounds names.
365+
envReferenceMode:
366+
context.requestMode === 'assistant' || context.secretActorUserId === null ? 'off' : undefined,
367+
secretMountPolicy: context.secretMountPolicy,
364368
...(context.billingAttribution ? { billingAttribution: context.billingAttribution } : {}),
365369
}
366370

‎apps/sim/tools/index.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7150,6 +7150,42 @@ describe('Copilot Env Variable Reference Resolution', () => {
71507150
expect(sentOperationInput().apiKey).toBe('{{SENTRY_AUTH_TOKEN}}')
71517151
})
71527152

7153+
it('resolves only names the turn secret mount policy allows', async () => {
7154+
mockGetEffectiveEnvironmentSnapshot.mockResolvedValue(
7155+
environmentSnapshot({ SENTRY_AUTH_TOKEN: 'sntrys_real_token', OPENAI_API_KEY: 'sk-real' })
7156+
)
7157+
const _context = {
7158+
secretMountPolicy: { secretScope: 'selected', mountedSecrets: ['SENTRY_AUTH_TOKEN'] },
7159+
}
7160+
7161+
const refused = await executeTool(
7162+
'test_env_ref_tool',
7163+
{ apiKey: '{{OPENAI_API_KEY}}', _context },
7164+
{ executionContext: copilotContext() }
7165+
)
7166+
expect(refused).toMatchObject({
7167+
success: false,
7168+
error: expect.stringContaining('Secret access is not allowed for: OPENAI_API_KEY'),
7169+
})
7170+
expect(mockExecuteInternalToolOperation).not.toHaveBeenCalled()
7171+
7172+
const bare = await executeTool(
7173+
'test_env_ref_tool',
7174+
{ apiKey: 'OPENAI_API_KEY', _context },
7175+
{ executionContext: copilotContext() }
7176+
)
7177+
expect(bare.success).toBe(true)
7178+
expect(sentOperationInput().apiKey).toBe('OPENAI_API_KEY')
7179+
7180+
const allowed = await executeTool(
7181+
'test_env_ref_tool',
7182+
{ apiKey: '{{SENTRY_AUTH_TOKEN}}', _context },
7183+
{ executionContext: copilotContext() }
7184+
)
7185+
expect(allowed.success).toBe(true)
7186+
expect(sentOperationInput().apiKey).toBe('sntrys_real_token')
7187+
})
7188+
71537189
it('never mutates the caller-owned params object (log-leak guard)', async () => {
71547190
const callerParams = { apiKey: '{{SENTRY_AUTH_TOKEN}}' }
71557191

‎apps/sim/tools/index.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ import {
101101
recordServiceCost,
102102
recordServiceMeteringFailure,
103103
} from '@/lib/mothership/billing/service-observer'
104+
import {
105+
applySecretMountPolicy,
106+
filterSecretNamesByMountPolicy,
107+
type SecretMountPolicy,
108+
} from '@/lib/mothership/secret-mount-policy'
104109
import { CredentialRevokedError } from '@/lib/oauth/credential-revoked'
105110
import type { CredentialTokenPayload } from '@/lib/oauth/token-resolution'
106111
import { isFalAICostSource } from '@/lib/tools/falai-pricing'
@@ -186,6 +191,8 @@ interface ToolExecutionScope {
186191
enforceCredentialAccess?: boolean
187192
copilotToolExecution?: boolean
188193
envReferenceMode?: ToolEnvReferenceMode
194+
/** A Copilot turn's secret allowlist; names outside it never resolve into a param. */
195+
secretMountPolicy?: SecretMountPolicy
189196
billingAttribution?: BillingAttributionSnapshot
190197
}
191198

@@ -219,6 +226,7 @@ function resolveToolScope(
219226
((executionContext?.copilotToolExecution ?? ctx?.copilotToolExecution)
220227
? 'explicit-and-bare'
221228
: 'off'),
229+
secretMountPolicy: ctx?.secretMountPolicy as SecretMountPolicy | undefined,
222230
billingAttribution: (executionContext?.metadata.billingAttribution ??
223231
ctx?.billingAttribution) as BillingAttributionSnapshot | undefined,
224232
}
@@ -459,7 +467,15 @@ async function resolveToolEnvReferences(
459467
const environmentScope = { userId: scope.userId, workspaceId: scope.workspaceId }
460468
const { getEffectiveEnvironmentSnapshot } = await import('@/lib/environment/utils')
461469
const environment = await getEffectiveEnvironmentSnapshot(scope.userId, scope.workspaceId)
462-
const envVars = { ...environment.personalDecrypted, ...environment.workspaceDecrypted }
470+
const availableEnvVars = {
471+
...environment.personalDecrypted,
472+
...environment.workspaceDecrypted,
473+
}
474+
const envVars = Object.fromEntries(
475+
filterSecretNamesByMountPolicy(Object.keys(availableEnvVars), scope.secretMountPolicy).map(
476+
(name) => [name, availableEnvVars[name]]
477+
)
478+
)
463479

464480
for (const { paramId, value, soft } of pending) {
465481
const missingKeys: string[] = []
@@ -479,6 +495,9 @@ async function resolveToolEnvReferences(
479495
// A bare name that matches no variable is treated as the literal
480496
// credential it probably is; only explicit reference forms error.
481497
if (soft) continue
498+
if (Object.hasOwn(availableEnvVars, missingKeys[0])) {
499+
applySecretMountPolicy(missingKeys, scope.secretMountPolicy)
500+
}
482501
const scopeHint = scope.workspaceId
483502
? ''
484503
: ' (no workspace context — only personal variables are available here)'

0 commit comments

Comments
 (0)