Skip to content

Commit 9213315

Browse files
committed
fix failing tests, update testing
1 parent b4c633b commit 9213315

File tree

4 files changed

+124
-59
lines changed

4 files changed

+124
-59
lines changed

apps/sim/socket/constants.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,20 @@ export const UNDO_REDO_OPERATIONS = {
9090
} as const
9191

9292
export type UndoRedoOperation = (typeof UNDO_REDO_OPERATIONS)[keyof typeof UNDO_REDO_OPERATIONS]
93+
94+
/**
95+
* All socket operations that require permission checks.
96+
* This is the single source of truth for valid operations.
97+
*/
98+
export const ALL_SOCKET_OPERATIONS = [
99+
...Object.values(BLOCK_OPERATIONS),
100+
...Object.values(BLOCKS_OPERATIONS),
101+
...Object.values(EDGE_OPERATIONS),
102+
...Object.values(EDGES_OPERATIONS),
103+
...Object.values(WORKFLOW_OPERATIONS),
104+
...Object.values(SUBBLOCK_OPERATIONS),
105+
...Object.values(VARIABLE_OPERATIONS),
106+
...Object.values(SUBFLOW_OPERATIONS),
107+
] as const
108+
109+
export type SocketOperation = (typeof ALL_SOCKET_OPERATIONS)[number]

apps/sim/socket/middleware/permissions.ts

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,56 @@ import { workflow } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { eq } from 'drizzle-orm'
55
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
6+
import {
7+
BLOCK_OPERATIONS,
8+
BLOCKS_OPERATIONS,
9+
EDGE_OPERATIONS,
10+
EDGES_OPERATIONS,
11+
SUBFLOW_OPERATIONS,
12+
WORKFLOW_OPERATIONS,
13+
} from '@/socket/constants'
614

715
const logger = createLogger('SocketPermissions')
816

17+
// All write operations (admin and write roles have same permissions)
18+
const WRITE_OPERATIONS: string[] = [
19+
// Block operations
20+
BLOCK_OPERATIONS.UPDATE_POSITION,
21+
BLOCK_OPERATIONS.UPDATE_NAME,
22+
BLOCK_OPERATIONS.TOGGLE_ENABLED,
23+
BLOCK_OPERATIONS.UPDATE_PARENT,
24+
BLOCK_OPERATIONS.UPDATE_ADVANCED_MODE,
25+
BLOCK_OPERATIONS.TOGGLE_HANDLES,
26+
// Batch block operations
27+
BLOCKS_OPERATIONS.BATCH_UPDATE_POSITIONS,
28+
BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS,
29+
BLOCKS_OPERATIONS.BATCH_REMOVE_BLOCKS,
30+
BLOCKS_OPERATIONS.BATCH_TOGGLE_ENABLED,
31+
BLOCKS_OPERATIONS.BATCH_TOGGLE_HANDLES,
32+
BLOCKS_OPERATIONS.BATCH_UPDATE_PARENT,
33+
// Edge operations
34+
EDGE_OPERATIONS.ADD,
35+
EDGE_OPERATIONS.REMOVE,
36+
// Batch edge operations
37+
EDGES_OPERATIONS.BATCH_ADD_EDGES,
38+
EDGES_OPERATIONS.BATCH_REMOVE_EDGES,
39+
// Subflow operations
40+
SUBFLOW_OPERATIONS.UPDATE,
41+
// Workflow operations
42+
WORKFLOW_OPERATIONS.REPLACE_STATE,
43+
]
44+
45+
// Read role can only update positions (for cursor sync, etc.)
46+
const READ_OPERATIONS: string[] = [
47+
BLOCK_OPERATIONS.UPDATE_POSITION,
48+
BLOCKS_OPERATIONS.BATCH_UPDATE_POSITIONS,
49+
]
50+
951
// Define operation permissions based on role
1052
const ROLE_PERMISSIONS: Record<string, string[]> = {
11-
admin: [
12-
'add',
13-
'remove',
14-
'update',
15-
'update-position',
16-
'batch-update-positions',
17-
'batch-add-blocks',
18-
'batch-remove-blocks',
19-
'batch-add-edges',
20-
'batch-remove-edges',
21-
'batch-toggle-enabled',
22-
'batch-toggle-handles',
23-
'batch-update-parent',
24-
'update-name',
25-
'toggle-enabled',
26-
'update-parent',
27-
'update-advanced-mode',
28-
'toggle-handles',
29-
'replace-state',
30-
],
31-
write: [
32-
'add',
33-
'remove',
34-
'update',
35-
'update-position',
36-
'batch-update-positions',
37-
'batch-add-blocks',
38-
'batch-remove-blocks',
39-
'batch-add-edges',
40-
'batch-remove-edges',
41-
'batch-toggle-enabled',
42-
'batch-toggle-handles',
43-
'batch-update-parent',
44-
'update-name',
45-
'toggle-enabled',
46-
'update-parent',
47-
'update-advanced-mode',
48-
'toggle-handles',
49-
'replace-state',
50-
],
51-
read: ['update-position', 'batch-update-positions'],
53+
admin: WRITE_OPERATIONS,
54+
write: WRITE_OPERATIONS,
55+
read: READ_OPERATIONS,
5256
}
5357

