Skip to content

Commit f2f0c41

Browse files
committed
fix(realtime): refuse whole pasted subtrees under a locked ancestor
batch-add-blocks checks each added block against the graph as it will land, so a rejected container's new children and loop/parallel config are refused with it, and an edge refused for targeting a locked block re-syncs the room.
1 parent cac9713 commit f2f0c41

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

‎apps/realtime/src/database/block-lock.integration.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,24 +215,39 @@ describe('block locks against PostgreSQL', () => {
215215
expect(result).toEqual({ reconcile: false })
216216
})
217217

218+
check('an add whose only rejected part is an edge into a locked block re-syncs', async () => {
219+
const result = await persist(OPERATION_TARGETS.BLOCKS, BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS, {
220+
blocks: [],
221+
edges: [{ id: generateId(), source: ids.free, target: ids.locked, sourceHandle: 'source' }],
222+
loops: {},
223+
parallels: {},
224+
})
225+
226+
expect(result).toEqual({ reconcile: true })
227+
})
228+
218229
check(
219230
'batch-add-blocks never takes the lock flag from the client nor nests under a locked ancestor',
220231
async () => {
221232
const lockedNew = generateId()
222233
const nestedNew = generateId()
234+
const nestedChild = generateId()
223235
await persist(OPERATION_TARGETS.BLOCKS, BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS, {
224236
blocks: [
225237
payloadBlock(lockedNew, 'Self-locked', { locked: true }),
226-
payloadBlock(nestedNew, 'Nested', { data: { parentId: ids.child } }),
238+
payloadBlock(nestedNew, 'Nested loop', { type: 'loop', data: { parentId: ids.child } }),
239+
payloadBlock(nestedChild, 'Nested child', { data: { parentId: nestedNew } }),
227240
],
228241
edges: [],
229-
loops: {},
242+
loops: { [nestedNew]: { id: nestedNew, nodes: [nestedChild], iterations: 2 } },
230243
parallels: {},
231244
})
232245

233246
const blocks = await readBlocks()
234247
expect(blocks[lockedNew]).toMatchObject({ locked: false })
235248
expect(blocks[nestedNew]).toBeUndefined()
249+
expect(blocks[nestedChild]).toBeUndefined()
250+
expect(await readSubflows()).toEqual([{ id: ids.container, config: lockedLoopConfig }])
236251
}
237252
)
238253

‎apps/realtime/src/database/operations.ts‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
getWorkflowEdgeScopeDropReason,
3535
isKnownWorkflowTriggerBlock,
3636
isWorkflowAnnotationOnlyBlockType,
37+
isWorkflowBlockAncestorLocked,
3738
isWorkflowBlockProtected,
3839
normalizeWorkflowEdgeSourceHandle,
3940
normalizeWorkflowEdgeTargetHandle,
@@ -978,21 +979,28 @@ async function handleBlocksOperationTx(
978979
// Lock state is admin-only, so an add never writes the lock flag, overwrites a protected
979980
// block or its loop/parallel config, nor places a block inside a locked container.
980981
let reconcile = false
982+
const rejectedBlockIds = new Set<string>()
981983

982984
if (blocks && blocks.length > 0) {
983-
const allowedBlocks = (blocks as Array<Record<string, unknown>>).filter((block) => {
984-
const parentId = (block.data as Record<string, unknown> | null)?.parentId as
985-
| string
986-
| undefined
987-
if (isProtected(block.id as string) || (parentId && isProtected(parentId))) {
988-
logger.info(`Skipping block ${block.id} - it or its parent is locked`)
989-
return false
990-
}
991-
return true
992-
})
993-
reconcile =
994-
allowedBlocks.length < blocks.length ||
995-
(blocks as Array<Record<string, unknown>>).some((block) => block.locked)
985+
const incoming = blocks as Array<Record<string, unknown>>
986+
// Added blocks land unlocked, so one is refused exactly when it is protected now or
987+
// would sit under a locked ancestor once the whole batch lands.
988+
const landed: Record<string, DbBlockRef> = { ...lockBlocks }
989+
for (const block of incoming) {
990+
const id = block.id as string
991+
if (!isProtected(id)) landed[id] = { id, locked: false, data: block.data }
992+
}
993+
for (const block of incoming) {
994+
const id = block.id as string
995+
if (isProtected(id) || isWorkflowBlockAncestorLocked(id, landed)) rejectedBlockIds.add(id)
996+
}
997+
const allowedBlocks = incoming.filter((block) => !rejectedBlockIds.has(block.id as string))
998+
if (rejectedBlockIds.size > 0) {
999+
logger.info('Skipping blocks that are locked or inside a locked container', {
1000+
count: rejectedBlockIds.size,
1001+
})
1002+
}
1003+
reconcile = rejectedBlockIds.size > 0 || incoming.some((block) => block.locked)
9961004

9971005
if (allowedBlocks.length === 0) {
9981006
logger.info('All blocks filtered out due to locks, skipping add')
@@ -1118,6 +1126,7 @@ async function handleBlocksOperationTx(
11181126
droppedCyclic,
11191127
})
11201128
}
1129+
if (droppedCounts['protected target block']) reconcile = true
11211130

11221131
if (safeEdges.length > 0) {
11231132
const edgeValues = safeEdges.map((edge) => ({
@@ -1142,7 +1151,7 @@ async function handleBlocksOperationTx(
11421151
})),
11431152
]
11441153
const subflowValues = subflowEntries
1145-
.filter((subflow) => !isProtected(subflow.id))
1154+
.filter((subflow) => !isProtected(subflow.id) && !rejectedBlockIds.has(subflow.id))
11461155
.map((subflow) => ({
11471156
...subflow,
11481157
workflowId,

0 commit comments

Comments
 (0)