Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(projects): bound the boards an archive or a copy may name

Josh Corbett1061ceeb9cea
diff --git a/docs/src/content/docs/development/Front End/project-files.mdx b/docs/src/content/docs/development/Front End/project-files.mdx
index 27733ffd91..515a8650ce 100644
--- a/docs/src/content/docs/development/Front End/project-files.mdx
+++ b/docs/src/content/docs/development/Front End/project-files.mdx
@@ -199,7 +199,7 @@ Future format changes should add a new member to the union rather than widening
## Guards
-`archive.ts` is the only module that imports fflate, and it does so with `await import()` so the ZIP codec never enters an initial chunk — the architecture budget fails on a new package appearing in a route's source-owner set. Both directions are bounded by `INVK_MAX_ARCHIVE_BYTES` and `INVK_MAX_ENTRIES`, because an `.invk` is a file someone was handed.
+`archive.ts` is the only module that imports fflate, and it does so with `await import()` so the ZIP codec never enters an initial chunk — the architecture budget fails on a new package appearing in a route's source-owner set. Both directions are bounded by `INVK_MAX_ARCHIVE_BYTES`, `INVK_MAX_ENTRIES` and, for `board.json`, `INVK_MAX_BOARDS`, because an `.invk` is a file someone was handed: a few kilobytes of empty boards must not be able to ask the importer for thousands of creates and moves. Duplicating a project applies the same board ceiling.
The input `File.size` is rejected before `arrayBuffer()` allocates its bytes. On export, one shared response reader checks declared `Content-Length` values and streams every asset through the same fixed 2 GiB allocation budget, including concurrent responses. It cancels a response when its declared length, streamed bytes or cumulative retained bytes cross the ceiling. The accumulated entries are checked before packing and the packed ZIP is checked again afterward, because compression can change the final output size.
diff --git a/invokeai/frontend/webv2/src/workbench/projects/invk/board.test.ts b/invokeai/frontend/webv2/src/workbench/projects/invk/board.test.ts
index 89daf4969a..2387135050 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/invk/board.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/invk/board.test.ts
@@ -1,6 +1,12 @@
import { describe, expect, it } from 'vitest';
-import { buildInvkBoardSnapshot, flattenInvkBoardItems, parseInvkBoardSnapshot, type InvkBoardItem } from './board';
+import {
+ buildInvkBoardSnapshot,
+ flattenInvkBoardItems,
+ INVK_MAX_BOARDS,
+ parseInvkBoardSnapshot,
+ type InvkBoardItem,
+} from './board';
import { InvkFormatError } from './format';
/** Malformed board enumeration must fail before restore creates resources. */
@@ -122,6 +128,17 @@ describe('parseInvkBoardSnapshot', () => {
expectRefusal({ boards: [inbox([item({ category: 'other' as never })])], version: 2 });
});
+ /** A few kilobytes must not be able to ask the importer for an unbounded number of creates and moves. */
+ it('caps how many boards a file may name', () => {
+ const members = (count: number) =>
+ Array.from({ length: count }, (_, index) => member([], `Board ${String(index)}`));
+
+ expect(
+ parseInvkBoardSnapshot({ boards: [inbox([]), ...members(INVK_MAX_BOARDS - 1)], version: 2 }).boards
+ ).toHaveLength(INVK_MAX_BOARDS);
+ expectRefusal({ boards: [inbox([]), ...members(INVK_MAX_BOARDS)], version: 2 });
+ });
+
it('refuses an unknown kind, a malformed known version, and an unknown key', () => {
expectRefusal({ boards: [inbox([item({ kind: 'audio' as never })])], version: 2 });
expectRefusal({ items: [], version: 2 });
@@ -145,6 +162,15 @@ describe('parseInvkBoardSnapshot', () => {
});
describe('buildInvkBoardSnapshot', () => {
+ it('refuses to write more boards than a reader accepts', () => {
+ const boards = [
+ inbox([]),
+ ...Array.from({ length: INVK_MAX_BOARDS }, (_, index) => member([], `B${String(index)}`)),
+ ];
+
+ expect(() => buildInvkBoardSnapshot(boards)).toThrowError(expect.objectContaining({ reason: 'too-large' }));
+ });
+
it('sorts items, leads with the inbox and stamps the file version, without touching the input', () => {
const items = [item({ name: 'b.png' }), item({ name: 'a.png' })];
const snapshot = buildInvkBoardSnapshot([member([item({ name: 'm.png' })]), inbox(items)]);
diff --git a/invokeai/frontend/webv2/src/workbench/projects/invk/board.ts b/invokeai/frontend/webv2/src/workbench/projects/invk/board.ts
index bf95d085b4..6086c96289 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/invk/board.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/invk/board.ts
@@ -74,9 +74,15 @@ const zBoard = z
})
.strict();
+/**
+ * Boards an archive may name. Each one costs the importer a create and a move, so a file must not be able to ask
+ * for an unbounded number of them in a few kilobytes; no real project comes near this.
+ */
+export const INVK_MAX_BOARDS = 1000;
+
const zBoardSnapshotV2 = z
.object({
- boards: z.array(zBoard).min(1),
+ boards: z.array(zBoard).min(1).max(INVK_MAX_BOARDS),
version: z.literal(2),
})
.strict();
@@ -144,12 +150,21 @@ export const parseInvkBoardSnapshot = (data: unknown): InvkBoardSnapshot => {
};
/** Build the entry from a server snapshot. Sorting here is what makes exports byte-comparable. */
-export const buildInvkBoardSnapshot = (boards: readonly InvkBoard[]): InvkBoardSnapshot => ({
- boards: [...boards.filter((board) => board.isInbox), ...boards.filter((board) => !board.isInbox)].map((board) => ({
- archived: board.archived,
- isInbox: board.isInbox,
- items: [...board.items].sort(compareItems),
- name: board.name,
- })),
- version: 2,
-});
+export const buildInvkBoardSnapshot = (boards: readonly InvkBoard[]): InvkBoardSnapshot => {
+ if (boards.length > INVK_MAX_BOARDS) {
+ throw new InvkFormatError(
+ 'too-large',
+ `Project has ${String(boards.length)} boards; an archive names at most ${String(INVK_MAX_BOARDS)}`
+ );
+ }
+
+ return {
+ boards: [...boards.filter((board) => board.isInbox), ...boards.filter((board) => !board.isInbox)].map((board) => ({
+ archived: board.archived,
+ isInbox: board.isInbox,
+ items: [...board.items].sort(compareItems),
+ name: board.name,
+ })),
+ version: 2,
+ };
+};
diff --git a/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.test.ts b/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.test.ts
index 034bd50ab4..54400f4178 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.test.ts
@@ -345,6 +345,23 @@ describe('duplicateProjectRecord', () => {
expect(api.createProjectSettled.mock.calls[0]![0]).toMatchObject({ board_id: 'staging-board' });
});
+ it('refuses a source with more boards than a copy may carry, before staging anything', async () => {
+ const members = Array.from({ length: 1000 }, (_unused, index) => ({
+ archived: false,
+ board_id: `b${String(index)}`,
+ is_inbox: false,
+ items: [],
+ name: `Board ${String(index)}`,
+ }));
+
+ await expect(
+ duplicateProject.duplicateProjectRecord({ boards: [...inboxOf([]), ...members], owner, record: sourceRecord() })
+ ).rejects.toMatchObject({ reason: 'too-large' });
+
+ expect(transport.createStagingBoard).not.toHaveBeenCalled();
+ expect(api.createProjectSettled).not.toHaveBeenCalled();
+ });
+
it('deletes the boards it had staged when staging a later one fails', async () => {
transport.createStagingBoard.mockResolvedValueOnce('staging-board').mockRejectedValueOnce(new Error('refused'));
diff --git a/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.ts b/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.ts
index dd32cf1561..997262bf29 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/invk/duplicateProject.ts
@@ -10,11 +10,11 @@ import {
stripInstallationState,
} from '@workbench/projects/projectAssets';
-import type { InvkBoard } from './board';
import type { MediaMaterializer } from './restoreProjectMedia';
import type { ProjectTransferIssues } from './transfer';
import { type CopyMediaResult, copyImagesToBoard, copyVideosToBoard, isRequestCancellation } from './assetTransport';
+import { INVK_MAX_BOARDS, type InvkBoard } from './board';
import { InvkFormatError, toInvkFormatReason } from './format';
import { createStagingBoards, findInboxStagingBoardId, placeMemberBoards } from './memberBoards';
import {
@@ -130,6 +130,14 @@ export const duplicateProjectRecord = async (
items: board.items,
name: board.name,
}));
+
+ // The same ceiling as an archive: a copy stages and moves each board one request at a time.
+ if (boards.length > INVK_MAX_BOARDS) {
+ throw new InvkFormatError(
+ 'too-large',
+ `Project has ${String(boards.length)} boards; a copy carries at most ${String(INVK_MAX_BOARDS)}`
+ );
+ }
// Made before anything is staged: every staging board joins it as it is created, so a copy abandoned at any point
// can delete them all.
const ledger = createRestoredMediaLedger([]);
diff --git a/invokeai/frontend/webv2/src/workbench/projects/invk/exportProject.test.ts b/invokeai/frontend/webv2/src/workbench/projects/invk/exportProject.test.ts
index 01f82436ad..b7cf503cae 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/invk/exportProject.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/invk/exportProject.test.ts
@@ -551,4 +551,17 @@ describe('board membership', () => {
expect(() => planInvkExport(withInbox(boardItems))).toThrowError(/archive entries/u);
});
+
+ it('refuses a project with more boards than an archive may name, before fetching anything', () => {
+ const members = Array.from({ length: 1000 }, (_unused, index) => ({
+ archived: false,
+ isInbox: false,
+ items: [],
+ name: `Board ${String(index)}`,
+ }));
+
+ expect(() => planInvkExport({ ...withInbox([]), boards: [...withInbox([]).boards, ...members] })).toThrowError(
+ expect.objectContaining({ reason: 'too-large' })
+ );
+ });
});