The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 07:00 UTC
fix(desktop): save and copy any image the app can show (#29079)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Nov 1, 2026 at 21:50 UTC
0338e99fe359ea6e4c1b93ef60245597f76cd522
8 files changed
+144
-11
apps/desktop/src/ipc/DesktopIpcHandlers.ts
+2
@@ -40,6 +40,7 @@ import {
40
getSystemLocale,
41
getWindowFullscreenState,
42
openExternal,
43
+ readMedia,
44
openSystemSettings,
45
checkSystemPermission,
46
pasteAsText,
@@ -141,6 +142,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
142
yield* ipc.handle(setTheme);
143
yield* ipc.handle(showContextMenu);
144
yield* ipc.handle(openExternal);
145
+ yield* ipc.handle(readMedia);
146
yield* ipc.handle(receiveProviderAuthCallback);
147
yield* ipc.handle(cancelProviderAuthCallback);
148
yield* ipc.handle(openSystemSettings);
apps/desktop/src/ipc/channels.ts
+1
@@ -5,6 +5,7 @@ export const PICK_THEME_FILES_CHANNEL = "desktop:pick-theme-files";
5
export const SET_THEME_CHANNEL = "desktop:set-theme";
6
export const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
7
export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
8
+export const READ_MEDIA_CHANNEL = "desktop:read-media";
9
export const OPEN_SYSTEM_SETTINGS_CHANNEL = "desktop:open-system-settings";
10
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
11
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
apps/desktop/src/ipc/methods/window.test.ts
+50
@@ -9,6 +9,7 @@ import * as Effect from "effect/Effect";
9
import * as Layer from "effect/Layer";
10
import * as Option from "effect/Option";
11
import { vi } from "vite-plus/test";
12
+import type * as Electron from "electron";
13
14
const { focusedWebContents, ownerWindow } = vi.hoisted(() => ({
15
focusedWebContents: vi.fn(),
@@ -33,6 +34,7 @@ import {
34
installRelayTelemetryEnabled,
35
pickProjectFavicon,
36
probeRemoteEditors,
37
+ readMedia,
38
} from "./window.ts";
39
40
const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
@@ -234,6 +236,54 @@ describe("installRelayTelemetryEnabled", () => {
236
);
237
});
238
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
+
287
describe("pickProjectFavicon", () => {
288
const pickerLayer = (
289
pickFiles: () => Effect.Effect<Array<string>>,
apps/desktop/src/ipc/methods/window.ts
+42
@@ -2,6 +2,7 @@ import {
2
ContextMenuItemSchema,
3
DesktopAppBrandingSchema,
4
DesktopEnvironmentBootstrapSchema,
5
+ DesktopMediaReadSchema,
6
DesktopThemeSchema,
7
EDITORS,
8
EditorId,
@@ -311,6 +312,47 @@ export const openExternal = DesktopIpc.makeIpcMethod({
312
}),
313
});
314
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
+
356
export const openSystemSettings = DesktopIpc.makeIpcMethod({
357
channel: IpcChannels.OPEN_SYSTEM_SETTINGS_CHANNEL,
358
payload: SystemSettingsPaneSchema,
apps/desktop/src/preload.ts
+1
@@ -202,6 +202,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
202
cancelProviderAuthCallback: (url: string) =>
203
ipcRenderer.invoke(IpcChannels.CANCEL_PROVIDER_AUTH_CALLBACK_CHANNEL, url),
204
openExternal: (url: string) => ipcRenderer.invoke(IpcChannels.OPEN_EXTERNAL_CHANNEL, url),
205
+ readMedia: (url: string) => ipcRenderer.invoke(IpcChannels.READ_MEDIA_CHANNEL, url),
206
checkSystemPermission: (pane: string) =>
207
ipcRenderer.invoke(IpcChannels.CHECK_SYSTEM_PERMISSION_CHANNEL, pane),
208
openSystemSettings: (pane: string) =>
apps/web/src/components/media/mediaContent.test.ts
+19
@@ -55,3 +55,22 @@ it("keeps remote media on the fetch path", async () => {
55
expect(await readMediaPng("https://example.com/image.png")).toEqual(png);
56
expect(fetch).toHaveBeenCalledWith("https://example.com/image.png");
57
});
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
+20
-11
@@ -40,19 +40,28 @@ function readDataUrlBlob(src: string): Blob {
40
}
41
}
42
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
+
62
async function readMediaBlob(src: string): Promise<Blob> {
63
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);
65
if (blob.type.split(";", 1)[0] === "text/html") {
66
throw new Error("This link returned a web page instead of media. Open the original URL.");
67
}
packages/contracts/src/ipc.ts
+9
@@ -1012,6 +1012,13 @@ export const DesktopPreviewRecordingSaveInputSchema = Schema.Struct({
1012
data: Schema.Uint8Array,
1013
});
1014
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
+
1022
export const SystemSettingsPaneSchema = Schema.Literals(["full-disk-access"]);
1023
export type SystemSettingsPane = typeof SystemSettingsPaneSchema.Type;
1024
@@ -1099,6 +1106,8 @@ export interface DesktopBridge {
1106
receiveProviderAuthCallback: (authorizationUrl: string) => Promise<string>;
1107
cancelProviderAuthCallback: (authorizationUrl: string) => Promise<void>;
1108
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>;
1111
openSystemSettings: (pane: SystemSettingsPane) => Promise<boolean>;
1112
checkSystemPermission: (pane: SystemSettingsPane) => Promise<boolean>;
1113
probeRemoteEditors: () => Promise<readonly EditorId[]>;