|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * End-to-end guard for the socket operation ACL: the security boundary is not the |
| 5 | + * role table on its own but whether a role reaches `persistWorkflowOperation`. |
| 6 | + * These tests drive the real handler with the real permission middleware (only the |
| 7 | + * database and the workspace authorizer are mocked) and assert on the persist call, |
| 8 | + * because that is what durably rewrites `workflow_blocks`. |
| 9 | + */ |
| 10 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 11 | +import type { IRoomManager } from '@/rooms' |
| 12 | + |
| 13 | +const { mockAuthorizeWorkflow, mockPersist, mockAssertMutable } = vi.hoisted(() => ({ |
| 14 | + mockAuthorizeWorkflow: vi.fn(), |
| 15 | + mockPersist: vi.fn(), |
| 16 | + mockAssertMutable: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('@sim/platform-authz/workflow', () => ({ |
| 20 | + authorizeWorkflowByWorkspacePermission: mockAuthorizeWorkflow, |
| 21 | + assertWorkflowMutable: mockAssertMutable, |
| 22 | + WorkflowLockedError: class WorkflowLockedError extends Error {}, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/database/operations', () => ({ |
| 26 | + persistWorkflowOperation: mockPersist, |
| 27 | +})) |
| 28 | + |
| 29 | +import { setupOperationsHandlers } from '@/handlers/operations' |
| 30 | + |
| 31 | +const WORKFLOW_ID = 'wf-acl' |
| 32 | +const BLOCK_ID = 'block-1' |
| 33 | + |
| 34 | +type Handler = (payload: unknown) => Promise<void> | void |
| 35 | + |
| 36 | +function createSocket(id: string) { |
| 37 | + const handlers: Record<string, Handler> = {} |
| 38 | + const toEmit = vi.fn() |
| 39 | + const socket = { |
| 40 | + id, |
| 41 | + userId: `user-${id}`, |
| 42 | + userName: 'Test User', |
| 43 | + on: vi.fn((event: string, handler: Handler) => { |
| 44 | + handlers[event] = handler |
| 45 | + }), |
| 46 | + emit: vi.fn(), |
| 47 | + to: vi.fn().mockReturnValue({ emit: toEmit }), |
| 48 | + } |
| 49 | + return { socket, handlers, toEmit } |
| 50 | +} |
| 51 | + |
| 52 | +function createRoomManager(socketId: string, role: string): IRoomManager { |
| 53 | + return { |
| 54 | + isReady: () => true, |
| 55 | + getRoomForSocket: vi.fn().mockResolvedValue({ type: 'workflow', id: WORKFLOW_ID }), |
| 56 | + getUserSession: vi |
| 57 | + .fn() |
| 58 | + .mockResolvedValue({ userId: `user-${socketId}`, userName: 'Test User' }), |
| 59 | + hasRoom: vi.fn().mockResolvedValue(true), |
| 60 | + getRoomUsers: vi.fn().mockResolvedValue([{ socketId, userId: `user-${socketId}`, role }]), |
| 61 | + updateUserActivity: vi.fn().mockResolvedValue(undefined), |
| 62 | + updateRoomLastModified: vi.fn().mockResolvedValue(undefined), |
| 63 | + } as unknown as IRoomManager |
| 64 | +} |
| 65 | + |
| 66 | +/** A committed single-block move — the form that writes `workflow_blocks`. */ |
| 67 | +function committedPositionUpdate() { |
| 68 | + return { |
| 69 | + operationId: 'op-1', |
| 70 | + operation: 'update-position', |
| 71 | + target: 'block', |
| 72 | + timestamp: Date.now(), |
| 73 | + payload: { id: BLOCK_ID, position: { x: 999_999, y: 999_999 }, commit: true }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** The batch form, which persists with no `commit` flag at all. */ |
| 78 | +function batchPositionUpdate() { |
| 79 | + return { |
| 80 | + operationId: 'op-2', |
| 81 | + operation: 'batch-update-positions', |
| 82 | + target: 'blocks', |
| 83 | + timestamp: Date.now(), |
| 84 | + payload: { updates: [{ id: BLOCK_ID, position: { x: 1, y: 2 } }] }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function setup(id: string, role: string) { |
| 89 | + const { socket, handlers, toEmit } = createSocket(id) |
| 90 | + setupOperationsHandlers( |
| 91 | + socket as unknown as Parameters<typeof setupOperationsHandlers>[0], |
| 92 | + createRoomManager(id, role) |
| 93 | + ) |
| 94 | + return { socket, handlers, toEmit } |
| 95 | +} |
| 96 | + |
| 97 | +describe('workflow operation ACL', () => { |
| 98 | + beforeEach(() => { |
| 99 | + vi.clearAllMocks() |
| 100 | + mockAssertMutable.mockResolvedValue(undefined) |
| 101 | + mockPersist.mockResolvedValue(undefined) |
| 102 | + }) |
| 103 | + |
| 104 | + describe('read-only member', () => { |
| 105 | + beforeEach(() => { |
| 106 | + mockAuthorizeWorkflow.mockResolvedValue({ allowed: true, workspacePermission: 'read' }) |
| 107 | + }) |
| 108 | + |
| 109 | + it('cannot persist a committed block position', async () => { |
| 110 | + // Unique socket id per test: the permission cache is module-global and keyed |
| 111 | + // by (user, workflow), so sharing a user across roles would hit a warm entry. |
| 112 | + const { socket, handlers } = setup('sock-read-1', 'read') |
| 113 | + |
| 114 | + await handlers['workflow-operation'](committedPositionUpdate()) |
| 115 | + |
| 116 | + expect(mockPersist).not.toHaveBeenCalled() |
| 117 | + expect(socket.emit).toHaveBeenCalledWith( |
| 118 | + 'operation-forbidden', |
| 119 | + expect.objectContaining({ type: 'INSUFFICIENT_PERMISSIONS' }) |
| 120 | + ) |
| 121 | + }) |
| 122 | + |
| 123 | + it('cannot persist a batch position update', async () => { |
| 124 | + const { socket, handlers } = setup('sock-read-2', 'read') |
| 125 | + |
| 126 | + await handlers['workflow-operation'](batchPositionUpdate()) |
| 127 | + |
| 128 | + expect(mockPersist).not.toHaveBeenCalled() |
| 129 | + expect(socket.emit).toHaveBeenCalledWith( |
| 130 | + 'operation-forbidden', |
| 131 | + expect.objectContaining({ type: 'INSUFFICIENT_PERMISSIONS' }) |
| 132 | + ) |
| 133 | + }) |
| 134 | + |
| 135 | + it('still relays an UNCOMMITTED position update without persisting it', async () => { |
| 136 | + // The smooth-drag broadcast is deliberately ungated (it never persists), and |
| 137 | + // tightening the ACL must not turn it into an error. |
| 138 | + const { socket, handlers, toEmit } = setup('sock-read-3', 'read') |
| 139 | + |
| 140 | + await handlers['workflow-operation']({ |
| 141 | + ...committedPositionUpdate(), |
| 142 | + payload: { id: BLOCK_ID, position: { x: 5, y: 6 }, commit: false }, |
| 143 | + }) |
| 144 | + |
| 145 | + expect(mockPersist).not.toHaveBeenCalled() |
| 146 | + expect(toEmit).toHaveBeenCalledWith('workflow-operation', expect.anything()) |
| 147 | + expect(socket.emit).not.toHaveBeenCalledWith( |
| 148 | + 'operation-forbidden', |
| 149 | + expect.objectContaining({ type: 'INSUFFICIENT_PERMISSIONS' }) |
| 150 | + ) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe('write member (positive control)', () => { |
| 155 | + beforeEach(() => { |
| 156 | + mockAuthorizeWorkflow.mockResolvedValue({ allowed: true, workspacePermission: 'write' }) |
| 157 | + }) |
| 158 | + |
| 159 | + it('persists a committed block position', async () => { |
| 160 | + const { handlers } = setup('sock-write-1', 'write') |
| 161 | + |
| 162 | + await handlers['workflow-operation'](committedPositionUpdate()) |
| 163 | + |
| 164 | + expect(mockPersist).toHaveBeenCalledWith( |
| 165 | + WORKFLOW_ID, |
| 166 | + expect.objectContaining({ operation: 'update-position' }) |
| 167 | + ) |
| 168 | + }) |
| 169 | + |
| 170 | + it('persists a batch position update', async () => { |
| 171 | + const { handlers } = setup('sock-write-2', 'write') |
| 172 | + |
| 173 | + await handlers['workflow-operation'](batchPositionUpdate()) |
| 174 | + |
| 175 | + expect(mockPersist).toHaveBeenCalledWith( |
| 176 | + WORKFLOW_ID, |
| 177 | + expect.objectContaining({ operation: 'batch-update-positions' }) |
| 178 | + ) |
| 179 | + }) |
| 180 | + }) |
| 181 | +}) |
0 commit comments