Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkgs/id/web/src/image-upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

// ============================================================================
Expand Down
29 changes: 28 additions & 1 deletion pkgs/id/web/src/image-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,34 @@ export async function uploadImageFile(file: File): Promise<UploadResponse> {
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<string, unknown>;
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 };
}

// ============================================================================
Expand Down
Loading