Skip to content

Commit 55f95c4

Browse files
Merge origin/main into fix/grok-held-turns
2 parents 307dfab + 716357c commit 55f95c4

423 files changed

Lines changed: 21913 additions & 3243 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ describe("pickProjectFavicon", () => {
287287
filters: [
288288
{
289289
name: "Images",
290-
extensions: ["avif", "gif", "ico", "jpeg", "jpg", "png", "svg", "webp"],
290+
extensions: ["avif", "gif", "ico", "jpeg", "jpg", "png", "svg", "webp", "icns"],
291291
},
292292
],
293293
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
type DesktopEnvironmentBootstrap,
1414
type PickedThemeFile,
1515
} from "@t3tools/contracts";
16-
import { WORKSPACE_IMAGE_PREVIEW_EXTENSIONS } from "@t3tools/shared/filePreview";
16+
import { PROJECT_FAVICON_EXTENSIONS } from "@t3tools/shared/projectFavicon";
1717
import { resolveEditorCommand } from "@t3tools/shared/editor";
1818
import * as HostProcess from "@t3tools/shared/HostProcess";
1919
import * as FileSystem from "effect/FileSystem";
@@ -270,7 +270,7 @@ export const pickProjectFavicon = DesktopIpc.makeIpcMethod({
270270
filters: [
271271
{
272272
name: "Images",
273-
extensions: WORKSPACE_IMAGE_PREVIEW_EXTENSIONS.map((extension) => extension.slice(1)),
273+
extensions: PROJECT_FAVICON_EXTENSIONS.map((extension) => extension.slice(1)),
274274
},
275275
],
276276
});

‎apps/desktop/src/preview/FaviconCapture.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MAX_FAVICON_RESPONSE_BYTES,
66
captureFavicon,
77
selectFaviconCandidates,
8+
sourceDimensions,
89
} from "./FaviconCapture.ts";
910

1011
const PNG = "data:image/png;base64,cG5n";
@@ -47,6 +48,26 @@ function sourceGif(
4748
return buffer;
4849
}
4950

