diff --git a/apps/server/src/attachmentStore.test.ts b/apps/server/src/attachmentStore.test.ts index 3de9a8081c..456d775b9c 100644 --- a/apps/server/src/attachmentStore.test.ts +++ b/apps/server/src/attachmentStore.test.ts @@ -23,6 +23,17 @@ describe("attachmentStore", () => { expect(first && parseThreadSegmentFromAttachmentId(first)).toBe("thread-1"); }); + it("keeps deterministic ids distinct when sanitized thread segments collide", () => { + const dotted = createDeterministicAttachmentId("thread.a", "message-1:0"); + const dashed = createDeterministicAttachmentId("thread-a", "message-1:0"); + + expect(dotted).toBeTruthy(); + expect(dashed).toBeTruthy(); + expect(dotted).not.toBe(dashed); + expect(dotted && parseThreadSegmentFromAttachmentId(dotted)).toBe("thread-a"); + expect(dashed && parseThreadSegmentFromAttachmentId(dashed)).toBe("thread-a"); + }); + it("sanitizes thread ids when creating attachment ids", () => { const attachmentId = createAttachmentId("thread.folder/unsafe space"); expect(attachmentId).toBeTruthy(); diff --git a/apps/server/src/attachmentStore.ts b/apps/server/src/attachmentStore.ts index 597c75dd91..12c0c35143 100644 --- a/apps/server/src/attachmentStore.ts +++ b/apps/server/src/attachmentStore.ts @@ -48,7 +48,10 @@ export function createDeterministicAttachmentId( ): string | null { const threadSegment = toSafeThreadAttachmentSegment(threadId); if (!threadSegment) return null; - const hash = NodeCrypto.createHash("sha256").update(stableKey).digest("hex").slice(0, 32); + const hash = NodeCrypto.createHash("sha256") + .update(JSON.stringify([threadId, stableKey])) + .digest("hex") + .slice(0, 32); const uuid = `${hash.slice(0, 8)}-${hash.slice(8, 12)}-${hash.slice(12, 16)}-${hash.slice(16, 20)}-${hash.slice(20)}`; return `${threadSegment}-${uuid}`; }