|
| 1 | +/** |
| 2 | + * Mothership runs a file tool through `tools execute`, whose use case authorizes the chat's |
| 3 | + * delegated principal for `sim:tool-execution`. The File write that follows must still reach the |
| 4 | + * workspace-file use cases, which admit only `sim:workspace-files`, and must still refuse a |
| 5 | + * subject or workspace the original delegation does not cover. Exercises the real handler, |
| 6 | + * application authorization, metadata and local storage against Postgres. |
| 7 | + */ |
| 8 | +import { mkdtempSync } from 'node:fs' |
| 9 | +import { rm } from 'node:fs/promises' |
| 10 | +import { tmpdir } from 'node:os' |
| 11 | +import path from 'node:path' |
| 12 | +import type { SubjectDelegatedPrincipal } from '@sim/auth/principal' |
| 13 | +import { db } from '@sim/db' |
| 14 | +import { auditLog, organization, user, workspace, workspaceFiles } from '@sim/db/schema' |
| 15 | +import { deleteWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' |
| 16 | +import { generateId } from '@sim/utils/id' |
| 17 | +import { and, eq, inArray } from 'drizzle-orm' |
| 18 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 19 | + |
| 20 | +const fixtureStorage = vi.hoisted(() => ({ root: '' })) |
| 21 | +vi.mock('@/lib/uploads/core/setup.server', () => ({ |
| 22 | + get UPLOAD_DIR_SERVER() { |
| 23 | + return fixtureStorage.root |
| 24 | + }, |
| 25 | +})) |
| 26 | + |
| 27 | +import { executeFileTool } from '@/lib/internal/file/execute-tool' |
| 28 | +import { |
| 29 | + createKnowledgeAclFixtureIds, |
| 30 | + seedKnowledgeAclFixture, |
| 31 | +} from '@/lib/knowledge/__integration__/seed-source-access-fixture' |
| 32 | +import { uploadCopilotFile } from '@/lib/uploads/contexts/copilot/file-manager' |
| 33 | + |
| 34 | +const fixtures: ReturnType<typeof createKnowledgeAclFixtureIds>[] = [] |
| 35 | + |
| 36 | +async function seed() { |
| 37 | + const ids = createKnowledgeAclFixtureIds() |
| 38 | + fixtures.push(ids) |
| 39 | + await seedKnowledgeAclFixture(ids) |
| 40 | + return ids |
| 41 | +} |
| 42 | + |
| 43 | +type Fixture = Awaited<ReturnType<typeof seed>> |
| 44 | + |
| 45 | +/** The chat principal `tools execute` hands to the tool layer. */ |
| 46 | +function toolExecutionPrincipal(subjectUserId: string, workspaceId: string) { |
| 47 | + const issuedAt = new Date() |
| 48 | + return { |
| 49 | + kind: 'delegated', |
| 50 | + serviceId: 'copilot', |
| 51 | + subjectUserId, |
| 52 | + workspaceId, |
| 53 | + delegationId: generateId(), |
| 54 | + audience: 'sim:tool-execution', |
| 55 | + issuedAt, |
| 56 | + expiresAt: new Date(issuedAt.getTime() + 60_000), |
| 57 | + resourceScope: { chatId: generateId() }, |
| 58 | + } satisfies SubjectDelegatedPrincipal |
| 59 | +} |
| 60 | + |
| 61 | +/** A generated image as `image_generate` stores it outside a workflow run. */ |
| 62 | +async function generatedImage(ownerId: string) { |
| 63 | + return uploadCopilotFile({ |
| 64 | + buffer: Buffer.from(`image bytes ${generateId()}`), |
| 65 | + fileName: `${generateId()}.webp`, |
| 66 | + contentType: 'image/webp', |
| 67 | + userId: ownerId, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +async function writeCopy( |
| 72 | + ids: Fixture, |
| 73 | + principal: SubjectDelegatedPrincipal, |
| 74 | + fileInput: Awaited<ReturnType<typeof generatedImage>> |
| 75 | +) { |
| 76 | + const fileName = `saved-${generateId()}.webp` |
| 77 | + const response = await executeFileTool({ |
| 78 | + toolId: 'file_write', |
| 79 | + input: { operation: 'write', workspaceId: ids.workspaceId, fileName, fileInput }, |
| 80 | + headers: new Headers(), |
| 81 | + requestId: generateId(), |
| 82 | + context: { |
| 83 | + workflowId: '', |
| 84 | + workspaceId: ids.workspaceId, |
| 85 | + userId: principal.subjectUserId, |
| 86 | + callerPrincipal: principal, |
| 87 | + }, |
| 88 | + }) |
| 89 | + const [saved] = await db |
| 90 | + .select({ id: workspaceFiles.id, sizeBytes: workspaceFiles.sizeBytes }) |
| 91 | + .from(workspaceFiles) |
| 92 | + .where( |
| 93 | + and( |
| 94 | + eq(workspaceFiles.workspaceId, ids.workspaceId), |
| 95 | + eq(workspaceFiles.originalName, fileName), |
| 96 | + eq(workspaceFiles.context, 'workspace') |
| 97 | + ) |
| 98 | + ) |
| 99 | + return { status: response.status, saved } |
| 100 | +} |
| 101 | + |
| 102 | +beforeAll(() => { |
| 103 | + fixtureStorage.root = mkdtempSync(path.join(tmpdir(), 'sim-delegated-caller-write-')) |
| 104 | +}) |
| 105 | + |
| 106 | +afterAll(async () => { |
| 107 | + try { |
| 108 | + for (const ids of fixtures) { |
| 109 | + await db.delete(auditLog).where(eq(auditLog.workspaceId, ids.workspaceId)) |
| 110 | + await db |
| 111 | + .delete(workspaceFiles) |
| 112 | + .where(inArray(workspaceFiles.userId, [ids.aliceId, ids.bobId])) |
| 113 | + await deleteWorkspaceFixture(db, eq(workspace.id, ids.workspaceId)) |
| 114 | + await db.delete(organization).where(eq(organization.id, ids.organizationId)) |
| 115 | + await db.delete(user).where(inArray(user.id, [ids.aliceId, ids.bobId])) |
| 116 | + } |
| 117 | + await rm(fixtureStorage.root, { recursive: true, force: true }) |
| 118 | + } finally { |
| 119 | + await db.$client.end() |
| 120 | + } |
| 121 | +}) |
| 122 | + |
| 123 | +describe('a File write run directly from a Mothership chat', () => { |
| 124 | + it('saves a generated image into Workspace Files', async () => { |
| 125 | + const ids = await seed() |
| 126 | + const image = await generatedImage(ids.aliceId) |
| 127 | + |
| 128 | + const result = await writeCopy(ids, toolExecutionPrincipal(ids.aliceId, ids.workspaceId), image) |
| 129 | + |
| 130 | + expect(result.status).toBe(200) |
| 131 | + expect(result.saved?.sizeBytes).toBe(image.size) |
| 132 | + }) |
| 133 | + |
| 134 | + it('still refuses a member whose role cannot write', async () => { |
| 135 | + const ids = await seed() |
| 136 | + const image = await generatedImage(ids.bobId) |
| 137 | + |
| 138 | + const result = await writeCopy(ids, toolExecutionPrincipal(ids.bobId, ids.workspaceId), image) |
| 139 | + |
| 140 | + expect(result.status).toBeGreaterThanOrEqual(400) |
| 141 | + expect(result.saved).toBeUndefined() |
| 142 | + }) |
| 143 | + |
| 144 | + it('still refuses a delegation minted for another workspace', async () => { |
| 145 | + const ids = await seed() |
| 146 | + const image = await generatedImage(ids.aliceId) |
| 147 | + |
| 148 | + const result = await writeCopy(ids, toolExecutionPrincipal(ids.aliceId, generateId()), image) |
| 149 | + |
| 150 | + expect(result.status).toBeGreaterThanOrEqual(400) |
| 151 | + expect(result.saved).toBeUndefined() |
| 152 | + }) |
| 153 | +}) |
0 commit comments