From 6a61dc6d86622111b48dac03d83a5240422aa5ae Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 16:15:17 -0600 Subject: [PATCH] fix(id/web): validate /api/upload response shape before inserting (#7823) --- pkgs/id/web/src/image-upload.test.ts | 25 ++++++++++++++++++++++++ pkgs/id/web/src/image-upload.ts | 29 +++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/pkgs/id/web/src/image-upload.test.ts b/pkgs/id/web/src/image-upload.test.ts index 33b9633897..0b10b92b11 100644 --- a/pkgs/id/web/src/image-upload.test.ts +++ b/pkgs/id/web/src/image-upload.test.ts @@ -272,6 +272,31 @@ describe("uploadImageFile", () => { const file = new File(["data"], "photo.png", { type: "image/png" }); await expect(uploadImageFile(file)).rejects.toThrow("Network error"); }); + + // Regression (#7823): a wrong-shape 200 must reject at the fetch + // boundary so the caller takes the upload-failure path; pre-fix these + // payloads resolved and inserted an image node with src undefined + // (or made the caller deref null and report "Insertion failed"). + it.each([ + ["null body", null], + ["string body", "abc"], + ["array body", []], + ["empty object", {}], + ["missing url", { hash: "abc123", name: "photo.png" }], + ["missing hash", { name: "photo.png", url: "/blob/abc123" }], + ["missing name", { hash: "abc123", url: "/blob/abc123" }], + ["non-string url", { hash: "abc123", name: "photo.png", url: 123 }], + ["non-string hash", { hash: 42, name: "photo.png", url: "/blob/abc123" }], + ])("rejects a malformed /api/upload payload: %s", async (_label, payload) => { + const mockResponse = { + ok: true, + json: () => Promise.resolve(payload), + }; + vi.spyOn(globalThis, "fetch").mockResolvedValue(mockResponse as Response); + + const file = new File(["fake image data"], "photo.png", { type: "image/png" }); + await expect(uploadImageFile(file)).rejects.toThrow("Upload failed: malformed /api/upload response"); + }); }); // ============================================================================ diff --git a/pkgs/id/web/src/image-upload.ts b/pkgs/id/web/src/image-upload.ts index a64f7bb342..f2570b4e60 100644 --- a/pkgs/id/web/src/image-upload.ts +++ b/pkgs/id/web/src/image-upload.ts @@ -106,7 +106,34 @@ export async function uploadImageFile(file: File): Promise { throw new Error(`Upload failed (${response.status}): ${text}`); } - return (await response.json()) as UploadResponse; + return parseUploadResponse(await response.json()); +} + +/** + * Validate an `/api/upload` payload at the fetch boundary (#7823). + * + * The server contract is three required strings (hash, name, url). A + * wrong-shape 200 (deploy skew, a future contract change) must fail + * closed through the upload-failure path: without this check the bare + * cast resolved malformed values and the caller inserted an image + * node with an undefined/wrong-typed src, or dereferenced null and + * misreported the failure as an insertion failure. + */ +export function parseUploadResponse(payload: unknown): UploadResponse { + if ( + typeof payload !== "object" || + payload === null || + Array.isArray(payload) + ) { + throw new Error("Upload failed: malformed /api/upload response (expected an object)"); + } + const { hash, name, url } = payload as Record; + if (typeof hash !== "string" || typeof name !== "string" || typeof url !== "string") { + throw new Error( + "Upload failed: malformed /api/upload response (hash, name and url must be strings)", + ); + } + return { hash, name, url }; } // ============================================================================