51+
it.each([
52+
["logical pixels", sourceGif(2048, 1024), { width: 2048, height: 1024 }],
53+
[
54+
"cumulative frame pixels",
55+
sourceGif(1024, 1024, 1024, 1024, [{ width: 1024, height: 1024 }]),
56+
{ width: 1024, height: 1024 },
57+
],
58+
])("uses the caller's GIF limit for %s while keeping the favicon limit", (_, gif, dimensions) => {
59+
expect(sourceDimensions(gif)).toBeNull();
60+
expect(sourceDimensions(gif, 8_388_608)).toEqual(dimensions);
61+
});
62+
63+
it("rejects GIF frames exceeding the caller's cumulative pixel limit", () => {
64+
const gif = sourceGif(2048, 2048, 2048, 2048, [
65+
{ width: 2048, height: 2048 },
66+
{ width: 2048, height: 2048 },
67+
]);
68+
expect(sourceDimensions(gif, 8_388_608)).toBeNull();
69+
});
70+
5071
function sourceJpeg(
5172
width: number,
5273
height: number,

‎apps/desktop/src/preview/FaviconCapture.ts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,17 @@ interface ImageDimensions {
246246
readonly height: number;
247247
}
248248

249-
function safeDimensions(dimensions: ImageDimensions | null): dimensions is ImageDimensions {
249+
function safeDimensions(
250+
dimensions: ImageDimensions | null,
251+
maxPixels = MAX_FAVICON_SOURCE_PIXELS,
252+
): dimensions is ImageDimensions {
250253
return (
251254
dimensions !== null &&
252255
Number.isSafeInteger(dimensions.width) &&
253256
Number.isSafeInteger(dimensions.height) &&
254257
dimensions.width > 0 &&
255258
dimensions.height > 0 &&
256-
dimensions.width * dimensions.height <= MAX_FAVICON_SOURCE_PIXELS
259+
dimensions.width * dimensions.height <= maxPixels
257260
);
258261
}
259262

@@ -276,13 +279,13 @@ function skipGifSubBlocks(buffer: Buffer, startOffset: number): number | null {
276279
return null;
277280
}
278281

279-
function gifDimensions(buffer: Buffer): ImageDimensions | null {
282+
function gifDimensions(buffer: Buffer, maxPixels: number): ImageDimensions | null {
280283
if (buffer.byteLength < 13 || !/^GIF8[79]a$/u.test(buffer.subarray(0, 6).toString("ascii"))) {
281284
return null;
282285
}
283286
const logicalWidth = buffer.readUInt16LE(6);
284287
const logicalHeight = buffer.readUInt16LE(8);
285-
if (!safeDimensions({ width: logicalWidth, height: logicalHeight })) return null;
288+
if (!safeDimensions({ width: logicalWidth, height: logicalHeight }, maxPixels)) return null;
286289
const packed = buffer[10]!;
287290
let offset = 13 + ((packed & 0x80) === 0 ? 0 : 3 * 2 ** ((packed & 0x07) + 1));
288291
if (offset > buffer.byteLength) return null;
@@ -301,10 +304,10 @@ function gifDimensions(buffer: Buffer): ImageDimensions | null {
301304
const frameHeight = buffer.readUInt16LE(offset + 7);
302305
if (frameWidth === 0 || frameHeight === 0) return null;
303306
framePixels += frameWidth * frameHeight;
304-
if (framePixels > MAX_FAVICON_SOURCE_PIXELS) return null;
307+
if (framePixels > maxPixels) return null;
305308
width = Math.max(width, left + frameWidth);
306309
height = Math.max(height, top + frameHeight);
307-
if (!safeDimensions({ width, height })) return null;
310+
if (!safeDimensions({ width, height }, maxPixels)) return null;
308311
const framePacked = buffer[offset + 9]!;
309312
offset += 10;
310313
if ((framePacked & 0x80) !== 0) {
@@ -491,7 +494,7 @@ function dibDimensions(buffer: Buffer): ImageDimensions | null {
491494
};
492495
}
493496

494-
function icoDimensions(buffer: Buffer): ImageDimensions | null {
497+
function icoDimensions(buffer: Buffer, maxPixels: number): ImageDimensions | null {
495498
if (
496499
buffer.byteLength < 22 ||
497500
buffer.readUInt16LE(0) !== 0 ||
@@ -507,7 +510,7 @@ function icoDimensions(buffer: Buffer): ImageDimensions | null {
507510
const offset = 6 + index * 16;
508511
width = Math.max(width, buffer[offset] === 0 ? 256 : buffer[offset]!);
509512
height = Math.max(height, buffer[offset + 1] === 0 ? 256 : buffer[offset + 1]!);
510-
if (!safeDimensions({ width, height })) return null;
513+
if (!safeDimensions({ width, height }, maxPixels)) return null;
511514
const byteLength = buffer.readUInt32LE(offset + 8);
512515
const imageOffset = buffer.readUInt32LE(offset + 12);
513516
if (
@@ -519,18 +522,21 @@ function icoDimensions(buffer: Buffer): ImageDimensions | null {
519522
return null;
520523
const embedded = buffer.subarray(imageOffset, imageOffset + byteLength);
521524
const embeddedDimensions = pngDimensions(embedded) ?? dibDimensions(embedded);
522-
if (!safeDimensions(embeddedDimensions)) return null;
525+
if (!safeDimensions(embeddedDimensions, maxPixels)) return null;
523526
}
524527
return { width, height };
525528
}
526529

527-
function sourceDimensions(buffer: Buffer): ImageDimensions | null {
530+
export function sourceDimensions(
531+
buffer: Buffer,
532+
maxPixels = MAX_FAVICON_SOURCE_PIXELS,
533+
): ImageDimensions | null {
528534
return (
529535
pngDimensions(buffer) ??
530-
gifDimensions(buffer) ??
536+
gifDimensions(buffer, maxPixels) ??
531537
jpegDimensions(buffer) ??
532538
webpDimensions(buffer) ??
533-
icoDimensions(buffer)
539+
icoDimensions(buffer, maxPixels)
534540
);
535541
}
536542

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import { beforeEach, expect, it, vi } from "vite-plus/test";
2+
import type * as Electron from "electron";
3+
import { copyContextMenuImage } from "./ContextMenuImage.ts";
4+
5+
const native = vi.hoisted(() => ({
6+
fetch: vi.fn(),
7+
createFromBuffer: vi.fn(),
8+
write: vi.fn(),
9+
image: { isEmpty: vi.fn(() => false), toPNG: vi.fn(() => Buffer.from("png")) },
10+
}));
11+
12+
vi.mock("electron", () => ({
13+
net: { fetch: native.fetch },
14+
nativeImage: { createFromBuffer: native.createFromBuffer },
15+
clipboard: { write: native.write },
16+
ClipboardItem: class {
17+
readonly blobs: Record<string, Blob>;
18+
constructor(blobs: Record<string, Blob>) {
19+
this.blobs = blobs;
20+
}
21+
},
22+
}));
23+
24+
const mainFrame = { isDestroyed: vi.fn(() => false) };
25+
const frame = { isDestroyed: vi.fn(() => false) };
26+
const contents = {
27+
isDestroyed: vi.fn(() => false),
28+
copyImageAt: vi.fn(),
29+
executeJavaScriptInIsolatedWorld: vi.fn(),
30+
mainFrame,
31+
};
32+
const params = {
33+
x: 12,
34+
y: 34,
35+
srcURL: "https://example.com/image.png",
36+
frame,
37+
};
38+
const sourcePng = Buffer.from(
39+
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAFklEQVR4nGP4TyFgGDVg1IBRA4aLAQBdePwur/3haQAAAABJRU5ErkJggg==",
40+
"base64",
41+
);
42+
43+
const copyImage = (overrides: Partial<typeof params> = {}) =>
44+
copyContextMenuImage(
45+
contents as unknown as Electron.WebContents,
46+
{ ...params, ...overrides } as unknown as Electron.ContextMenuParams,
47+
);
48+
49+
beforeEach(() => {
50+
vi.clearAllMocks();
51+
contents.isDestroyed.mockReturnValue(false);
52+
frame.isDestroyed.mockReturnValue(false);
53+
native.image.isEmpty.mockReturnValue(false);
54+
native.createFromBuffer.mockReturnValue(native.image);
55+
native.fetch.mockResolvedValue(new Response(Uint8Array.from(sourcePng)));
56+
native.write.mockResolvedValue(undefined);
57+
contents.executeJavaScriptInIsolatedWorld.mockRejectedValue(
58+
new Error("Image could not be decoded"),
59+
);
60+
});
61+
62+
it("preserves the clipboard when the context-menu frame is missing", async () => {
63+
await copyContextMenuImage(
64+
contents as unknown as Electron.WebContents,
65+
{ ...params, frame: null } as unknown as Electron.ContextMenuParams,
66+
);
67+
expect(contents.copyImageAt).not.toHaveBeenCalled();
68+
expect(native.fetch).not.toHaveBeenCalled();
69+
expect(native.write).not.toHaveBeenCalled();
70+
});
71+
72+
it("keeps native image copying for the main frame", async () => {
73+
await copyImage({ frame: mainFrame });
74+
expect(contents.copyImageAt).toHaveBeenCalledWith(12, 34);
75+
expect(native.fetch).not.toHaveBeenCalled();
76+
});
77+
78+
it.each(["https://example.com/image.png", `data:image/png;base64,${sourcePng.toString("base64")}`])(
79+
"copies an iframe image from %s",
80+
async (srcURL) => {
81+
await copyImage({ srcURL });
82+
if (srcURL.startsWith("https:")) {
83+
expect(native.fetch).toHaveBeenCalledWith(srcURL, {
84+
credentials: "omit",
85+
redirect: "error",
86+
signal: expect.any(AbortSignal),
87+
});
88+
} else {
89+
expect(native.fetch).not.toHaveBeenCalled();
90+
}
91+
expect(contents.copyImageAt).not.toHaveBeenCalled();
92+
expect(native.createFromBuffer).toHaveBeenCalledWith(sourcePng);
93+
const blob = native.write.mock.calls[0]?.[0][0].blobs["image/png"] as Blob;
94+
expect(blob.type).toBe("image/png");
95+
expect(Buffer.from(await blob.arrayBuffer())).toEqual(Buffer.from("png"));
96+
},
97+
);
98+
99+
it.each([
100+
["WebP", "UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAUAmJaQAA3AA/vz0AAA="],
101+
["GIF", "R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAkQBADs="],
102+
])("copies %s using Chromium when nativeImage cannot decode it", async (_, base64) => {
103+
native.fetch.mockResolvedValue(new Response(Uint8Array.from(Buffer.from(base64, "base64"))));
104+
native.image.isEmpty.mockReturnValue(true);
105+
contents.executeJavaScriptInIsolatedWorld.mockResolvedValue(Uint8Array.from(sourcePng).buffer);
106+
await copyImage();
107+
const blob = native.write.mock.calls[0]?.[0][0].blobs["image/png"] as Blob;
108+
expect(Buffer.from(await blob.arrayBuffer())).toEqual(sourcePng);
109+
expect(native.image.toPNG).not.toHaveBeenCalled();
110+
});
111+
112+
it("preserves the clipboard if the source frame closes during Chromium decoding", async () => {
113+
native.image.isEmpty.mockReturnValue(true);
114+
contents.executeJavaScriptInIsolatedWorld.mockImplementation(() => {
115+
frame.isDestroyed.mockReturnValue(true);
116+
return Promise.resolve(Uint8Array.from(sourcePng).buffer);
117+
});
118+
await copyImage();
119+
expect(native.write).not.toHaveBeenCalled();
120+
});
121+
122+
it("copies a GIF larger than the favicon pixel limit", async () => {
123+
const gif = Buffer.from("R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAkQBADs=", "base64");
124+
gif.writeUInt16LE(2048, 6);
125+
gif.writeUInt16LE(1024, 8);
126+
native.fetch.mockResolvedValue(new Response(Uint8Array.from(gif)));
127+
native.image.isEmpty.mockReturnValue(true);
128+
contents.executeJavaScriptInIsolatedWorld.mockResolvedValue(Uint8Array.from(sourcePng).buffer);
129+
await copyImage();
130+
const blob = native.write.mock.calls[0]?.[0][0].blobs["image/png"] as Blob;
131+
expect(Buffer.from(await blob.arrayBuffer())).toEqual(sourcePng);
132+
});
133+
134+
it.each([
135+
"file:///private/image.png",
136+
"blob:https://example.com/image",
137+
"https://user:[email protected]/image.png",
138+
])("rejects unsupported or credential-bearing URLs: %s", async (srcURL) => {
139+
await expect(copyImage({ srcURL })).rejects.toThrow("Unsupported image URL");
140+
expect(native.fetch).not.toHaveBeenCalled();
141+
expect(native.write).not.toHaveBeenCalled();
142+
});
143+
144+
it("cancels responses whose declared size exceeds the limit", async () => {
145+
const cancel = vi.fn();
146+
native.fetch.mockResolvedValue(
147+
new Response(new ReadableStream({ cancel }), {
148+
headers: { "content-length": String(32 * 1024 * 1024 + 1) },
149+
}),
150+
);
151+
await expect(copyImage()).rejects.toThrow("size limit");
152+
expect(cancel).toHaveBeenCalled();
153+
expect(native.createFromBuffer).not.toHaveBeenCalled();
154+
});
155+
156+
it("cancels oversized streams without a content-length", async () => {
157+
const cancel = vi.fn();
158+
native.fetch.mockResolvedValue(
159+
new Response(
160+
new ReadableStream({
161+
start(controller) {
162+
controller.enqueue(new Uint8Array(32 * 1024 * 1024));
163+
controller.enqueue(new Uint8Array(1));
164+
},
165+
cancel,
166+
}),
167+
),
168+
);
169+
await expect(copyImage()).rejects.toThrow("size limit");
170+
expect(cancel).toHaveBeenCalled();
171+
expect(native.write).not.toHaveBeenCalled();
172+
});
173+
174+
it("preserves the clipboard when the response is not an image", async () => {
175+
native.image.isEmpty.mockReturnValue(true);
176+
await expect(copyImage()).rejects.toThrow("could not be decoded");
177+
expect(native.write).not.toHaveBeenCalled();
178+
});
179+
180+
it("rejects large decoded dimensions before image allocation", async () => {
181+
const oversizedPng = Buffer.from(sourcePng);
182+
oversizedPng.writeUInt32BE(4096, 16);
183+
oversizedPng.writeUInt32BE(4096, 20);
184+
native.fetch.mockResolvedValue(new Response(Uint8Array.from(oversizedPng)));
185+
await expect(copyImage()).rejects.toThrow("size limit");
186+
expect(native.createFromBuffer).not.toHaveBeenCalled();
187+
expect(native.image.toPNG).not.toHaveBeenCalled();
188+
expect(native.write).not.toHaveBeenCalled();
189+
});
190+
191+
it("preserves the clipboard when the encoded PNG exceeds the limit", async () => {
192+
native.image.toPNG.mockReturnValueOnce(Buffer.alloc(32 * 1024 * 1024 + 1));
193+
await expect(copyImage()).rejects.toThrow("size limit");
194+
expect(native.write).not.toHaveBeenCalled();
195+
});
196+
197+
it("does not copy after the source frame closes during the request", async () => {
198+
native.fetch.mockImplementation(() => {
199+
frame.isDestroyed.mockReturnValue(true);
200+
return Promise.resolve(new Response(Uint8Array.from(sourcePng)));
201+
});
202+
await copyImage();
203+
expect(native.write).not.toHaveBeenCalled();
204+
});

0 commit comments

Comments
 (0)