diff --git a/apps/sim/ee/workspace-forking/lib/copy/copy-resources.test.ts b/apps/sim/ee/workspace-forking/lib/copy/copy-resources.test.ts index 248dccc9cd9..2025e91912b 100644 --- a/apps/sim/ee/workspace-forking/lib/copy/copy-resources.test.ts +++ b/apps/sim/ee/workspace-forking/lib/copy/copy-resources.test.ts @@ -1176,6 +1176,7 @@ describe('planForkMappedKbDocumentCopies', () => { const { tx, inserted } = makeTx([sourceRow('doc-1', 'src-kb')]) const result = await planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId: 'source-ws', resolver: mappedKbResolver, referencedDocumentIds: ['doc-1'], alreadyCopiedSourceDocIds: new Set(), @@ -1215,6 +1216,7 @@ describe('planForkMappedKbDocumentCopies', () => { const { tx, wheres } = makeTx([]) await planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId: 'source-ws', resolver: mappedKbResolver, referencedDocumentIds: ['doc-1'], alreadyCopiedSourceDocIds: new Set(), @@ -1234,6 +1236,7 @@ describe('planForkMappedKbDocumentCopies', () => { const { tx, inserted } = makeTx([sourceRow('doc-1', 'unmapped-kb')]) const result = await planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId: 'source-ws', resolver: mappedKbResolver, referencedDocumentIds: ['doc-1'], alreadyCopiedSourceDocIds: new Set(), @@ -1261,6 +1264,7 @@ describe('planForkMappedKbDocumentCopies', () => { const result = await planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId: 'source-ws', resolver: mappedKbResolver, referencedDocumentIds: ['doc-1'], alreadyCopiedSourceDocIds: new Set(), @@ -1289,6 +1293,7 @@ describe('planForkMappedKbDocumentCopies', () => { const result = await planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId: 'source-ws', resolver: mappedKbResolver, referencedDocumentIds: ['doc-1'], alreadyCopiedSourceDocIds: new Set(), @@ -1320,6 +1325,7 @@ describe('planForkMappedKbDocumentCopies', () => { await expect( planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId: 'source-ws', resolver: mappedKbResolver, referencedDocumentIds: ['doc-1'], alreadyCopiedSourceDocIds: new Set(), diff --git a/apps/sim/ee/workspace-forking/lib/copy/copy-resources.ts b/apps/sim/ee/workspace-forking/lib/copy/copy-resources.ts index 79168a3f9ba..cd8aed28366 100644 --- a/apps/sim/ee/workspace-forking/lib/copy/copy-resources.ts +++ b/apps/sim/ee/workspace-forking/lib/copy/copy-resources.ts @@ -926,9 +926,12 @@ async function createForkDocumentPlaceholders(params: { * untouched, so its reference is cleared as before. Connector-managed documents are excluded for * the reason given on {@link copyForkResourceContent} - and the exclusion matters MORE here, since * the target KB is an existing one that may already run its own connector over the same source. + * Candidates must live in `sourceWorkspaceId`: the ids come from workflow state and caller-supplied + * dependent values, so an id from another workspace is skipped rather than copied. */ export async function planForkMappedKbDocumentCopies(params: { tx: DbOrTx + sourceWorkspaceId: string resolver: ForkReferenceResolver referencedDocumentIds: string[] alreadyCopiedSourceDocIds: Set @@ -938,7 +941,8 @@ export async function planForkMappedKbDocumentCopies(params: { docIdMap: Map mappingEntries: ForkMappingUpsert[] }> { - const { tx, resolver, referencedDocumentIds, alreadyCopiedSourceDocIds, now } = params + const { tx, sourceWorkspaceId, resolver, referencedDocumentIds, alreadyCopiedSourceDocIds, now } = + params const documents: ForkContentDocumentEntry[] = [] const docIdMap = new Map() const mappingEntries: ForkMappingUpsert[] = [] @@ -964,6 +968,7 @@ export async function planForkMappedKbDocumentCopies(params: { .where( and( eq(knowledgeBase.id, document.knowledgeBaseId), + eq(knowledgeBase.workspaceId, sourceWorkspaceId), connectorIndexingCondition(), isNull(knowledgeBase.deletedAt) ) @@ -1544,6 +1549,18 @@ export async function copyForkResourceContent(params: { .where( and( eq(document.id, docEntry.sourceDocId), + // A queued payload is re-scoped too: one planned before a deploy may name any document. + exists( + db + .select({ id: knowledgeBase.id }) + .from(knowledgeBase) + .where( + and( + eq(knowledgeBase.id, document.knowledgeBaseId), + eq(knowledgeBase.workspaceId, contentPlan.sourceWorkspaceId) + ) + ) + ), isNull(document.deletedAt), isNull(document.archivedAt) ) diff --git a/apps/sim/ee/workspace-forking/lib/promote/copy-unmapped.ts b/apps/sim/ee/workspace-forking/lib/promote/copy-unmapped.ts index 97911c62d09..e7f233e71a1 100644 --- a/apps/sim/ee/workspace-forking/lib/promote/copy-unmapped.ts +++ b/apps/sim/ee/workspace-forking/lib/promote/copy-unmapped.ts @@ -262,6 +262,7 @@ export async function copyPromoteUnmappedResources(params: { const containerDocMap = result.idMap.get('knowledge_document') ?? new Map() const mappedKbDocs = await planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId, resolver, referencedDocumentIds, alreadyCopiedSourceDocIds: new Set(containerDocMap.keys()), diff --git a/apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts b/apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts index f96bd924f25..dca8d0e565c 100644 --- a/apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts +++ b/apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts @@ -1250,6 +1250,89 @@ describe('authorized fork and sync against PostgreSQL', () => { return { sourceWorkspaceId, childWorkspaceId, sourceId, childId, source } } + it('never copies a document from a workspace outside the fork edge into a mapped target KB', async () => { + const childId = await createChild() + const foreign = await seedKnowledgeCopy() + const attackerKbId = generateId() + await db + .insert(knowledgeBase) + .values({ id: attackerKbId, workspaceId: childId, userId, name: 'Attacker' }) + + const result = await syncWorkspace.execute({ + principal, + input: { + workspaceId: sourceWorkspaceId, + otherWorkspaceId: childId, + direction: 'push', + mappings: [ + { resourceType: 'knowledge_base', sourceId: foreign.sourceId, targetId: attackerKbId }, + ], + dependentValues: [ + { + workflowId: generateId(), + blockId: 'b1', + subBlockKey: 'documentId', + value: foreign.source.id, + }, + ], + }, + }) + + expect(result.redeployed).toBe(1) + expect( + await db.select().from(document).where(eq(document.knowledgeBaseId, attackerKbId)) + ).toEqual([]) + expect( + await db + .select() + .from(workspaceForkResourceMap) + .where( + and( + eq(workspaceForkResourceMap.childWorkspaceId, childId), + eq(workspaceForkResourceMap.resourceType, 'knowledge_document') + ) + ) + ).toEqual([]) + }) + + it('refuses to fill a queued document copy from outside the source workspace', async () => { + const fixture = await seedKnowledgeCopy() + const foreign = await seedKnowledgeCopy() + const childDocId = generateId() + await db.insert(document).values({ + ...foreign.source, + id: childDocId, + knowledgeBaseId: fixture.childId, + archivedAt: new Date(), + }) + const result = await copyForkResourceContent({ + contentPlan: { + sourceWorkspaceId: fixture.sourceWorkspaceId, + childWorkspaceId: fixture.childWorkspaceId, + userId, + tables: [], + knowledgeBases: [], + skills: [], + documents: [ + { + sourceDocId: foreign.source.id, + childDocId, + childKnowledgeBaseId: fixture.childId, + storageKey: null, + fileUrl: '', + fileSize: 0, + filename: foreign.source.filename, + mimeType: foreign.source.mimeType, + }, + ], + }, + control: { progress: { completed: [], tables: {}, embeddings: {} } }, + }) + expect(result).toMatchObject({ copied: 0, failed: 1 }) + const [placeholder] = await db.select().from(document).where(eq(document.id, childDocId)) + expect(placeholder.archivedAt).not.toBeNull() + }) + it('copies ordinary knowledge containers but excludes retired Search containers', async () => { const fixture = await seedKnowledgeCopy() const retiredId = generateId() @@ -1311,6 +1394,7 @@ describe('authorized fork and sync against PostgreSQL', () => { db.transaction((tx) => planForkMappedKbDocumentCopies({ tx, + sourceWorkspaceId: fixture.sourceWorkspaceId, resolver: (kind, id) => kind === 'knowledge-base' && id === fixture.sourceId ? fixture.childId : null, referencedDocumentIds: [fixture.source.id],