@@ -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