From ca4aed4c159a4e9e13a98a5bf87d86af3080f3d2 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 25 Nov 2026 09:00:05 -0500 Subject: [PATCH] fix: Apply archive limits when reading import attachments (#14709) --- server/queues/tasks/JSONAPIImportTask.ts | 90 +++++++++++--------- server/queues/tasks/MarkdownAPIImportTask.ts | 87 +++++++++++-------- 2 files changed, 100 insertions(+), 77 deletions(-) diff --git a/server/queues/tasks/JSONAPIImportTask.ts b/server/queues/tasks/JSONAPIImportTask.ts index 88e7024500..91cd03c02c 100644 --- a/server/queues/tasks/JSONAPIImportTask.ts +++ b/server/queues/tasks/JSONAPIImportTask.ts @@ -152,49 +152,59 @@ export default class JSONAPIImportTask extends APIImportTask { ); const seen = new Set(); - await ZipHelper.walk(handle.path, async (entry) => { - if (entry.isDirectory) { - return; - } - // Normalize to match the bootstrap-phase pathInZip (segments rejoined - // with `/`, no leading `./` or empty segments). - const normalized = entry.fileName - .split("/") - .filter((s) => s !== "" && s !== ".") - .join("/"); - const item = manifestByPath.get(normalized); - if (!item) { - return; - } - seen.add(item.pathInZip); - const buffer = await entry.readBuffer(maxAttachmentSize); - - try { - await sequelize.transaction(async (transaction) => - attachmentCreator({ - source: "import", - preset: AttachmentPreset.DocumentAttachment, - id: item.id, - name: item.name, - type: item.mimeType, - buffer, - user: createdBy, - ctx: createContext({ user: createdBy, transaction }), - fetchOptions: { - timeout: env.FILE_STORAGE_IMPORT_TIMEOUT, - }, - }) - ); - } catch (err) { - // Each attachment commits in its own transaction, so a retry of - // this hook can re-encounter ids that already landed. Treat the - // unique-id collision as a no-op so the import remains resumable. - if (err instanceof UniqueConstraintError) { + await ZipHelper.walk( + handle.path, + async (entry) => { + if (entry.isDirectory) { return; } - throw err; + // Normalize to match the bootstrap-phase pathInZip (segments rejoined + // with `/`, no leading `./` or empty segments). + const normalized = entry.fileName + .split("/") + .filter((s) => s !== "" && s !== ".") + .join("/"); + const item = manifestByPath.get(normalized); + if (!item) { + return; + } + seen.add(item.pathInZip); + const buffer = await entry.readBuffer(maxAttachmentSize); + + try { + await sequelize.transaction(async (transaction) => + attachmentCreator({ + source: "import", + preset: AttachmentPreset.DocumentAttachment, + id: item.id, + name: item.name, + type: item.mimeType, + buffer, + user: createdBy, + ctx: createContext({ user: createdBy, transaction }), + fetchOptions: { + timeout: env.FILE_STORAGE_IMPORT_TIMEOUT, + }, + }) + ); + } catch (err) { + // Each attachment commits in its own transaction, so a retry of + // this hook can re-encounter ids that already landed. Treat the + // unique-id collision as a no-op so the import remains resumable. + if (err instanceof UniqueConstraintError) { + return; + } + throw err; + } + }, + // Bytes read across all attachments may not exceed the upload size limit. + { + maxEntries: ImportValidation.maxZipEntries, + maxTotalSize: AttachmentHelper.presetToMaxUploadSize( + AttachmentPreset.WorkspaceImport + ), } - }); + ); for (const item of scratch.manifest) { if (!seen.has(item.pathInZip)) { diff --git a/server/queues/tasks/MarkdownAPIImportTask.ts b/server/queues/tasks/MarkdownAPIImportTask.ts index 032ed1feb4..1289831f2f 100644 --- a/server/queues/tasks/MarkdownAPIImportTask.ts +++ b/server/queues/tasks/MarkdownAPIImportTask.ts @@ -213,46 +213,59 @@ export default class MarkdownAPIImportTask extends APIImportTask { ); const seen = new Set(); - await ZipHelper.walk(handle.path, async (entry) => { - if (entry.isDirectory) { - return; - } - // Normalize to match the bootstrap-phase pathInZip (segments rejoined - // with `/`, no leading `./` or empty segments). - const normalized = entry.fileName.split("/").filter(Boolean).join("/"); - const item = manifestByPath.get(normalized); - if (!item) { - return; - } - seen.add(item.pathInZip); - const buffer = await entry.readBuffer(maxAttachmentSize); - - try { - await sequelize.transaction(async (transaction) => - attachmentCreator({ - source: "import", - preset: AttachmentPreset.DocumentAttachment, - id: item.id, - name: item.name, - type: item.mimeType, - buffer, - user: createdBy, - ctx: createContext({ user: createdBy, transaction }), - fetchOptions: { - timeout: env.FILE_STORAGE_IMPORT_TIMEOUT, - }, - }) - ); - } catch (err) { - // Each attachment commits in its own transaction, so a retry of - // this hook can re-encounter ids that already landed. Treat the - // unique-id collision as a no-op so the import remains resumable. - if (err instanceof UniqueConstraintError) { + await ZipHelper.walk( + handle.path, + async (entry) => { + if (entry.isDirectory) { return; } - throw err; + // Normalize to match the bootstrap-phase pathInZip (segments rejoined + // with `/`, no leading `./` or empty segments). + const normalized = entry.fileName + .split("/") + .filter(Boolean) + .join("/"); + const item = manifestByPath.get(normalized); + if (!item) { + return; + } + seen.add(item.pathInZip); + const buffer = await entry.readBuffer(maxAttachmentSize); + + try { + await sequelize.transaction(async (transaction) => + attachmentCreator({ + source: "import", + preset: AttachmentPreset.DocumentAttachment, + id: item.id, + name: item.name, + type: item.mimeType, + buffer, + user: createdBy, + ctx: createContext({ user: createdBy, transaction }), + fetchOptions: { + timeout: env.FILE_STORAGE_IMPORT_TIMEOUT, + }, + }) + ); + } catch (err) { + // Each attachment commits in its own transaction, so a retry of + // this hook can re-encounter ids that already landed. Treat the + // unique-id collision as a no-op so the import remains resumable. + if (err instanceof UniqueConstraintError) { + return; + } + throw err; + } + }, + // Bytes read across all attachments may not exceed the upload size limit. + { + maxEntries: ImportValidation.maxZipEntries, + maxTotalSize: AttachmentHelper.presetToMaxUploadSize( + AttachmentPreset.WorkspaceImport + ), } - }); + ); for (const item of scratch.manifest) { if (!seen.has(item.pathInZip)) {