fix(server): isolate deterministic attachment ids
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
d09ff5cdf6
commit
c03c49f265
2 files changed
+15
-1
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user