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(webv2): report every unreadable archive the same way

Josh Corbett599b94e5ef24
diff --git a/invokeai/frontend/webv2/src/workbench/projects/invk/archive.test.ts b/invokeai/frontend/webv2/src/workbench/projects/invk/archive.test.ts
index 1b4dcf0648..718906bc41 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/invk/archive.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/invk/archive.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import {
binaryEntry,
@@ -79,6 +79,37 @@ describe('writeArchive / readArchive', () => {
});
});
+ /**
+ * `unzip` reports the damage this version happens to produce through its callback, which is
+ * wrapped. It is not contractually bound to: a throw out of its own walk would bypass that wrap
+ * and reach the caller as an fflate message, so the two ways a file can be unreadable would stop
+ * looking the same from outside. Forced rather than provoked, because provoking it means relying
+ * on which corruptions a given fflate release chooses to raise rather than report.
+ */
+ it('reports damage fflate raises synchronously as not a project too', async () => {
+ vi.resetModules();
+ vi.doMock('fflate', () => ({
+ unzip: () => {
+ throw new Error('invalid zip data');
+ },
+ zip: () => undefined,
+ }));
+
+ try {
+ const { readArchive: readWithThrowingUnzip } = await import('./archive');
+
+ // Matched by shape, not by class: resetting the registry gives this import its own copy of
+ // `./format`, so `InvkFormatError` here is a different constructor than the one above.
+ await expect(readWithThrowingUnzip(new Uint8Array([1, 2, 3]))).rejects.toMatchObject({
+ name: 'InvkFormatError',
+ reason: 'not-a-project',
+ });
+ } finally {
+ vi.doUnmock('fflate');
+ vi.resetModules();
+ }
+ });
+
it('reports an oversized input rather than expanding it', async () => {
const oversized = { byteLength: 5 * 1024 * 1024 * 1024 } as Uint8Array;
diff --git a/invokeai/frontend/webv2/src/workbench/projects/invk/archive.ts b/invokeai/frontend/webv2/src/workbench/projects/invk/archive.ts
index e50fae6e48..46928a1ac9 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/invk/archive.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/invk/archive.ts
@@ -181,15 +181,23 @@ export const readArchive = async (bytes: Uint8Array): Promise<Map<string, Uint8A
const { unzip } = await import('fflate');
const budget = createExpansionBudget();
const expanded = await new Promise<Record<string, Uint8Array>>((resolve, reject) => {
- unzip(bytes, { filter: budget.accept }, (error, data) => {
- if (error) {
- reject(new InvkFormatError('not-a-project', error.message));
-
- return;
- }
+ // `unzip` reports most damage through the callback, but raises some of it — a central directory
+ // it cannot walk at all — synchronously. That throw rejects this promise with fflate's own error
+ // rather than the one this function promises to raise, so it is caught here too. Otherwise the
+ // guarantee holds for a truncated file and quietly fails for a corrupt one.
+ try {
+ unzip(bytes, { filter: budget.accept }, (error, data) => {
+ if (error) {
+ reject(new InvkFormatError('not-a-project', error.message));
+
+ return;
+ }
- resolve(data);
- });
+ resolve(data);
+ });
+ } catch (error) {
+ reject(new InvkFormatError('not-a-project', error instanceof Error ? error.message : 'Unreadable archive.'));
+ }
});
const refusal = budget.getRefusal();