|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { |
| 5 | + hasResourceOperationGrant, |
| 6 | + isWorkspaceDelegation, |
| 7 | + parsePrincipal, |
| 8 | + principalWorkspaceId, |
| 9 | + type ResourceDelegationScope, |
| 10 | + serializePrincipal, |
| 11 | + workspaceDelegationRestrictions, |
| 12 | +} from '@sim/auth/principal' |
| 13 | +import { createDelegatedPrincipal } from '@sim/testing/factories/principal.factory' |
| 14 | +import { describe, expect, it } from 'vitest' |
| 15 | +import { markCopilotWorkspaceInvocation } from '@/lib/core/application/copilot-workspace-invocation' |
| 16 | +import { requireResourceDelegation } from '@/lib/core/application/resource-delegation' |
| 17 | +import { requireAllowedWorkspacePrincipal } from '@/lib/core/application/workspace-authorization' |
| 18 | +import { defineWorkspaceOperation } from '@/lib/core/application/workspace-operation' |
| 19 | +import { createWorkspaceFileDelegatedPrincipal } from '@/lib/workspace-files/application/delegated-principal' |
| 20 | + |
| 21 | +const issuedAt = new Date('2026-01-01T00:00:00.000Z') |
| 22 | +const expiresAt = new Date('2099-01-01T00:00:00.000Z') |
| 23 | +const workspaceOperation = defineWorkspaceOperation({ |
| 24 | + id: 'test.workspace-grant', |
| 25 | + minimumRole: 'read', |
| 26 | + workspaceApiKey: 'deny', |
| 27 | + principalKinds: ['delegated'], |
| 28 | + delegatedServices: ['copilot'], |
| 29 | + contentDelivery: 'none', |
| 30 | + capability: 'none', |
| 31 | +}) |
| 32 | +const resourcePolicy = { |
| 33 | + audience: 'sim:files', |
| 34 | + services: ['copilot'], |
| 35 | + maxTtlMs: 60_000, |
| 36 | + scope: { kind: 'entity', owner: { entityType: 'project', entityId: 'project-1' } }, |
| 37 | +} as const |
| 38 | + |
| 39 | +function copilotEnvelope( |
| 40 | + grant: ResourceDelegationScope, |
| 41 | + invocation: { kind: 'chat'; chatId: string } | { kind: 'workspace'; workspaceId: string } |
| 42 | +) { |
| 43 | + return { |
| 44 | + kind: 'delegated' as const, |
| 45 | + serviceId: 'copilot' as const, |
| 46 | + subjectUserId: 'user-1', |
| 47 | + delegationId: 'delegation-1', |
| 48 | + audience: 'sim:test', |
| 49 | + issuedAt, |
| 50 | + expiresAt, |
| 51 | + invocation, |
| 52 | + grant, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +const workspaceGrant = { |
| 57 | + kind: 'entity' as const, |
| 58 | + owner: { entityType: 'workspace' as const, entityId: 'workspace-1' }, |
| 59 | +} |
| 60 | +const illegalFileGrant = { ...workspaceGrant, fileId: 'file-1' } |
| 61 | + |
| 62 | +describe('delegated grant admission', () => { |
| 63 | + it('rejects a workspace entity grant that also names a top-level file', () => { |
| 64 | + const principal = copilotEnvelope(illegalFileGrant, { |
| 65 | + kind: 'workspace', |
| 66 | + workspaceId: 'workspace-1', |
| 67 | + }) |
| 68 | + expect(isWorkspaceDelegation(principal)).toBe(false) |
| 69 | + expect(hasResourceOperationGrant(principal)).toBe(false) |
| 70 | + expect(principalWorkspaceId(principal)).toBeUndefined() |
| 71 | + expect(() => requireAllowedWorkspacePrincipal(principal, workspaceOperation)).toThrow( |
| 72 | + expect.objectContaining({ detailCode: 'PRINCIPAL_KIND_NOT_PERMITTED' }) |
| 73 | + ) |
| 74 | + expect(() => requireResourceDelegation(principal, resourcePolicy)).toThrow( |
| 75 | + 'Resource delegation is no longer valid' |
| 76 | + ) |
| 77 | + expect(() => markCopilotWorkspaceInvocation(principal)).toThrow(/current/) |
| 78 | + }) |
| 79 | + |
| 80 | + it('rejects a workspace invocation that names a different workspace than the grant', () => { |
| 81 | + const principal = { |
| 82 | + ...createDelegatedPrincipal({ workspaceId: 'workspace-1' }), |
| 83 | + invocation: { kind: 'workspace' as const, workspaceId: 'other' }, |
| 84 | + } |
| 85 | + expect(isWorkspaceDelegation(principal)).toBe(false) |
| 86 | + expect(principalWorkspaceId(principal)).toBeUndefined() |
| 87 | + }) |
| 88 | + |
| 89 | + it('rejects a chat invocation whose chat id is absent from restrictions', () => { |
| 90 | + const principal = { |
| 91 | + ...createDelegatedPrincipal({ workspaceId: 'workspace-1', resourceScope: {} }), |
| 92 | + invocation: { kind: 'chat' as const, chatId: 'chat-1' }, |
| 93 | + } |
| 94 | + expect(isWorkspaceDelegation(principal)).toBe(false) |
| 95 | + expect(workspaceDelegationRestrictions(principal)?.chatId).toBeUndefined() |
| 96 | + }) |
| 97 | + |
| 98 | + it('accepts a chat invocation that matches restrictions and reads the grant owner', () => { |
| 99 | + const principal = { |
| 100 | + ...createDelegatedPrincipal({ |
| 101 | + workspaceId: 'workspace-1', |
| 102 | + resourceScope: { chatId: 'chat-1' }, |
| 103 | + }), |
| 104 | + invocation: { kind: 'chat' as const, chatId: 'chat-1' }, |
| 105 | + } |
| 106 | + expect(isWorkspaceDelegation(principal)).toBe(true) |
| 107 | + expect(principalWorkspaceId(principal)).toBe('workspace-1') |
| 108 | + }) |
| 109 | + |
| 110 | + it('keeps an empty restriction object from bypassing a required chat id', () => { |
| 111 | + const principal = createDelegatedPrincipal({ |
| 112 | + workspaceId: 'workspace-1', |
| 113 | + resourceScope: {}, |
| 114 | + }) |
| 115 | + expect(isWorkspaceDelegation(principal)).toBe(true) |
| 116 | + expect(workspaceDelegationRestrictions(principal)?.chatId).toBeUndefined() |
| 117 | + }) |
| 118 | + |
| 119 | + it('keeps a project entity grant on the resource side of the partition', () => { |
| 120 | + const principal = copilotEnvelope( |
| 121 | + { kind: 'entity', owner: { entityType: 'project', entityId: 'project-1' } }, |
| 122 | + { kind: 'chat', chatId: 'chat-1' } |
| 123 | + ) |
| 124 | + expect(hasResourceOperationGrant(principal)).toBe(true) |
| 125 | + expect(isWorkspaceDelegation(principal)).toBe(false) |
| 126 | + expect(principalWorkspaceId(principal)).toBeUndefined() |
| 127 | + }) |
| 128 | + |
| 129 | + it('keeps a copilot workspace-file chat invocation across a snapshot round-trip', () => { |
| 130 | + const minted = createWorkspaceFileDelegatedPrincipal({ |
| 131 | + serviceId: 'copilot', |
| 132 | + subjectUserId: 'user-1', |
| 133 | + workspaceId: 'workspace-1', |
| 134 | + delegationId: 'delegation-1', |
| 135 | + fileId: 'file-1', |
| 136 | + chatId: 'chat-1', |
| 137 | + }) |
| 138 | + if (minted.serviceId !== 'copilot') throw new Error('Expected a copilot file principal') |
| 139 | + expect(minted.invocation).toEqual({ kind: 'chat', chatId: 'chat-1' }) |
| 140 | + expect(minted.grant.restrictions).toMatchObject({ fileId: 'file-1', chatId: 'chat-1' }) |
| 141 | + const parsed = parsePrincipal(serializePrincipal(minted)) |
| 142 | + if (parsed.kind !== 'delegated' || parsed.serviceId === 'executor') |
| 143 | + throw new Error('Expected a subject delegation') |
| 144 | + expect(parsed.invocation).toEqual({ kind: 'chat', chatId: 'chat-1' }) |
| 145 | + expect(parsed.grant.restrictions).toMatchObject({ fileId: 'file-1', chatId: 'chat-1' }) |
| 146 | + }) |
| 147 | + |
| 148 | + it('parses a realtime workspace payload as a workspace invocation', () => { |
| 149 | + const parsed = parsePrincipal({ |
| 150 | + version: 1, |
| 151 | + principal: { |
| 152 | + kind: 'delegated', |
| 153 | + serviceId: 'realtime', |
| 154 | + subjectUserId: 'user-1', |
| 155 | + workspaceId: 'workspace-1', |
| 156 | + delegationId: 'delegation-1', |
| 157 | + audience: 'sim:test', |
| 158 | + issuedAt: issuedAt.toISOString(), |
| 159 | + expiresAt: new Date('2026-01-01T00:05:00.000Z').toISOString(), |
| 160 | + }, |
| 161 | + }) |
| 162 | + if (parsed.kind !== 'delegated' || parsed.serviceId !== 'realtime') |
| 163 | + throw new Error('Expected a realtime delegation') |
| 164 | + expect(parsed.invocation).toEqual({ kind: 'workspace', workspaceId: 'workspace-1' }) |
| 165 | + expect(parsed).not.toHaveProperty('connectionId') |
| 166 | + }) |
| 167 | +}) |
0 commit comments