|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { copilotChats, member, workspace } from '@sim/db/schema' |
| 3 | +import { queueTableRows, resetDbChainMock } from '@sim/testing' |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import { |
| 6 | + createTrustedCopilotPrincipal, |
| 7 | + createTrustedOrganizationCopilotPrincipal, |
| 8 | +} from '@/lib/mothership/auth/application-delegation' |
| 9 | +import { MEMORY_SCOPE_AUDIENCE, readMemoryScope } from './read-scope' |
| 10 | + |
| 11 | +const mocks = vi.hoisted(() => ({ |
| 12 | + config: vi.fn(), |
| 13 | + banned: vi.fn(), |
| 14 | + permission: vi.fn(), |
| 15 | + capability: vi.fn(), |
| 16 | +})) |
| 17 | +vi.mock('@/lib/auth/ban', () => ({ getActivelyBannedUserIds: mocks.banned })) |
| 18 | +vi.mock('@/lib/permission-groups/resolve.server', () => ({ |
| 19 | + getUserPermissionConfigForOrganization: mocks.config, |
| 20 | +})) |
| 21 | +vi.mock('@sim/platform-authz/workspace', async (importOriginal) => ({ |
| 22 | + ...(await importOriginal<typeof import('@sim/platform-authz/workspace')>()), |
| 23 | + resolveEffectiveWorkspacePermission: mocks.permission, |
| 24 | +})) |
| 25 | +vi.mock('@/lib/permission-groups/capability-assertions', async (importOriginal) => ({ |
| 26 | + ...(await importOriginal<typeof import('@/lib/permission-groups/capability-assertions')>()), |
| 27 | + assertWorkspaceCapability: mocks.capability, |
| 28 | +})) |
| 29 | +const input = { chatId: 'chat-1' } |
| 30 | +function principal() { |
| 31 | + return createTrustedOrganizationCopilotPrincipal( |
| 32 | + { userId: 'actor', organizationId: 'org-1', chatId: 'chat-1', delegationId: 'memory-1' }, |
| 33 | + { audience: MEMORY_SCOPE_AUDIENCE, ttlMs: 60_000 } |
| 34 | + ) |
| 35 | +} |
| 36 | +function queueChat(mode = 'plan', membership = true) { |
| 37 | + queueTableRows(copilotChats, [ |
| 38 | + { userId: 'actor', organizationId: 'org-1', workspaceId: null, type: 'mothership', mode }, |
| 39 | + ]) |
| 40 | + queueTableRows(member, membership ? [{ role: 'member' }] : []) |
| 41 | +} |
| 42 | +describe('private memory scope', () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks() |
| 45 | + resetDbChainMock() |
| 46 | + mocks.banned.mockResolvedValue([]) |
| 47 | + mocks.config.mockResolvedValue(null) |
| 48 | + mocks.permission.mockResolvedValue('read') |
| 49 | + mocks.capability.mockResolvedValue(undefined) |
| 50 | + }) |
| 51 | + it.each(['plan', 'agent'])('resolves the current %s chat owner', async (mode) => { |
| 52 | + queueChat(mode) |
| 53 | + expect(await readMemoryScope.execute({ principal: principal(), input })).toEqual({ |
| 54 | + userId: 'actor', |
| 55 | + organizationId: 'org-1', |
| 56 | + workspaceId: null, |
| 57 | + }) |
| 58 | + }) |
| 59 | + it.each(['user', 'organization', 'chat', 'audience', 'expired'] as const)( |
| 60 | + 'rejects forged %s', |
| 61 | + async (field) => { |
| 62 | + queueChat() |
| 63 | + const trusted = principal() |
| 64 | + const caller = { |
| 65 | + ...trusted, |
| 66 | + ...(field === 'user' ? { subjectUserId: 'other' } : {}), |
| 67 | + ...(field === 'organization' ? { organizationId: 'other' } : {}), |
| 68 | + ...(field === 'chat' ? { resourceScope: { chatId: 'other' } } : {}), |
| 69 | + ...(field === 'audience' ? { audience: 'other' } : {}), |
| 70 | + ...(field === 'expired' ? { expiresAt: new Date(0) } : {}), |
| 71 | + } |
| 72 | + await expect(readMemoryScope.execute({ principal: caller, input })).rejects.toThrow() |
| 73 | + } |
| 74 | + ) |
| 75 | + it('rejects revoked membership, Search chats, and suspended users', async () => { |
| 76 | + queueChat('plan', false) |
| 77 | + await expect(readMemoryScope.execute({ principal: principal(), input })).rejects.toThrow() |
| 78 | + resetDbChainMock() |
| 79 | + queueChat('assistant') |
| 80 | + await expect(readMemoryScope.execute({ principal: principal(), input })).rejects.toThrow() |
| 81 | + resetDbChainMock() |
| 82 | + queueChat() |
| 83 | + mocks.banned.mockResolvedValue(['actor']) |
| 84 | + await expect(readMemoryScope.execute({ principal: principal(), input })).rejects.toThrow() |
| 85 | + }) |
| 86 | + it('derives workspace organization from canonical state and rechecks access on every call', async () => { |
| 87 | + function queueWorkspace() { |
| 88 | + queueTableRows(copilotChats, [ |
| 89 | + { |
| 90 | + userId: 'actor', |
| 91 | + organizationId: null, |
| 92 | + workspaceId: 'workspace', |
| 93 | + type: 'mothership', |
| 94 | + mode: 'plan', |
| 95 | + }, |
| 96 | + ]) |
| 97 | + queueTableRows(workspace, [ |
| 98 | + { |
| 99 | + id: 'workspace', |
| 100 | + organizationId: 'canonical-org', |
| 101 | + allowPersonalApiKeys: true, |
| 102 | + billedAccountUserId: 'billing-owner', |
| 103 | + }, |
| 104 | + ]) |
| 105 | + } |
| 106 | + const caller = createTrustedCopilotPrincipal( |
| 107 | + { userId: 'actor', workspaceId: 'workspace', chatId: 'chat-1', delegationId: 'memory' }, |
| 108 | + { audience: MEMORY_SCOPE_AUDIENCE, ttlMs: 60000 } |
| 109 | + ) |
| 110 | + queueWorkspace() |
| 111 | + expect(await readMemoryScope.execute({ principal: caller, input })).toEqual({ |
| 112 | + userId: 'actor', |
| 113 | + organizationId: 'canonical-org', |
| 114 | + workspaceId: 'workspace', |
| 115 | + }) |
| 116 | + expect(mocks.capability).toHaveBeenCalledWith( |
| 117 | + 'actor', |
| 118 | + 'workspace', |
| 119 | + 'copilot.use', |
| 120 | + 'canonical-org', |
| 121 | + undefined |
| 122 | + ) |
| 123 | + resetDbChainMock() |
| 124 | + queueWorkspace() |
| 125 | + mocks.permission.mockResolvedValue(null) |
| 126 | + await expect(readMemoryScope.execute({ principal: caller, input })).rejects.toThrow() |
| 127 | + resetDbChainMock() |
| 128 | + queueWorkspace() |
| 129 | + mocks.permission.mockResolvedValue('read') |
| 130 | + await expect( |
| 131 | + readMemoryScope.execute({ |
| 132 | + principal: { ...caller, resourceScope: { chatId: 'other' } }, |
| 133 | + input, |
| 134 | + }) |
| 135 | + ).rejects.toThrow() |
| 136 | + resetDbChainMock() |
| 137 | + queueWorkspace() |
| 138 | + mocks.capability.mockRejectedValue(new Error('Capability revoked')) |
| 139 | + await expect(readMemoryScope.execute({ principal: caller, input })).rejects.toThrow( |
| 140 | + 'Capability revoked' |
| 141 | + ) |
| 142 | + }) |
| 143 | +}) |
0 commit comments