From 4f25f3854ee50e8fd7ebf2336468dcefe29de118 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 25 Nov 2026 20:20:58 -0500 Subject: [PATCH] fix: Validate attachment in documents.import (#14711) --- server/routes/api/documents/documents.test.ts | 26 +++++++++++++++++++ server/routes/api/documents/documents.ts | 5 ++++ 2 files changed, 31 insertions(+) diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 8ab6a119ff..34b55867a8 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -6363,6 +6363,32 @@ describe("#documents.import", () => { ); }); + it("should reject an attachment that is not an import upload", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const attachment = await buildAttachment({ + teamId: user.teamId, + userId: user.id, + }); + const deleteFile = vi.spyOn(FileStorage, "deleteFile"); + + const res = await server.post("/api/documents.import", user, { + body: { + collectionId: collection.id, + attachmentId: attachment.id, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(400); + expect(body.message).toEqual("Attachment is not an import upload"); + expect(deleteFile).not.toHaveBeenCalled(); + + vi.restoreAllMocks(); + }); + it("should error if no file or attachmentId is passed", async () => { const user = await buildUser(); const collection = await buildCollection({ diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index e9e9a0f7d1..314407487a 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -1794,6 +1794,11 @@ router.post( const attachment = await Attachment.findByPk(attachmentId); authorize(user, "read", attachment); + // The import task deletes the file, so only accept temporary import uploads. + if (!attachment.expiresAt) { + throw ValidationError("Attachment is not an import upload"); + } + key = attachment.key; fileName = attachment.name; mimeType = attachment.contentType;