Skip to content

Commit 1e43e10

Browse files
committed
fix(workspace-forking): scope fork-sync document copies to the source workspace
1 parent 507953c commit 1e43e10

4 files changed

Lines changed: 109 additions & 1 deletion

File tree

‎apps/sim/ee/workspace-forking/lib/copy/copy-resources.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ describe('planForkMappedKbDocumentCopies', () => {
11761176
const { tx, inserted } = makeTx([sourceRow('doc-1', 'src-kb')])
11771177
const result = await planForkMappedKbDocumentCopies({
11781178
tx,
1179+
sourceWorkspaceId: 'source-ws',
11791180
resolver: mappedKbResolver,
11801181
referencedDocumentIds: ['doc-1'],
11811182
alreadyCopiedSourceDocIds: new Set(),
@@ -1215,6 +1216,7 @@ describe('planForkMappedKbDocumentCopies', () => {
12151216
const { tx, wheres } = makeTx([])
12161217
await planForkMappedKbDocumentCopies({
12171218
tx,
1219+
sourceWorkspaceId: 'source-ws',
12181220
resolver: mappedKbResolver,
12191221
referencedDocumentIds: ['doc-1'],
12201222
alreadyCopiedSourceDocIds: new Set(),
@@ -1234,6 +1236,7 @@ describe('planForkMappedKbDocumentCopies', () => {
12341236
const { tx, inserted } = makeTx([sourceRow('doc-1', 'unmapped-kb')])
12351237
const result = await planForkMappedKbDocumentCopies({
12361238
tx,
1239+
sourceWorkspaceId: 'source-ws',
12371240
resolver: mappedKbResolver,
12381241
referencedDocumentIds: ['doc-1'],
12391242
alreadyCopiedSourceDocIds: new Set(),
@@ -1261,6 +1264,7 @@ describe('planForkMappedKbDocumentCopies', () => {
12611264

12621265
const result = await planForkMappedKbDocumentCopies({
12631266
tx,
1267+
sourceWorkspaceId: 'source-ws',
12641268
resolver: mappedKbResolver,
12651269
referencedDocumentIds: ['doc-1'],
12661270
alreadyCopiedSourceDocIds: new Set(),
@@ -1289,6 +1293,7 @@ describe('planForkMappedKbDocumentCopies', () => {
12891293

12901294
const result = await planForkMappedKbDocumentCopies({
12911295
tx,
1296+
sourceWorkspaceId: 'source-ws',
12921297
resolver: mappedKbResolver,
12931298
referencedDocumentIds: ['doc-1'],
12941299
alreadyCopiedSourceDocIds: new Set(),
@@ -1320,6 +1325,7 @@ describe('planForkMappedKbDocumentCopies', () => {
13201325
await expect(
13211326
planForkMappedKbDocumentCopies({
13221327
tx,
1328+
sourceWorkspaceId: 'source-ws',
13231329
resolver: mappedKbResolver,
13241330
referencedDocumentIds: ['doc-1'],
13251331
alreadyCopiedSourceDocIds: new Set(),

‎apps/sim/ee/workspace-forking/lib/copy/copy-resources.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,9 +926,12 @@ async function createForkDocumentPlaceholders(params: {
926926
* untouched, so its reference is cleared as before. Connector-managed documents are excluded for
927927
* the reason given on {@link copyForkResourceContent} - and the exclusion matters MORE here, since
928928
* the target KB is an existing one that may already run its own connector over the same source.
929+
* Candidates must live in `sourceWorkspaceId`: the ids come from workflow state and caller-supplied
930+
* dependent values, so an id from another workspace is skipped rather than copied.
929931
*/
930932
export async function planForkMappedKbDocumentCopies(params: {
931933
tx: DbOrTx
934+
sourceWorkspaceId: string
932935
resolver: ForkReferenceResolver
933936
referencedDocumentIds: string[]
934937
alreadyCopiedSourceDocIds: Set<string>
@@ -938,7 +941,8 @@ export async function planForkMappedKbDocumentCopies(params: {
938941
docIdMap: Map<string, string>
939942
mappingEntries: ForkMappingUpsert[]
940943
}> {
941-
const { tx, resolver, referencedDocumentIds, alreadyCopiedSourceDocIds, now } = params
944+
const { tx, sourceWorkspaceId, resolver, referencedDocumentIds, alreadyCopiedSourceDocIds, now } =
945+
params
942946
const documents: ForkContentDocumentEntry[] = []
943947
const docIdMap = new Map<string, string>()
944948
const mappingEntries: ForkMappingUpsert[] = []
@@ -964,6 +968,7 @@ export async function planForkMappedKbDocumentCopies(params: {
964968
.where(
965969
and(
966970
eq(knowledgeBase.id, document.knowledgeBaseId),
971+
eq(knowledgeBase.workspaceId, sourceWorkspaceId),
967972
connectorIndexingCondition(),
968973
isNull(knowledgeBase.deletedAt)
969974
)
@@ -1544,6 +1549,18 @@ export async function copyForkResourceContent(params: {
15441549
.where(
15451550
and(
15461551
eq(document.id, docEntry.sourceDocId),
1552+
// A queued payload is re-scoped too: one planned before a deploy may name any document.
1553+
exists(
1554+
db
1555+
.select({ id: knowledgeBase.id })
1556+
.from(knowledgeBase)
1557+
.where(
1558+
and(
1559+
eq(knowledgeBase.id, document.knowledgeBaseId),
1560+
eq(knowledgeBase.workspaceId, contentPlan.sourceWorkspaceId)
1561+
)
1562+
)
1563+
),
15471564
isNull(document.deletedAt),
15481565
isNull(document.archivedAt)
15491566
)

‎apps/sim/ee/workspace-forking/lib/promote/copy-unmapped.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ export async function copyPromoteUnmappedResources(params: {
262262
const containerDocMap = result.idMap.get('knowledge_document') ?? new Map<string, string>()
263263
const mappedKbDocs = await planForkMappedKbDocumentCopies({
264264
tx,
265+
sourceWorkspaceId,
265266
resolver,
266267
referencedDocumentIds,
267268
alreadyCopiedSourceDocIds: new Set(containerDocMap.keys()),

‎apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,89 @@ describe('authorized fork and sync against PostgreSQL', () => {
12501250
return { sourceWorkspaceId, childWorkspaceId, sourceId, childId, source }
12511251
}
12521252

1253+
it('never copies a document from a workspace outside the fork edge into a mapped target KB', async () => {
1254+
const childId = await createChild()
1255+
const foreign = await seedKnowledgeCopy()
1256+
const attackerKbId = generateId()
1257+
await db
1258+
.insert(knowledgeBase)
1259+
.values({ id: attackerKbId, workspaceId: childId, userId, name: 'Attacker' })
1260+
1261+
const result = await syncWorkspace.execute({
1262+
principal,
1263+
input: {
1264+
workspaceId: sourceWorkspaceId,
1265+
otherWorkspaceId: childId,
1266+
direction: 'push',
1267+
mappings: [
1268+
{ resourceType: 'knowledge_base', sourceId: foreign.sourceId, targetId: attackerKbId },
1269+
],
1270+
dependentValues: [
1271+
{
1272+
workflowId: generateId(),
1273+
blockId: 'b1',
1274+
subBlockKey: 'documentId',
1275+
value: foreign.source.id,
1276+
},
1277+
],
1278+
},
1279+
})
1280+
1281+
expect(result.redeployed).toBe(1)
1282+
expect(
1283+
await db.select().from(document).where(eq(document.knowledgeBaseId, attackerKbId))
1284+
).toEqual([])
1285+
expect(
1286+
await db
1287+
.select()
1288+
.from(workspaceForkResourceMap)
1289+
.where(
1290+
and(
1291+
eq(workspaceForkResourceMap.childWorkspaceId, childId),
1292+
eq(workspaceForkResourceMap.resourceType, 'knowledge_document')
1293+
)
1294+
)
1295+
).toEqual([])
1296+
})
1297+
1298+
it('refuses to fill a queued document copy from outside the source workspace', async () => {
1299+
const fixture = await seedKnowledgeCopy()
1300+
const foreign = await seedKnowledgeCopy()
1301+
const childDocId = generateId()
1302+
await db.insert(document).values({
1303+
...foreign.source,
1304+
id: childDocId,
1305+
knowledgeBaseId: fixture.childId,
1306+
archivedAt: new Date(),
1307+
})
1308+
const result = await copyForkResourceContent({
1309+
contentPlan: {
1310+
sourceWorkspaceId: fixture.sourceWorkspaceId,
1311+
childWorkspaceId: fixture.childWorkspaceId,
1312+
userId,
1313+
tables: [],
1314+
knowledgeBases: [],
1315+
skills: [],
1316+
documents: [
1317+
{
1318+
sourceDocId: foreign.source.id,
1319+
childDocId,
1320+
childKnowledgeBaseId: fixture.childId,
1321+
storageKey: null,
1322+
fileUrl: '',
1323+
fileSize: 0,
1324+
filename: foreign.source.filename,
1325+
mimeType: foreign.source.mimeType,
1326+
},
1327+
],
1328+
},
1329+
control: { progress: { completed: [], tables: {}, embeddings: {} } },
1330+
})
1331+
expect(result).toMatchObject({ copied: 0, failed: 1 })
1332+
const [placeholder] = await db.select().from(document).where(eq(document.id, childDocId))
1333+
expect(placeholder.archivedAt).not.toBeNull()
1334+
})
1335+
12531336
it('copies ordinary knowledge containers but excludes retired Search containers', async () => {
12541337
const fixture = await seedKnowledgeCopy()
12551338
const retiredId = generateId()
@@ -1311,6 +1394,7 @@ describe('authorized fork and sync against PostgreSQL', () => {
13111394
db.transaction((tx) =>
13121395
planForkMappedKbDocumentCopies({
13131396
tx,
1397+
sourceWorkspaceId: fixture.sourceWorkspaceId,
13141398
resolver: (kind, id) =>
13151399
kind === 'knowledge-base' && id === fixture.sourceId ? fixture.childId : null,
13161400
referencedDocumentIds: [fixture.source.id],

0 commit comments

Comments
 (0)