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(canvas): guard malformed canvas snapshots

joshistoast80a26b36b24c
diff --git a/invokeai/frontend/webv2/src/workbench/canvasMigration.test.ts b/invokeai/frontend/webv2/src/workbench/canvasMigration.test.ts
index 853a026e18..782b7bc768 100644
--- a/invokeai/frontend/webv2/src/workbench/canvasMigration.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvasMigration.test.ts
@@ -130,6 +130,61 @@ describe('migrateCanvasStateToV2', () => {
expect(getAdapter(migrated.snapshots[0]!.document)).toMatchObject({ beginEndStepPct: [0, 1], weight: 0.75 });
});
+ it('keeps valid snapshots and discards malformed snapshot entries without blocking the live canvas', () => {
+ const state = createEmptyCanvasStateV2();
+ const liveLayer = {
+ blendMode: 'normal',
+ id: 'live',
+ isEnabled: true,
+ isLocked: false,
+ name: 'Live',
+ opacity: 1,
+ source: { bitmap: null, type: 'paint' },
+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
+ type: 'raster',
+ };
+ const snapshotLayer = {
+ adapter: { kind: 'z_image_control', model: 'z-control' },
+ ...liveLayer,
+ id: 'snapshot-control',
+ name: 'Snapshot Control',
+ type: 'control',
+ withTransparencyEffect: true,
+ };
+ const validDocument = { ...state.document, layers: [snapshotLayer] };
+
+ const migrated = migrateCanvasStateToV2({
+ ...state,
+ document: { ...state.document, layers: [liveLayer] },
+ snapshots: [
+ { createdAt: 'now', document: validDocument, id: 'valid', name: 'Valid' },
+ null,
+ 'malformed',
+ {},
+ { createdAt: 'now', id: 'missing-document', name: 'Missing document' },
+ { createdAt: 'now', document: null, id: 'null-document', name: 'Null document' },
+ {
+ createdAt: 'now',
+ document: { ...state.document, layers: { bad: true } },
+ id: 'malformed-layers',
+ name: 'Malformed layers',
+ },
+ ],
+ });
+
+ expect(migrated.document.layers.map((layer) => layer.id)).toEqual(['live']);
+ expect(migrated.snapshots).toHaveLength(1);
+ expect(migrated.snapshots[0]).toMatchObject({ createdAt: 'now', id: 'valid', name: 'Valid' });
+ const validLayer = migrated.snapshots[0]!.document.layers[0];
+ expect(validLayer?.type === 'control' ? validLayer.adapter : null).toEqual({
+ beginEndStepPct: [0, 1],
+ controlMode: null,
+ kind: 'z_image_control',
+ model: 'z-control',
+ weight: 0.75,
+ });
+ });
+
it('maps a v1 raster layer to a v2 raster layer positioned by transform', () => {
const v1Canvas = {
document: {
diff --git a/invokeai/frontend/webv2/src/workbench/canvasMigration.ts b/invokeai/frontend/webv2/src/workbench/canvasMigration.ts
index 474e74ce8e..cf45c1a866 100644
--- a/invokeai/frontend/webv2/src/workbench/canvasMigration.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvasMigration.ts
@@ -250,14 +250,19 @@ export const normalizeCanvasDocumentControlAdapters = (
}),
});
-/** Defensively re-normalizes an already-v2 canvas state (fills in anything missing without re-deriving layers). */
-const normalizeCanvasStateV2 = (canvas: Record<string, unknown>): CanvasStateContractV2 => {
- const rawDocument = isRecord(canvas.document) ? canvas.document : {};
+const normalizeCanvasDocumentV2 = (value: unknown, requireValidLayers: boolean): CanvasDocumentContractV2 | null => {
+ const rawDocument = isRecord(value) ? value : {};
+ if (
+ requireValidLayers &&
+ (!Array.isArray(rawDocument.layers) ||
+ !rawDocument.layers.every((layer) => isRecord(layer) && typeof layer.id === 'string'))
+ ) {
+ return null;
+ }
const width = asPositiveNumber(rawDocument.width, DEFAULT_CANVAS_DOCUMENT_WIDTH);
const height = asPositiveNumber(rawDocument.height, DEFAULT_CANVAS_DOCUMENT_HEIGHT);
const bbox = isRecord(rawDocument.bbox) ? rawDocument.bbox : {};
-
- const document = normalizeCanvasDocumentControlAdapters({
+ return normalizeCanvasDocumentControlAdapters({
background:
rawDocument.background === 'transparent' || isRecord(rawDocument.background)
? (rawDocument.background as CanvasDocumentContractV2['background'])
@@ -274,15 +279,33 @@ const normalizeCanvasStateV2 = (canvas: Record<string, unknown>): CanvasStateCon
version: 2,
width,
});
+};
+
+const normalizeCanvasSnapshot = (value: unknown): CanvasStateContractV2['snapshots'][number] | null => {
+ if (
+ !isRecord(value) ||
+ typeof value.id !== 'string' ||
+ typeof value.name !== 'string' ||
+ typeof value.createdAt !== 'string'
+ ) {
+ return null;
+ }
+ const document = normalizeCanvasDocumentV2(value.document, true);
+ return document ? ({ ...value, document } as CanvasStateContractV2['snapshots'][number]) : null;
+};
+
+/** Defensively re-normalizes an already-v2 canvas state (fills in anything missing without re-deriving layers). */
+const normalizeCanvasStateV2 = (canvas: Record<string, unknown>): CanvasStateContractV2 => {
+ const document = normalizeCanvasDocumentV2(canvas.document, false) ?? createEmptyCanvasDocumentV2();
return {
document,
documentRevision: asNumber(canvas.documentRevision, 0),
snapshots: Array.isArray(canvas.snapshots)
- ? (canvas.snapshots as CanvasStateContractV2['snapshots']).map((snapshot) => ({
- ...snapshot,
- document: normalizeCanvasDocumentControlAdapters(snapshot.document),
- }))
+ ? canvas.snapshots.flatMap((snapshot) => {
+ const normalized = normalizeCanvasSnapshot(snapshot);
+ return normalized ? [normalized] : [];
+ })
: [],
stagingArea: migrateStagingAreaToV2(canvas),
version: 2,