5458
// Check if a role allows a specific operation (no DB query, pure logic)

apps/sim/stores/workflows/workflow/store.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,21 @@ export const useWorkflowStore = create<WorkflowStore>()(
498498
const currentEdges = get().edges
499499
const newEdges = [...currentEdges]
500500
const existingEdgeIds = new Set(currentEdges.map((e) => e.id))
501+
// Track existing connections to prevent duplicates (same source->target)
502+
const existingConnections = new Set(currentEdges.map((e) => `${e.source}->${e.target}`))
501503

502504
for (const edge of edges) {
505+
// Skip if edge ID already exists
503506
if (existingEdgeIds.has(edge.id)) continue
504507

508+
// Skip self-referencing edges
509+
if (edge.source === edge.target) continue
510+
511+
// Skip if connection already exists (same source and target)
512+
const connectionKey = `${edge.source}->${edge.target}`
513+
if (existingConnections.has(connectionKey)) continue
514+
515+
// Skip if would create a cycle
505516
if (wouldCreateCycle([...newEdges], edge.source, edge.target)) continue
506517

507518
newEdges.push({
@@ -514,6 +525,7 @@ export const useWorkflowStore = create<WorkflowStore>()(
514525
data: edge.data || {},
515526
})
516527
existingEdgeIds.add(edge.id)
528+
existingConnections.add(connectionKey)
517529
}
518530

519531
const blocks = get().blocks

packages/testing/src/factories/permission.factory.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,33 +252,65 @@ export function createWorkflowAccessContext(options: {
252252
}
253253

254254
/**
255-
* All socket operations that can be performed.
255+
* Socket operations
256+
*/
257+
const BLOCK_OPERATIONS = {
258+
UPDATE_POSITION: 'update-position',
259+
UPDATE_NAME: 'update-name',
260+
TOGGLE_ENABLED: 'toggle-enabled',
261+
UPDATE_PARENT: 'update-parent',
262+
UPDATE_ADVANCED_MODE: 'update-advanced-mode',
263+
TOGGLE_HANDLES: 'toggle-handles',
264+
} as const
265+
266+
const BLOCKS_OPERATIONS = {
267+
BATCH_UPDATE_POSITIONS: 'batch-update-positions',
268+
BATCH_ADD_BLOCKS: 'batch-add-blocks',
269+
BATCH_REMOVE_BLOCKS: 'batch-remove-blocks',
270+
BATCH_TOGGLE_ENABLED: 'batch-toggle-enabled',
271+
BATCH_TOGGLE_HANDLES: 'batch-toggle-handles',
272+
BATCH_UPDATE_PARENT: 'batch-update-parent',
273+
} as const
274+
275+
const EDGE_OPERATIONS = {
276+
ADD: 'add',
277+
REMOVE: 'remove',
278+
} as const
279+
280+
const EDGES_OPERATIONS = {
281+
BATCH_ADD_EDGES: 'batch-add-edges',
282+
BATCH_REMOVE_EDGES: 'batch-remove-edges',
283+
} as const
284+
285+
const SUBFLOW_OPERATIONS = {
286+
UPDATE: 'update',
287+
} as const
288+
289+
const WORKFLOW_OPERATIONS = {
290+
REPLACE_STATE: 'replace-state',
291+
} as const
292+
293+
/**
294+
* All socket operations that require permission checks.
256295
*/
257296
export const SOCKET_OPERATIONS = [
258-
'add',
259-
'remove',
260-
'batch-add-blocks',
261-
'batch-remove-blocks',
262-
'update',
263-
'update-position',
264-
'update-name',
265-
'toggle-enabled',
266-
'update-parent',
267-
'update-advanced-mode',
268-
'toggle-handles',
269-
'batch-update-positions',
270-
'replace-state',
297+
...Object.values(BLOCK_OPERATIONS),
298+
...Object.values(BLOCKS_OPERATIONS),
299+
...Object.values(EDGE_OPERATIONS),
300+
...Object.values(EDGES_OPERATIONS),
301+
...Object.values(SUBFLOW_OPERATIONS),
302+
...Object.values(WORKFLOW_OPERATIONS),
271303
] as const
272304

273305
export type SocketOperation = (typeof SOCKET_OPERATIONS)[number]
274306

275307
/**
276308
* Operations allowed for each role.
277309
*/
278-
export const ROLE_ALLOWED_OPERATIONS: Record<PermissionType, SocketOperation[]> = {
279-
admin: [...SOCKET_OPERATIONS],
280-
write: [...SOCKET_OPERATIONS],
281-
read: ['update-position', 'batch-update-positions'],
310+
export const ROLE_ALLOWED_OPERATIONS: Record<PermissionType, readonly SocketOperation[]> = {
311+
admin: SOCKET_OPERATIONS,
312+
write: SOCKET_OPERATIONS,
313+
read: [BLOCK_OPERATIONS.UPDATE_POSITION, BLOCKS_OPERATIONS.BATCH_UPDATE_POSITIONS],
282314
}
283315

284316
/**

0 commit comments

Comments
 (0)