From c50c9cadbfe99d255a7d0dabc11e6cf981602c16 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 2 Dec 2026 16:22:07 -0600 Subject: [PATCH 1/3] feat(audit): record integration tool calls made through the v2 API and Sim MCP --- .../docs/platform/enterprise/audit-logs.mdx | 3 ++ .../application/execute-tool.test.ts | 52 ++++++++++++++++++- .../application/execute-tool.ts | 25 +++++++++ packages/audit/src/types.ts | 8 +++ packages/testing/src/mocks/audit.mock.ts | 2 + 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/apps/docs/content/docs/platform/enterprise/audit-logs.mdx b/apps/docs/content/docs/platform/enterprise/audit-logs.mdx index 3c51f3aeed4..724fcae7923 100644 --- a/apps/docs/content/docs/platform/enterprise/audit-logs.mdx +++ b/apps/docs/content/docs/platform/enterprise/audit-logs.mdx @@ -103,6 +103,9 @@ Events use identifiers such as `resource.action`. These categories are represent | Organization | `organization.updated`, `org_member.added`, `org_member.role_changed`, `organization.sso_provider.updated` | | Links | `sim_link.created`, `sim_link.updated`, `sim_link.deleted` | | Exports and drains | `audit_logs.exported`, `data_drain.created`, `data_drain.updated`, `data_drain.ran`, `data_drain.tested` | +| Integration tools | `tool.executed` | + +A built-in integration tool run directly through the v2 API or Sim's MCP server, for example from Claude Code or Cursor, records `tool.executed` when the tool changes something outside Sim, such as sending a message or creating an issue. The entry names the person, the tool (`resourceId`, such as `slack_message`), whether the provider call succeeded or failed (`metadata.status`), and the credential used (`metadata.credentialId`), and its surface is `mcp` for calls made through the MCP server. Tool inputs and outputs are not copied into the entry. Read-only tools are not recorded per call; the credential they use records `credential.accessed`. Authentication failures and password-reset requests use an anonymous actor and record the target account separately. Impersonation and administrative session revocations identify the operator and target organization. Table-row mutations record summaries and counts without copying row contents. A workflow or workspace written from source by the CLI (`workflow.created`, `workflow.updated` or `workspace.updated`) records `metadata.client` as `cli` and, when the source directory is in a git repository and `git` can read them, `metadata.gitSha` (the commit checked out) and `metadata.repo` (the `origin` remote URL, with any credentials removed). Either may be absent, for example `repo` in a repository with no `origin`. diff --git a/apps/sim/lib/tool-execution/application/execute-tool.test.ts b/apps/sim/lib/tool-execution/application/execute-tool.test.ts index a00c072793b..ee63e3f4be6 100644 --- a/apps/sim/lib/tool-execution/application/execute-tool.test.ts +++ b/apps/sim/lib/tool-execution/application/execute-tool.test.ts @@ -4,7 +4,7 @@ import { createSessionPrincipal, createWorkspaceApiKeyPrincipal, } from '@sim/testing/factories/principal.factory' -import { auditMock } from '@sim/testing/mocks/audit.mock' +import { auditMock, auditMockFns } from '@sim/testing/mocks/audit.mock' import { billingAttributionMock, billingAttributionMockFns, @@ -117,6 +117,7 @@ const TOOL_METADATA: Record> = { slack_message: { id: 'slack_message', name: 'Slack Send Message', + effect: 'write', params: { accessToken: { type: 'string', required: true, visibility: 'hidden' }, text: { type: 'string', required: true, visibility: 'user-or-llm' }, @@ -126,6 +127,7 @@ const TOOL_METADATA: Record> = { firecrawl_scrape: { id: 'firecrawl_scrape', name: 'Firecrawl Scrape', + effect: 'read', params: { url: { type: 'string', required: true, visibility: 'user-or-llm' }, apiKey: { type: 'string', required: true, visibility: 'user-only' }, @@ -433,6 +435,54 @@ describe('executeToolForCaller', () => { }) }) + describe('audit trail', () => { + const sendSlack = () => + run({ toolId: 'slack_message', credentialId: 'credential-1', input: { text: 'secret plan' } }) + + it('records a tool that changes something outside Sim, without copying its input', async () => { + await sendSlack() + + expect(auditMockFns.mockRecordAudit).toHaveBeenCalledTimes(1) + const entry = auditMockFns.mockRecordAudit.mock.calls[0][0] + expect(entry).toMatchObject({ + workspaceId: WORKSPACE_ID, + actorId: 'user-1', + action: 'tool.executed', + resourceType: 'tool', + resourceId: 'slack_message', + resourceName: 'Slack Send Message', + metadata: { toolId: 'slack_message', status: 'succeeded', credentialId: 'credential-1' }, + }) + expect(JSON.stringify(entry)).not.toContain('secret plan') + }) + + it('records a write the provider refused, since it may still have reached them', async () => { + mocks.executeRegistryTool.mockResolvedValue({ success: false, error: 'channel_not_found' }) + + await sendSlack() + + expect(auditMockFns.mockRecordAudit.mock.calls[0][0]).toMatchObject({ + description: 'Ran Slack Send Message (failed)', + metadata: { status: 'failed' }, + }) + }) + + it('leaves reads to the credential trail', async () => { + await run({ input: { url: 'https://example.com' } }) + + expect(mocks.executeRegistryTool).toHaveBeenCalled() + expect(auditMockFns.mockRecordAudit).not.toHaveBeenCalled() + }) + + it('records nothing for a call refused before it dispatched', async () => { + await expect(run({ toolId: 'slack_message', input: { text: 'hi' } })).rejects.toMatchObject({ + code: 'validation', + }) + + expect(auditMockFns.mockRecordAudit).not.toHaveBeenCalled() + }) + }) + it('refuses a credential from another workspace the caller can use', async () => { credentialsAccessMockFns.mockGetCredentialActorContext.mockResolvedValue( usableCredentialIn('workspace-a') diff --git a/apps/sim/lib/tool-execution/application/execute-tool.ts b/apps/sim/lib/tool-execution/application/execute-tool.ts index e0e9a4ee81d..a3ca0f0aeb9 100644 --- a/apps/sim/lib/tool-execution/application/execute-tool.ts +++ b/apps/sim/lib/tool-execution/application/execute-tool.ts @@ -1,3 +1,4 @@ +import { AuditAction, AuditResourceType } from '@sim/audit' import { createLogger } from '@sim/logger' import { getErrorMessage } from '@sim/utils/errors' import { generateId } from '@sim/utils/id' @@ -298,6 +299,13 @@ function assertRequiredCallerInputsPresent( * under `enforceCredentialAccess`, injects a hosted API key where Sim supplies * one, applies the `deniedTools` denylist against the resolved id, and projects * secrets out of the result. + * + * A call that dispatched to a tool which changes something outside Sim is + * audited as `tool.executed`, whether the provider succeeded or not: the send, + * post or delete may have reached the third party either way. Reads are left to + * the credential's own `credential.accessed` trail, which keeps the audit log at + * human scale when a coding agent lists or searches in a loop. Inputs and outputs + * are never copied into the entry; they can hold message bodies and secrets. */ export const executeToolForCaller = defineAuthorizedWorkspaceUseCase({ operation: toolExecutionOperations.execute, @@ -502,6 +510,23 @@ export const executeToolForCaller = defineAuthorizedWorkspaceUseCase({ error: result.success ? null : { message: result.error ?? `${toolId} did not succeed` }, } }, + projectAudit: ({ input, result }) => { + const tool = getTool(result.toolId) + if (tool?.effect === 'read') return [] + const toolName = tool?.name ?? result.toolId + return { + action: AuditAction.TOOL_EXECUTED, + resourceType: AuditResourceType.TOOL, + resourceId: result.toolId, + resourceName: toolName, + description: `Ran ${toolName}${result.status === 'failed' ? ' (failed)' : ''}`, + metadata: { + toolId: result.toolId, + status: result.status, + ...(input.credentialId ? { credentialId: input.credentialId } : {}), + }, + } + }, }) /** diff --git a/packages/audit/src/types.ts b/packages/audit/src/types.ts index 290cf940fbb..582c817f9f7 100644 --- a/packages/audit/src/types.ts +++ b/packages/audit/src/types.ts @@ -76,6 +76,12 @@ export const AuditAction = { DESKTOP_TOOL_CALL_CLAIMED: 'desktop_tool_call.claimed', DESKTOP_TOOL_CALL_COMPLETED: 'desktop_tool_call.completed', + /** + * A built-in integration tool run directly for a person, through the v2 API or Sim's MCP + * server, outside any workflow or chat. Recorded for tools that change something outside Sim. + */ + TOOL_EXECUTED: 'tool.executed', + DOCUMENT_UPLOADED: 'document.uploaded', DOCUMENT_UPDATED: 'document.updated', DOCUMENT_DELETED: 'document.deleted', @@ -328,6 +334,8 @@ export const AuditResourceType = { SKILL: 'skill', SUBSCRIPTION: 'subscription', TABLE: 'table', + /** Not a stored resource: a built-in integration tool, identified by its tool id. */ + TOOL: 'tool', USER: 'user', WEBHOOK: 'webhook', WORKFLOW: 'workflow', diff --git a/packages/testing/src/mocks/audit.mock.ts b/packages/testing/src/mocks/audit.mock.ts index bacda204420..8e2f1579670 100644 --- a/packages/testing/src/mocks/audit.mock.ts +++ b/packages/testing/src/mocks/audit.mock.ts @@ -55,6 +55,7 @@ const AuditAction = { ENTERPRISE_SUBSCRIPTION_PROVISIONED: 'subscription.enterprise_provisioned', CONNECTOR_DOCUMENT_RESTORED: 'connector_document.restored', CONNECTOR_DOCUMENT_EXCLUDED: 'connector_document.excluded', + TOOL_EXECUTED: 'tool.executed', DOCUMENT_UPLOADED: 'document.uploaded', DOCUMENT_UPDATED: 'document.updated', DOCUMENT_DELETED: 'document.deleted', @@ -279,6 +280,7 @@ const AuditResourceType = { SKILL: 'skill', SUBSCRIPTION: 'subscription', TABLE: 'table', + TOOL: 'tool', USER: 'user', WEBHOOK: 'webhook', WORKFLOW: 'workflow', From e3615e7bb67cd0d18e4a6515ecf9d36ebf772a72 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 2 Dec 2026 16:34:54 -0600 Subject: [PATCH 2/3] fix(audit): keep a failed tool call's error and match the action's doc to its rule --- .../docs/platform/enterprise/audit-logs.mdx | 2 +- .../application/execute-tool.test.ts | 9 ++++++--- .../lib/tool-execution/application/execute-tool.ts | 14 +++++++++++--- packages/audit/src/types.ts | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/apps/docs/content/docs/platform/enterprise/audit-logs.mdx b/apps/docs/content/docs/platform/enterprise/audit-logs.mdx index 724fcae7923..372cb770edf 100644 --- a/apps/docs/content/docs/platform/enterprise/audit-logs.mdx +++ b/apps/docs/content/docs/platform/enterprise/audit-logs.mdx @@ -105,7 +105,7 @@ Events use identifiers such as `resource.action`. These categories are represent | Exports and drains | `audit_logs.exported`, `data_drain.created`, `data_drain.updated`, `data_drain.ran`, `data_drain.tested` | | Integration tools | `tool.executed` | -A built-in integration tool run directly through the v2 API or Sim's MCP server, for example from Claude Code or Cursor, records `tool.executed` when the tool changes something outside Sim, such as sending a message or creating an issue. The entry names the person, the tool (`resourceId`, such as `slack_message`), whether the provider call succeeded or failed (`metadata.status`), and the credential used (`metadata.credentialId`), and its surface is `mcp` for calls made through the MCP server. Tool inputs and outputs are not copied into the entry. Read-only tools are not recorded per call; the credential they use records `credential.accessed`. +A built-in integration tool run directly through the v2 API or Sim's MCP server, for example from Claude Code or Cursor, records `tool.executed` unless the tool is read-only, for example when it sends a message or creates an issue. A failed call is recorded too, whether the provider rejected it or Sim refused it, for example because an admin denied the tool; `metadata.error` says which. The entry names the person, the tool (`resourceId`, such as `slack_message`), whether it succeeded or failed (`metadata.status`), the error for a failure (`metadata.error`), and the credential used (`metadata.credentialId`), and its surface is `mcp` for calls made through the MCP server. Tool inputs and outputs are not copied into the entry. Read-only tools are not recorded per call; the credential they use records `credential.accessed`. Authentication failures and password-reset requests use an anonymous actor and record the target account separately. Impersonation and administrative session revocations identify the operator and target organization. Table-row mutations record summaries and counts without copying row contents. A workflow or workspace written from source by the CLI (`workflow.created`, `workflow.updated` or `workspace.updated`) records `metadata.client` as `cli` and, when the source directory is in a git repository and `git` can read them, `metadata.gitSha` (the commit checked out) and `metadata.repo` (the `origin` remote URL, with any credentials removed). Either may be absent, for example `repo` in a repository with no `origin`. diff --git a/apps/sim/lib/tool-execution/application/execute-tool.test.ts b/apps/sim/lib/tool-execution/application/execute-tool.test.ts index ee63e3f4be6..d46caa0a3cb 100644 --- a/apps/sim/lib/tool-execution/application/execute-tool.test.ts +++ b/apps/sim/lib/tool-execution/application/execute-tool.test.ts @@ -456,14 +456,17 @@ describe('executeToolForCaller', () => { expect(JSON.stringify(entry)).not.toContain('secret plan') }) - it('records a write the provider refused, since it may still have reached them', async () => { - mocks.executeRegistryTool.mockResolvedValue({ success: false, error: 'channel_not_found' }) + it.each([ + ['the provider rejected', 'channel_not_found'], + ['Sim refused inside the run', 'Tool slack_message is not allowed by your permission group'], + ])('records a write %s, with its error to tell the two apart', async (_case, error) => { + mocks.executeRegistryTool.mockResolvedValue({ success: false, error }) await sendSlack() expect(auditMockFns.mockRecordAudit.mock.calls[0][0]).toMatchObject({ description: 'Ran Slack Send Message (failed)', - metadata: { status: 'failed' }, + metadata: { status: 'failed', error }, }) }) diff --git a/apps/sim/lib/tool-execution/application/execute-tool.ts b/apps/sim/lib/tool-execution/application/execute-tool.ts index a3ca0f0aeb9..77b23337536 100644 --- a/apps/sim/lib/tool-execution/application/execute-tool.ts +++ b/apps/sim/lib/tool-execution/application/execute-tool.ts @@ -2,6 +2,7 @@ import { AuditAction, AuditResourceType } from '@sim/audit' import { createLogger } from '@sim/logger' import { getErrorMessage } from '@sim/utils/errors' import { generateId } from '@sim/utils/id' +import { truncateAtCodePoint } from '@sim/utils/string' import { authorizeCredentialUseForAuth } from '@/lib/auth/credential-access' import { resolveBillingAttribution, toBillingContext } from '@/lib/billing/core/attribution' import { checkExecutionUsageLimits } from '@/lib/billing/core/usage-gate-cache' @@ -37,6 +38,8 @@ import { getTool } from '@/tools/utils' const logger = createLogger('ExecuteToolUseCase') const DEFAULT_TIMEOUT_SECONDS = 120 +/** Keeps a provider's error text in the audit entry short enough to read in the viewer. */ +const MAX_AUDIT_ERROR_CHARS = 500 export interface ExecuteToolInput { workspaceId: string @@ -300,9 +303,11 @@ function assertRequiredCallerInputsPresent( * one, applies the `deniedTools` denylist against the resolved id, and projects * secrets out of the result. * - * A call that dispatched to a tool which changes something outside Sim is - * audited as `tool.executed`, whether the provider succeeded or not: the send, - * post or delete may have reached the third party either way. Reads are left to + * A call that dispatched to a tool not marked read-only is audited as + * `tool.executed`, whether it succeeded or not. A failure is kept with its error, + * which tells a write the provider rejected (and may have partly applied) from + * one Sim refused inside the run, such as a tool the admin denied: both are + * attempts an admin needs to see. Reads are left to * the credential's own `credential.accessed` trail, which keeps the audit log at * human scale when a coding agent lists or searches in a loop. Inputs and outputs * are never copied into the entry; they can hold message bodies and secrets. @@ -524,6 +529,9 @@ export const executeToolForCaller = defineAuthorizedWorkspaceUseCase({ toolId: result.toolId, status: result.status, ...(input.credentialId ? { credentialId: input.credentialId } : {}), + ...(result.error + ? { error: truncateAtCodePoint(result.error.message, MAX_AUDIT_ERROR_CHARS) } + : {}), }, } }, diff --git a/packages/audit/src/types.ts b/packages/audit/src/types.ts index 582c817f9f7..39234870afb 100644 --- a/packages/audit/src/types.ts +++ b/packages/audit/src/types.ts @@ -78,7 +78,7 @@ export const AuditAction = { /** * A built-in integration tool run directly for a person, through the v2 API or Sim's MCP - * server, outside any workflow or chat. Recorded for tools that change something outside Sim. + * server, outside any workflow or chat. Recorded for every tool not marked read-only. */ TOOL_EXECUTED: 'tool.executed', From 150f14ba73a7feacce3b7e80f1e05e4a50bfc3f8 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 2 Dec 2026 16:47:19 -0600 Subject: [PATCH 3/3] fix(audit): keep error text out of tool.executed entries --- .../docs/platform/enterprise/audit-logs.mdx | 2 +- .../application/execute-tool.test.ts | 9 ++++++--- .../tool-execution/application/execute-tool.ts | 15 ++++----------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/apps/docs/content/docs/platform/enterprise/audit-logs.mdx b/apps/docs/content/docs/platform/enterprise/audit-logs.mdx index 372cb770edf..95c755dd675 100644 --- a/apps/docs/content/docs/platform/enterprise/audit-logs.mdx +++ b/apps/docs/content/docs/platform/enterprise/audit-logs.mdx @@ -105,7 +105,7 @@ Events use identifiers such as `resource.action`. These categories are represent | Exports and drains | `audit_logs.exported`, `data_drain.created`, `data_drain.updated`, `data_drain.ran`, `data_drain.tested` | | Integration tools | `tool.executed` | -A built-in integration tool run directly through the v2 API or Sim's MCP server, for example from Claude Code or Cursor, records `tool.executed` unless the tool is read-only, for example when it sends a message or creates an issue. A failed call is recorded too, whether the provider rejected it or Sim refused it, for example because an admin denied the tool; `metadata.error` says which. The entry names the person, the tool (`resourceId`, such as `slack_message`), whether it succeeded or failed (`metadata.status`), the error for a failure (`metadata.error`), and the credential used (`metadata.credentialId`), and its surface is `mcp` for calls made through the MCP server. Tool inputs and outputs are not copied into the entry. Read-only tools are not recorded per call; the credential they use records `credential.accessed`. +A built-in integration tool run directly through the v2 API or Sim's MCP server, for example from Claude Code or Cursor, records `tool.executed` unless the tool is read-only, for example when it sends a message or creates an issue. A failed call is recorded too, whether the provider rejected it or Sim refused it, for example because an admin denied the tool. Error text is not stored, because it can repeat the tool's input. The entry names the person, the tool (`resourceId`, such as `slack_message`), whether it succeeded or failed (`metadata.status`), and the credential used (`metadata.credentialId`), and its surface is `mcp` for calls made through the MCP server. Tool inputs, outputs, and error messages are not copied into the entry. Read-only tools are not recorded per call; the credential they use records `credential.accessed`. Authentication failures and password-reset requests use an anonymous actor and record the target account separately. Impersonation and administrative session revocations identify the operator and target organization. Table-row mutations record summaries and counts without copying row contents. A workflow or workspace written from source by the CLI (`workflow.created`, `workflow.updated` or `workspace.updated`) records `metadata.client` as `cli` and, when the source directory is in a git repository and `git` can read them, `metadata.gitSha` (the commit checked out) and `metadata.repo` (the `origin` remote URL, with any credentials removed). Either may be absent, for example `repo` in a repository with no `origin`. diff --git a/apps/sim/lib/tool-execution/application/execute-tool.test.ts b/apps/sim/lib/tool-execution/application/execute-tool.test.ts index d46caa0a3cb..02f30a9128c 100644 --- a/apps/sim/lib/tool-execution/application/execute-tool.test.ts +++ b/apps/sim/lib/tool-execution/application/execute-tool.test.ts @@ -459,15 +459,18 @@ describe('executeToolForCaller', () => { it.each([ ['the provider rejected', 'channel_not_found'], ['Sim refused inside the run', 'Tool slack_message is not allowed by your permission group'], - ])('records a write %s, with its error to tell the two apart', async (_case, error) => { + ['a parser echoed the input back', 'Invalid JSON near "secret plan"'], + ])('records a write %s as failed, without its error text', async (_case, error) => { mocks.executeRegistryTool.mockResolvedValue({ success: false, error }) await sendSlack() - expect(auditMockFns.mockRecordAudit.mock.calls[0][0]).toMatchObject({ + const entry = auditMockFns.mockRecordAudit.mock.calls[0][0] + expect(entry).toMatchObject({ description: 'Ran Slack Send Message (failed)', - metadata: { status: 'failed', error }, + metadata: { status: 'failed' }, }) + expect(JSON.stringify(entry)).not.toContain(error) }) it('leaves reads to the credential trail', async () => { diff --git a/apps/sim/lib/tool-execution/application/execute-tool.ts b/apps/sim/lib/tool-execution/application/execute-tool.ts index 77b23337536..25efefa26d1 100644 --- a/apps/sim/lib/tool-execution/application/execute-tool.ts +++ b/apps/sim/lib/tool-execution/application/execute-tool.ts @@ -2,7 +2,6 @@ import { AuditAction, AuditResourceType } from '@sim/audit' import { createLogger } from '@sim/logger' import { getErrorMessage } from '@sim/utils/errors' import { generateId } from '@sim/utils/id' -import { truncateAtCodePoint } from '@sim/utils/string' import { authorizeCredentialUseForAuth } from '@/lib/auth/credential-access' import { resolveBillingAttribution, toBillingContext } from '@/lib/billing/core/attribution' import { checkExecutionUsageLimits } from '@/lib/billing/core/usage-gate-cache' @@ -38,9 +37,6 @@ import { getTool } from '@/tools/utils' const logger = createLogger('ExecuteToolUseCase') const DEFAULT_TIMEOUT_SECONDS = 120 -/** Keeps a provider's error text in the audit entry short enough to read in the viewer. */ -const MAX_AUDIT_ERROR_CHARS = 500 - export interface ExecuteToolInput { workspaceId: string toolId: string @@ -304,10 +300,10 @@ function assertRequiredCallerInputsPresent( * secrets out of the result. * * A call that dispatched to a tool not marked read-only is audited as - * `tool.executed`, whether it succeeded or not. A failure is kept with its error, - * which tells a write the provider rejected (and may have partly applied) from - * one Sim refused inside the run, such as a tool the admin denied: both are - * attempts an admin needs to see. Reads are left to + * `tool.executed`, whether it succeeded or not: a write the provider rejected + * may have partly applied, and one Sim refused inside the run, such as a tool + * the admin denied, is an attempt an admin needs to see. The error text is not + * kept, since a provider or parser error can echo the caller's input. Reads are left to * the credential's own `credential.accessed` trail, which keeps the audit log at * human scale when a coding agent lists or searches in a loop. Inputs and outputs * are never copied into the entry; they can hold message bodies and secrets. @@ -529,9 +525,6 @@ export const executeToolForCaller = defineAuthorizedWorkspaceUseCase({ toolId: result.toolId, status: result.status, ...(input.credentialId ? { credentialId: input.credentialId } : {}), - ...(result.error - ? { error: truncateAtCodePoint(result.error.message, MAX_AUDIT_ERROR_CHARS) } - : {}), }, } },