diff --git a/apps/docs/content/docs/platform/enterprise/audit-logs.mdx b/apps/docs/content/docs/platform/enterprise/audit-logs.mdx index 3c51f3aeed4..95c755dd675 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` 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 a00c072793b..02f30a9128c 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,60 @@ 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.each([ + ['the provider rejected', 'channel_not_found'], + ['Sim refused inside the run', 'Tool slack_message is not allowed by your permission group'], + ['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() + + const entry = auditMockFns.mockRecordAudit.mock.calls[0][0] + expect(entry).toMatchObject({ + description: 'Ran Slack Send Message (failed)', + metadata: { status: 'failed' }, + }) + expect(JSON.stringify(entry)).not.toContain(error) + }) + + 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..25efefa26d1 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' @@ -36,7 +37,6 @@ import { getTool } from '@/tools/utils' const logger = createLogger('ExecuteToolUseCase') const DEFAULT_TIMEOUT_SECONDS = 120 - export interface ExecuteToolInput { workspaceId: string toolId: string @@ -298,6 +298,15 @@ 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 not marked read-only is audited as + * `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. */ export const executeToolForCaller = defineAuthorizedWorkspaceUseCase({ operation: toolExecutionOperations.execute, @@ -502,6 +511,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..39234870afb 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 every tool not marked read-only. + */ + 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',