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
26 changes: 26 additions & 0 deletions apps/sim/lib/mothership/tool-executor/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion apps/sim/lib/mothership/tool-executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}),
}

Expand Down
36 changes: 36 additions & 0 deletions apps/sim/tools/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}' }

Expand Down
21 changes: 20 additions & 1 deletion apps/sim/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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[] = []
Expand All @@ -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)'
Expand Down