Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/sim/ee/workspace-forking/lib/copy/copy-resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -1215,6 +1216,7 @@ describe('planForkMappedKbDocumentCopies', () => {
const { tx, wheres } = makeTx([])
await planForkMappedKbDocumentCopies({
tx,
sourceWorkspaceId: 'source-ws',
resolver: mappedKbResolver,
referencedDocumentIds: ['doc-1'],
alreadyCopiedSourceDocIds: new Set(),
Expand All @@ -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(),
Expand Down Expand Up @@ -1261,6 +1264,7 @@ describe('planForkMappedKbDocumentCopies', () => {

const result = await planForkMappedKbDocumentCopies({
tx,
sourceWorkspaceId: 'source-ws',
resolver: mappedKbResolver,
referencedDocumentIds: ['doc-1'],
alreadyCopiedSourceDocIds: new Set(),
Expand Down Expand Up @@ -1289,6 +1293,7 @@ describe('planForkMappedKbDocumentCopies', () => {

const result = await planForkMappedKbDocumentCopies({
tx,
sourceWorkspaceId: 'source-ws',
resolver: mappedKbResolver,
referencedDocumentIds: ['doc-1'],
alreadyCopiedSourceDocIds: new Set(),
Expand Down Expand Up @@ -1320,6 +1325,7 @@ describe('planForkMappedKbDocumentCopies', () => {
await expect(
planForkMappedKbDocumentCopies({
tx,
sourceWorkspaceId: 'source-ws',
resolver: mappedKbResolver,
referencedDocumentIds: ['doc-1'],
alreadyCopiedSourceDocIds: new Set(),
Expand Down
19 changes: 18 additions & 1 deletion apps/sim/ee/workspace-forking/lib/copy/copy-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
Expand All @@ -938,7 +941,8 @@ export async function planForkMappedKbDocumentCopies(params: {
docIdMap: Map<string, string>
mappingEntries: ForkMappingUpsert[]
}> {
const { tx, resolver, referencedDocumentIds, alreadyCopiedSourceDocIds, now } = params
const { tx, sourceWorkspaceId, resolver, referencedDocumentIds, alreadyCopiedSourceDocIds, now } =
params
const documents: ForkContentDocumentEntry[] = []
const docIdMap = new Map<string, string>()
const mappingEntries: ForkMappingUpsert[] = []
Expand All @@ -964,6 +968,7 @@ export async function planForkMappedKbDocumentCopies(params: {
.where(
and(
eq(knowledgeBase.id, document.knowledgeBaseId),
eq(knowledgeBase.workspaceId, sourceWorkspaceId),
connectorIndexingCondition(),
isNull(knowledgeBase.deletedAt)
)
Expand Down Expand Up @@ -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)
)
Expand Down
1 change: 1 addition & 0 deletions apps/sim/ee/workspace-forking/lib/promote/copy-unmapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ export async function copyPromoteUnmappedResources(params: {
const containerDocMap = result.idMap.get('knowledge_document') ?? new Map<string, string>()
const mappedKbDocs = await planForkMappedKbDocumentCopies({
tx,
sourceWorkspaceId,
resolver,
referencedDocumentIds,
alreadyCopiedSourceDocIds: new Set(containerDocMap.keys()),
Expand Down
84 changes: 84 additions & 0 deletions apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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],
Expand Down
Loading