fix: Apply archive limits when reading import attachments (#14709)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-25 09:00:05 -05:00
1 parent 1c6e26553b
commit ca4aed4c15
2 files changed
+100 -77

No files matched your search

+50 -40
View File
@@ -152,49 +152,59 @@ export default class JSONAPIImportTask extends APIImportTask<Service> {
);
const seen = new Set<string>();
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)) {
+50 -37
View File
@@ -213,46 +213,59 @@ export default class MarkdownAPIImportTask extends APIImportTask<Markdown> {
);
const seen = new Set<string>();
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)) {