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 21, 08:30 UTC
fix(web): decode HEIC previews under desktop CSP (#38188)
Yash Singh committed
Nov 17, 2026 at 09:36 UTC
d085ee5d155064d98a27e4e936f0316b1d92e70e
4 files changed
+65
-3
apps/web/src/components/files/imageDecode.browser.test.ts
new
+52
@@ -0,0 +1,52 @@
1
+import { expect, it } from "vite-plus/test";
2
+import workerUrl from "./imageDecode.worker.ts?worker&url";
3
+import photoUrl from "../../../test/fixtures/images/photo.heic?url";
4
+
5
+it("decodes HEIC under the desktop content security policy", async () => {
6
+ const bytes = await (await fetch(photoUrl)).arrayBuffer();
7
+ const frame = document.createElement("iframe");
8
+ frame.srcdoc = `<!doctype html><meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval'; worker-src 'self' blob:"><script>
9
+ onmessage = async ({ data }) => {
10
+ const url = URL.createObjectURL(new Blob(['import ' + JSON.stringify(data.workerUrl)], { type: 'text/javascript' }));
11
+ const worker = new Worker(url, { type: 'module' });
12
+ worker.onmessage = ({ data }) => {
13
+ parent.postMessage(data, '*');
14
+ worker.terminate();
15
+ URL.revokeObjectURL(url);
16
+ };
17
+ worker.onerror = (event) => parent.postMessage({ error: event.message }, '*');
18
+ worker.postMessage({ bytes: data.bytes, heif: true }, [data.bytes]);
19
+ };
20
+ </script>`;
21
+ const controller = new AbortController();
22
+ try {
23
+ const loaded = new Promise<void>((resolve) =>
24
+ frame.addEventListener("load", () => resolve(), { once: true }),
25
+ );
26
+ document.body.append(frame);
27
+ await loaded;
28
+ const decoded = new Promise<{ blob?: Blob; error?: string }>((resolve) => {
29
+ window.addEventListener(
30
+ "message",
31
+ (event) => {
32
+ if (event.source === frame.contentWindow) resolve(event.data);
33
+ },
34
+ { signal: controller.signal },
35
+ );
36
+ });
37
+ frame.contentWindow!.postMessage(
38
+ { bytes, workerUrl: new URL(workerUrl, location.href).href },
39
+ "*",
40
+ [bytes],
41
+ );
42
+ const result = await decoded;
43
+ expect(result.error).toBeUndefined();
44
+ expect(result.blob?.type).toBe("image/png");
45
+ const bitmap = await createImageBitmap(result.blob!);
46
+ expect([bitmap.width, bitmap.height]).toEqual([128, 128]);
47
+ bitmap.close();
48
+ } finally {
49
+ controller.abort();
50
+ frame.remove();
51
+ }
52
+});
apps/web/src/components/files/imageDecode.worker.ts
+12
-2
@@ -152,8 +152,18 @@ async function decode(bytes: ArrayBuffer, heif: boolean) {
152
if (bytes.byteLength > 50 * 1024 * 1024) throw new Error("Preview limited to files under 50 MB.");
153
if (heif) {
154
validateHeif(bytes);
155
- const { heicTo } = await import("heic-to/next");
156
- return await heicTo({ blob: new Blob([bytes]), type: "image/png" });
155
+ // Desktop CSP forbids the JavaScript evaluation used by heic-to/next.
156
+ const { heicTo } = await import("heic-to/csp");
157
+ const bitmap = await heicTo({ blob: new Blob([bytes]), type: "bitmap" });
158
+ try {
159
+ const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
160
+ const context = canvas.getContext("2d");
161
+ if (!context) throw new Error("Image decoding is unavailable.");
162
+ context.drawImage(bitmap, 0, 0);
163
+ return await canvas.convertToBlob({ type: "image/png" });
164
+ } finally {
165
+ bitmap.close();
166
+ }
167
}
168
const { decode: decodeTiff } = await import("tiff");
169
validateTiffMetadata(bytes);
apps/web/test/fixtures/images/photo.heic
Binary files /dev/null and b/apps/web/test/fixtures/images/photo.heic differ
apps/web/vite.config.ts
+1
-1
@@ -123,7 +123,7 @@ const browserTestProject = {
123
"fast-xml-parser",
124
"entities",
125
"dompurify",
126
- "heic-to/next",
126
+ "heic-to/csp",
127
"jszip",
128
"pdfjs-dist",
129
"sql.js",