Skip to content

Commit 0338e99

Browse files
t3dotggclaude
andauthored
fix(desktop): save and copy any image the app can show (pingdotgg#29079)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 333f7a6 commit 0338e99

8 files changed

Lines changed: 144 additions & 11 deletions

File tree

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
getSystemLocale,
4141
getWindowFullscreenState,
4242
openExternal,
43+
readMedia,
4344
openSystemSettings,
4445
checkSystemPermission,
4546
pasteAsText,
@@ -141,6 +142,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
141142
yield* ipc.handle(setTheme);
142143
yield* ipc.handle(showContextMenu);
143144
yield* ipc.handle(openExternal);
145+
yield* ipc.handle(readMedia);
144146
yield* ipc.handle(receiveProviderAuthCallback);
145147
yield* ipc.handle(cancelProviderAuthCallback);
146148
yield* ipc.handle(openSystemSettings);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const PICK_THEME_FILES_CHANNEL = "desktop:pick-theme-files";
55
export const SET_THEME_CHANNEL = "desktop:set-theme";
66
export const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
77
export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
8+
export const READ_MEDIA_CHANNEL = "desktop:read-media";
89
export const OPEN_SYSTEM_SETTINGS_CHANNEL = "desktop:open-system-settings";
910
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
1011
export const MENU_ACTION_CHANNEL = "desktop:menu-action";

‎apps/desktop/src/ipc/methods/window.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Effect from "effect/Effect";
99
import * as Layer from "effect/Layer";
1010
import * as Option from "effect/Option";
1111
import { vi } from "vite-plus/test";
12+
import type * as Electron from "electron";
1213

1314
const { focusedWebContents, ownerWindow } = vi.hoisted(() => ({
1415
focusedWebContents: vi.fn(),
@@ -33,6 +34,7 @@ import {
3334
installRelayTelemetryEnabled,
3435
pickProjectFavicon,
3536
probeRemoteEditors,
37+
readMedia,
3638
} from "./window.ts";
3739

3840
const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
@@ -234,6 +236,54 @@ describe("installRelayTelemetryEnabled", () => {
234236
);
235237
});
236238

239+
describe("readMedia", () => {
240+
it.effect("reads http(s) media only for the main window", () =>
241+
Effect.gen(function* () {
242+
const mib = new Uint8Array(1024 * 1024);
243+
const fetch = vi.fn(async (url: string) => {
244+
if (url.endsWith("/missing.png")) return new Response("Not Found", { status: 404 });
245+
if (url.endsWith("/small.png")) return new Response(new Uint8Array([1, 2]));
246+
let sent = 0;
247+
// 257 MiB from one reused chunk: over the cap without allocating it.
248+
const huge = new ReadableStream<Uint8Array>({
249+
pull: (controller) => (sent++ < 257 ? controller.enqueue(mib) : controller.close()),
250+
});
251+
return new Response(huge);
252+
});
253+
const main = {
254+
webContents: { id: 1, session: { fetch } },
255+
} as unknown as Electron.BrowserWindow;
256+
const read = (url: string, senderId: number) =>
257+
readMedia
258+
.handler(url, { sender: { id: senderId } })
259+
.pipe(
260+
Effect.provide(
261+
Layer.mock(ElectronWindow.ElectronWindow)({ main: Effect.succeedSome(main) }),
262+
),
263+
);
264+
265+
assert.strictEqual(yield* read("https://example.com/a.png", 2), null);
266+
assert.strictEqual(yield* read("file:///etc/passwd", 1), null);
267+
assert.strictEqual(fetch.mock.calls.length, 0);
268+
assert.deepEqual(yield* read("https://example.com/small.png", 1), {
269+
status: 200,
270+
mimeType: "",
271+
data: Buffer.from([1, 2]),
272+
});
273+
assert.deepEqual(yield* read("https://example.com/missing.png", 1), {
274+
status: 404,
275+
mimeType: "text/plain;charset=UTF-8",
276+
data: new Uint8Array(),
277+
});
278+
assert.deepEqual(yield* read("https://example.com/huge.mp4", 1), {
279+
status: 413,
280+
mimeType: "",
281+
data: new Uint8Array(),
282+
});
283+
}),
284+
);
285+
});
286+
237287
describe("pickProjectFavicon", () => {
238288
const pickerLayer = (
239289
pickFiles: () => Effect.Effect<Array<string>>,

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ContextMenuItemSchema,
33
DesktopAppBrandingSchema,
44
DesktopEnvironmentBootstrapSchema,
5+
DesktopMediaReadSchema,
56
DesktopThemeSchema,
67
EDITORS,
78
EditorId,
@@ -311,6 +312,47 @@ export const openExternal = DesktopIpc.makeIpcMethod({
311312
}),
312313
});
313314

315+
const READ_MEDIA_MAX_BYTES = 256 * 1024 * 1024;
316+
317+
// The renderer can display media it cannot fetch(); reading here keeps Save and Copy working for it.
318+
export const readMedia = DesktopIpc.makeIpcMethod({
319+
channel: IpcChannels.READ_MEDIA_CHANNEL,
320+
payload: Schema.String,
321+
result: Schema.NullOr(DesktopMediaReadSchema),
322+
handler: Effect.fn("desktop.ipc.window.readMedia")(function* (url, event) {
323+
const window = yield* (yield* ElectronWindow.ElectronWindow).main;
324+
if (
325+
event === undefined ||
326+
Option.isNone(window) ||
327+
window.value.webContents.id !== event.sender.id ||
328+
!/^https?:$/.test(URL.parse(url)?.protocol ?? "")
329+
) {
330+
return null;
331+
}
332+
const session = window.value.webContents.session;
333+
return yield* Effect.tryPromise(async () => {
334+
// Like an <img>, send the session's cookies.
335+
const response = await session.fetch(url, { credentials: "include" });
336+
const mimeType = response.headers.get("content-type") ?? "";
337+
if (!response.ok || !response.body) {
338+
await response.body?.cancel();
339+
return { status: response.status, mimeType, data: new Uint8Array() };
340+
}
341+
// The whole body crosses IPC, so stop before a huge file can exhaust main-process memory.
342+
const chunks: Uint8Array[] = [];
343+
let size = 0;
344+
for await (const chunk of response.body) {
345+
size += chunk.byteLength;
346+
if (size > READ_MEDIA_MAX_BYTES) {
347+
return { status: 413, mimeType, data: new Uint8Array() };
348+
}
349+
chunks.push(chunk);
350+
}
351+
return { status: response.status, mimeType, data: Buffer.concat(chunks) };
352+
}).pipe(Effect.orElseSucceed(() => null));
353+
}),
354+
});
355+
314356
export const openSystemSettings = DesktopIpc.makeIpcMethod({
315357
channel: IpcChannels.OPEN_SYSTEM_SETTINGS_CHANNEL,
316358
payload: SystemSettingsPaneSchema,

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
202202
cancelProviderAuthCallback: (url: string) =>
203203
ipcRenderer.invoke(IpcChannels.CANCEL_PROVIDER_AUTH_CALLBACK_CHANNEL, url),
204204
openExternal: (url: string) => ipcRenderer.invoke(IpcChannels.OPEN_EXTERNAL_CHANNEL, url),
205+
readMedia: (url: string) => ipcRenderer.invoke(IpcChannels.READ_MEDIA_CHANNEL, url),
205206
checkSystemPermission: (pane: string) =>
206207
ipcRenderer.invoke(IpcChannels.CHECK_SYSTEM_PERMISSION_CHANNEL, pane),
207208
openSystemSettings: (pane: string) =>

‎apps/web/src/components/media/mediaContent.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,22 @@ it("keeps remote media on the fetch path", async () => {
5555
expect(await readMediaPng("https://example.com/image.png")).toEqual(png);
5656
expect(fetch).toHaveBeenCalledWith("https://example.com/image.png");
5757
});
58+
59+
it("reads media the renderer cannot fetch through the desktop main process", async () => {
60+
using fetch = vi.spyOn(globalThis, "fetch").mockRejectedValue(new TypeError("Failed to fetch"));
61+
const readMedia = vi.fn().mockResolvedValue({
62+
status: 200,
63+
mimeType: "image/png",
64+
data: new Uint8Array(PNG_BYTES),
65+
});
66+
vi.stubGlobal("window", { desktopBridge: { readMedia } });
67+
try {
68+
const png = await readMediaPng("https://example.com/image.png");
69+
70+
expect(fetch).toHaveBeenCalledOnce();
71+
expect(readMedia).toHaveBeenCalledWith("https://example.com/image.png");
72+
expect([...new Uint8Array(await png.arrayBuffer())]).toEqual(PNG_BYTES);
73+
} finally {
74+
vi.unstubAllGlobals();
75+
}
76+
});

‎apps/web/src/components/media/mediaContent.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,28 @@ function readDataUrlBlob(src: string): Blob {
4040
}
4141
}
4242

43+
async function fetchMediaBlob(src: string): Promise<Blob> {
44+
const response = await fetch(src).catch(() => null);
45+
if (response?.ok) return response.blob();
46+
// An <img> can show media that fetch() may not read (CORS, CSP, cookies); desktop reads it natively.
47+
const desktop = typeof window === "undefined" ? undefined : window.desktopBridge;
48+
const native =
49+
desktop && /^https?:/i.test(src) ? await desktop.readMedia(src).catch(() => null) : null;
50+
if (native && native.status >= 200 && native.status < 300) {
51+
return new Blob([new Uint8Array(native.data)], { type: native.mimeType });
52+
}
53+
const status = native?.status ?? response?.status;
54+
if (status !== undefined) throw new Error(`The file could not be fetched (HTTP ${status}).`);
55+
throw new Error(
56+
desktop
57+
? "The file could not be fetched. Check the connection and try again."
58+
: "The file could not be fetched. The host may block browser access (CORS), or the connection may be unavailable.",
59+
);
60+
}
61+
4362
async function readMediaBlob(src: string): Promise<Blob> {
4463
if (/^data:/i.test(src)) return readDataUrlBlob(src);
45-
let response: Response;
46-
try {
47-
response = await fetch(src);
48-
} catch (cause) {
49-
throw new Error(
50-
"The file could not be fetched. The host may block browser access (CORS), or the connection may be unavailable.",
51-
{ cause },
52-
);
53-
}
54-
if (!response.ok) throw new Error(`The file could not be fetched (HTTP ${response.status}).`);
55-
const blob = await response.blob();
64+
const blob = await fetchMediaBlob(src);
5665
if (blob.type.split(";", 1)[0] === "text/html") {
5766
throw new Error("This link returned a web page instead of media. Open the original URL.");
5867
}

‎packages/contracts/src/ipc.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,13 @@ export const DesktopPreviewRecordingSaveInputSchema = Schema.Struct({
10121012
data: Schema.Uint8Array,
10131013
});
10141014

1015+
export const DesktopMediaReadSchema = Schema.Struct({
1016+
status: Schema.Number,
1017+
mimeType: Schema.String,
1018+
data: Schema.Uint8Array,
1019+
});
1020+
export type DesktopMediaRead = typeof DesktopMediaReadSchema.Type;
1021+
10151022
export const SystemSettingsPaneSchema = Schema.Literals(["full-disk-access"]);
10161023
export type SystemSettingsPane = typeof SystemSettingsPaneSchema.Type;
10171024

@@ -1099,6 +1106,8 @@ export interface DesktopBridge {
10991106
receiveProviderAuthCallback: (authorizationUrl: string) => Promise<string>;
11001107
cancelProviderAuthCallback: (authorizationUrl: string) => Promise<void>;
11011108
openExternal: (url: string) => Promise<boolean>;
1109+
/** Reads http(s) media in the main process, outside renderer CORS and CSP; null on network failure. */
1110+
readMedia: (url: string) => Promise<DesktopMediaRead | null>;
11021111
openSystemSettings: (pane: SystemSettingsPane) => Promise<boolean>;
11031112
checkSystemPermission: (pane: SystemSettingsPane) => Promise<boolean>;
11041113
probeRemoteEditors: () => Promise<readonly EditorId[]>;

0 commit comments

Comments
 (0)