|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + assertAuthBinding: vi.fn(), |
| 8 | + completeSession: vi.fn(), |
| 9 | + finalizePurpose: vi.fn(), |
| 10 | + getOwnedSession: vi.fn(), |
| 11 | + getPrincipalSession: vi.fn(), |
| 12 | + reauthorizeWorkspacePurpose: vi.fn(), |
| 13 | + getWorkspaceFile: vi.fn(), |
| 14 | + createSession: vi.fn(), |
| 15 | + authorizeCreate: vi.fn(), |
| 16 | + attribution: vi.fn(), |
| 17 | + authorizeOrganizationAttachment: vi.fn(), |
| 18 | + authorizeOrganizationLogo: vi.fn(), |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/lib/uploads/contexts/organization-assistant/application', () => ({ |
| 22 | + authorizeOrganizationAttachmentControl: mocks.authorizeOrganizationAttachment, |
| 23 | + createOrganizationAssistantAttachment: vi.fn(), |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@/lib/uploads/contexts/organization-logo/application', () => ({ |
| 27 | + authorizeOrganizationLogoControl: mocks.authorizeOrganizationLogo, |
| 28 | + createOrganizationLogoUpload: vi.fn(), |
| 29 | +})) |
| 30 | + |
| 31 | +vi.mock('@/lib/uploads/contexts/workspace', () => ({ |
| 32 | + getWorkspaceFile: mocks.getWorkspaceFile, |
| 33 | +})) |
| 34 | + |
| 35 | +vi.mock('@/lib/uploads/upload-session/service', () => ({ |
| 36 | + abortUploadSession: vi.fn(), |
| 37 | + assertUploadSessionAuthBinding: mocks.assertAuthBinding, |
| 38 | + completeUploadSession: mocks.completeSession, |
| 39 | + createUploadPartUrls: vi.fn(), |
| 40 | + createUploadSession: mocks.createSession, |
| 41 | + getOwnedUploadSession: mocks.getOwnedSession, |
| 42 | + getPrincipalUploadSession: mocks.getPrincipalSession, |
| 43 | +})) |
| 44 | + |
| 45 | +vi.mock('@/app/api/files/uploads/finalizers', () => ({ |
| 46 | + finalizeUploadPurpose: mocks.finalizePurpose, |
| 47 | + finalizeWorkspaceFileUpload: vi.fn(), |
| 48 | + loadCompletedUploadPurpose: vi.fn(), |
| 49 | + loadCompletedWorkspaceFileUpload: vi.fn(), |
| 50 | +})) |
| 51 | + |
| 52 | +vi.mock('@/app/api/files/uploads/purposes', () => ({ |
| 53 | + createPurposeUploadSession: vi.fn(), |
| 54 | + reauthorizeUploadPurpose: vi.fn(), |
| 55 | + reauthorizeWorkspaceUploadPurpose: mocks.reauthorizeWorkspacePurpose, |
| 56 | + resolveUploadAttributionUserId: mocks.attribution, |
| 57 | +})) |
| 58 | + |
| 59 | +vi.mock('@/lib/workspace-files/application/workspace-operation-context', () => ({ |
| 60 | + authorizeWorkspaceFileOperation: mocks.authorizeCreate, |
| 61 | +})) |
| 62 | +vi.mock('@/lib/folders/queries', () => ({ |
| 63 | + loadActiveFolderPathIndex: async () => new Map(), |
| 64 | + resolveFolderPathFromIndex: () => null, |
| 65 | +})) |
| 66 | + |
| 67 | +import { OrchestrationError } from '@/lib/core/orchestration/types' |
| 68 | +import { createFileUploadTransport } from '@/lib/mothership/agent-cli/file-upload-transport' |
| 69 | + |
| 70 | +const workspaceId = '7727ef3f-8cf6-4686-b063-2bb006a10785' |
| 71 | +const endpoint = 'http://localhost:3400' |
| 72 | +const cloudTransfer = { method: 'put', url: 'https://storage.test/signed-object', headers: {} } |
| 73 | +const session = { |
| 74 | + id: 'upload', |
| 75 | + status: 'uploading', |
| 76 | + fileName: 'deck.pptx', |
| 77 | + contentType: 'application/octet-stream', |
| 78 | + fileSize: 4, |
| 79 | + expiresAt: new Date('2099-01-01'), |
| 80 | + error: null, |
| 81 | + uploadToken: 'fixture', |
| 82 | + transfer: cloudTransfer, |
| 83 | +} |
| 84 | +const fallback = vi.fn() |
| 85 | +function upload(headers: Record<string, string> = {}) { |
| 86 | + const transport = createFileUploadTransport({ |
| 87 | + endpoint, |
| 88 | + workspaceId, |
| 89 | + userId: 'reader', |
| 90 | + invocation: { userId: 'reader', workspaceId, chatId: 'chat' }, |
| 91 | + fallback, |
| 92 | + uploadProvenance: () => ({ status: 'exact', entries: [] }), |
| 93 | + }) |
| 94 | + return transport(`${endpoint}/api/v2/files/uploads`, { |
| 95 | + method: 'POST', |
| 96 | + headers: { 'content-type': 'application/json', ...headers }, |
| 97 | + body: JSON.stringify({ |
| 98 | + workspaceId, |
| 99 | + name: 'deck.pptx', |
| 100 | + contentType: 'application/octet-stream', |
| 101 | + size: 4, |
| 102 | + }), |
| 103 | + }) |
| 104 | +} |
| 105 | +beforeEach(() => { |
| 106 | + vi.resetAllMocks() |
| 107 | + mocks.authorizeCreate.mockResolvedValue(undefined) |
| 108 | + mocks.attribution.mockResolvedValue('reader') |
| 109 | + mocks.createSession.mockResolvedValue(session) |
| 110 | +}) |
| 111 | +describe('embedded upload application request context', () => { |
| 112 | + it.each([ |
| 113 | + {}, |
| 114 | + { |
| 115 | + origin: 'https://untrusted.test', |
| 116 | + host: 'untrusted.test', |
| 117 | + 'x-forwarded-host': 'untrusted.test', |
| 118 | + 'x-forwarded-proto': 'https', |
| 119 | + }, |
| 120 | + ])( |
| 121 | + 'supplies the configured origin to the real authorized operation despite headers %j', |
| 122 | + async (headers) => { |
| 123 | + const response = await upload(headers) |
| 124 | + expect(response.status).toBe(200) |
| 125 | + expect(mocks.authorizeCreate).toHaveBeenCalledWith( |
| 126 | + expect.objectContaining({ |
| 127 | + kind: 'delegated', |
| 128 | + subjectUserId: 'reader', |
| 129 | + workspaceId, |
| 130 | + audience: 'sim:workspace-files', |
| 131 | + }), |
| 132 | + expect.objectContaining({ id: 'files.upload.create', minimumRole: 'write' }), |
| 133 | + workspaceId |
| 134 | + ) |
| 135 | + expect(mocks.createSession).toHaveBeenCalledWith( |
| 136 | + expect.objectContaining({ localOrigin: endpoint, workspaceId, secretProvenance: 'pending' }) |
| 137 | + ) |
| 138 | + expect((await response.json()).data.transfer).toEqual(cloudTransfer) |
| 139 | + expect(fallback).not.toHaveBeenCalled() |
| 140 | + } |
| 141 | + ) |
| 142 | + it('still rejects canonical authorization failure before creating an upload session', async () => { |
| 143 | + mocks.authorizeCreate.mockRejectedValue(new OrchestrationError('forbidden', 'Access denied')) |
| 144 | + expect((await upload()).status).toBe(403) |
| 145 | + expect(mocks.createSession).not.toHaveBeenCalled() |
| 146 | + }) |
| 147 | +}) |
0 commit comments