-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(realtime): stop read-only members persisting block positions #6174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+269
−45
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| * | ||
| * End-to-end guard for the socket operation ACL: the security boundary is not the | ||
| * role table on its own but whether a role reaches `persistWorkflowOperation`. | ||
| * These tests drive the real handler with the real permission middleware (only the | ||
| * database and the workspace authorizer are mocked) and assert on the persist call, | ||
| * because that is what durably rewrites `workflow_blocks`. | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { IRoomManager } from '@/rooms' | ||
|
|
||
| const { mockAuthorizeWorkflow, mockPersist, mockAssertMutable } = vi.hoisted(() => ({ | ||
| mockAuthorizeWorkflow: vi.fn(), | ||
| mockPersist: vi.fn(), | ||
| mockAssertMutable: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@sim/platform-authz/workflow', () => ({ | ||
| authorizeWorkflowByWorkspacePermission: mockAuthorizeWorkflow, | ||
| assertWorkflowMutable: mockAssertMutable, | ||
| WorkflowLockedError: class WorkflowLockedError extends Error {}, | ||
| })) | ||
|
|
||
| vi.mock('@/database/operations', () => ({ | ||
| persistWorkflowOperation: mockPersist, | ||
| })) | ||
|
|
||
| import { setupOperationsHandlers } from '@/handlers/operations' | ||
|
|
||
| const WORKFLOW_ID = 'wf-acl' | ||
| const BLOCK_ID = 'block-1' | ||
|
|
||
| type Handler = (payload: unknown) => Promise<void> | void | ||
|
|
||
| function createSocket(id: string) { | ||
| const handlers: Record<string, Handler> = {} | ||
| const toEmit = vi.fn() | ||
| const socket = { | ||
| id, | ||
| userId: `user-${id}`, | ||
| userName: 'Test User', | ||
| on: vi.fn((event: string, handler: Handler) => { | ||
| handlers[event] = handler | ||
| }), | ||
| emit: vi.fn(), | ||
| to: vi.fn().mockReturnValue({ emit: toEmit }), | ||
| } | ||
| return { socket, handlers, toEmit } | ||
| } | ||
|
|
||
| function createRoomManager(socketId: string, role: string): IRoomManager { | ||
| return { | ||
| isReady: () => true, | ||
| getRoomForSocket: vi.fn().mockResolvedValue({ type: 'workflow', id: WORKFLOW_ID }), | ||
| getUserSession: vi | ||
| .fn() | ||
| .mockResolvedValue({ userId: `user-${socketId}`, userName: 'Test User' }), | ||
| hasRoom: vi.fn().mockResolvedValue(true), | ||
| getRoomUsers: vi.fn().mockResolvedValue([{ socketId, userId: `user-${socketId}`, role }]), | ||
| updateUserActivity: vi.fn().mockResolvedValue(undefined), | ||
| updateRoomLastModified: vi.fn().mockResolvedValue(undefined), | ||
| } as unknown as IRoomManager | ||
| } | ||
|
|
||
| /** A committed single-block move — the form that writes `workflow_blocks`. */ | ||
| function committedPositionUpdate() { | ||
| return { | ||
| operationId: 'op-1', | ||
| operation: 'update-position', | ||
| target: 'block', | ||
| timestamp: Date.now(), | ||
| payload: { id: BLOCK_ID, position: { x: 999_999, y: 999_999 }, commit: true }, | ||
| } | ||
| } | ||
|
|
||
| /** The batch form, which persists with no `commit` flag at all. */ | ||
| function batchPositionUpdate() { | ||
| return { | ||
| operationId: 'op-2', | ||
| operation: 'batch-update-positions', | ||
| target: 'blocks', | ||
| timestamp: Date.now(), | ||
| payload: { updates: [{ id: BLOCK_ID, position: { x: 1, y: 2 } }] }, | ||
| } | ||
| } | ||
|
|
||
| function setup(id: string, role: string) { | ||
| const { socket, handlers, toEmit } = createSocket(id) | ||
| setupOperationsHandlers( | ||
| socket as unknown as Parameters<typeof setupOperationsHandlers>[0], | ||
| createRoomManager(id, role) | ||
| ) | ||
| return { socket, handlers, toEmit } | ||
| } | ||
|
|
||
| describe('workflow operation ACL', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockAssertMutable.mockResolvedValue(undefined) | ||
| mockPersist.mockResolvedValue(undefined) | ||
| }) | ||
|
|
||
| describe('read-only member', () => { | ||
| beforeEach(() => { | ||
| mockAuthorizeWorkflow.mockResolvedValue({ allowed: true, workspacePermission: 'read' }) | ||
| }) | ||
|
|
||
| it('cannot persist a committed block position', async () => { | ||
| // Unique socket id per test: the permission cache is module-global and keyed | ||
| // by (user, workflow), so sharing a user across roles would hit a warm entry. | ||
| const { socket, handlers } = setup('sock-read-1', 'read') | ||
|
|
||
| await handlers['workflow-operation'](committedPositionUpdate()) | ||
|
|
||
| expect(mockPersist).not.toHaveBeenCalled() | ||
| expect(socket.emit).toHaveBeenCalledWith( | ||
| 'operation-forbidden', | ||
| expect.objectContaining({ type: 'INSUFFICIENT_PERMISSIONS' }) | ||
| ) | ||
| }) | ||
|
|
||
| it('cannot persist a batch position update', async () => { | ||
| const { socket, handlers } = setup('sock-read-2', 'read') | ||
|
|
||
| await handlers['workflow-operation'](batchPositionUpdate()) | ||
|
|
||
| expect(mockPersist).not.toHaveBeenCalled() | ||
| expect(socket.emit).toHaveBeenCalledWith( | ||
| 'operation-forbidden', | ||
| expect.objectContaining({ type: 'INSUFFICIENT_PERMISSIONS' }) | ||
| ) | ||
| }) | ||
|
|
||
| it('still relays an UNCOMMITTED position update without persisting it', async () => { | ||
| // The smooth-drag broadcast is deliberately ungated (it never persists), and | ||
| // tightening the ACL must not turn it into an error. | ||
| const { socket, handlers, toEmit } = setup('sock-read-3', 'read') | ||
|
|
||
| await handlers['workflow-operation']({ | ||
| ...committedPositionUpdate(), | ||
| payload: { id: BLOCK_ID, position: { x: 5, y: 6 }, commit: false }, | ||
| }) | ||
|
|
||
| expect(mockPersist).not.toHaveBeenCalled() | ||
| expect(toEmit).toHaveBeenCalledWith('workflow-operation', expect.anything()) | ||
| expect(socket.emit).not.toHaveBeenCalledWith( | ||
| 'operation-forbidden', | ||
| expect.objectContaining({ type: 'INSUFFICIENT_PERMISSIONS' }) | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('write member (positive control)', () => { | ||
| beforeEach(() => { | ||
| mockAuthorizeWorkflow.mockResolvedValue({ allowed: true, workspacePermission: 'write' }) | ||
| }) | ||
|
|
||
| it('persists a committed block position', async () => { | ||
| const { handlers } = setup('sock-write-1', 'write') | ||
|
|
||
| await handlers['workflow-operation'](committedPositionUpdate()) | ||
|
|
||
| expect(mockPersist).toHaveBeenCalledWith( | ||
| WORKFLOW_ID, | ||
| expect.objectContaining({ operation: 'update-position' }) | ||
| ) | ||
| }) | ||
|
|
||
| it('persists a batch position update', async () => { | ||
| const { handlers } = setup('sock-write-2', 'write') | ||
|
|
||
| await handlers['workflow-operation'](batchPositionUpdate()) | ||
|
|
||
| expect(mockPersist).toHaveBeenCalledWith( | ||
| WORKFLOW_ID, | ||
| expect.objectContaining({ operation: 'batch-update-positions' }) | ||
| ) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.