Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: decode video attachments instead of fetching a data URL (#16254)

Yakshitha3dd9b751f33
diff --git a/studio/frontend/src/components/assistant-ui/attachment-browser-open.tsx b/studio/frontend/src/components/assistant-ui/attachment-browser-open.tsx
index 7e8c1e37c44..1ad886388dc 100644
--- a/studio/frontend/src/components/assistant-ui/attachment-browser-open.tsx
+++ b/studio/frontend/src/components/assistant-ui/attachment-browser-open.tsx
@@ -8,6 +8,7 @@ import { authFetch } from "@/features/auth";
import { attachmentBodyText, fetchChatAttachmentBlob, parseAttachmentText } from "@/features/chat";
import { openFileInBrowser } from "@/features/browser";
import { isStudioUrl } from "@/lib/api-base";
+import { urlToBlob } from "@/lib/native-files";
import { toast } from "@/lib/toast";
import { useAuiState } from "@assistant-ui/react";
import { Slot } from "radix-ui";
@@ -38,11 +39,8 @@ function localLoader(source: AttachmentSource): (() => Promise<Opened>) | null {
const { video, src } = source;
const url = video ? (video.data.startsWith("data:") ? video.data : `data:${video.mimeType};base64,${video.data}`) : src;
if (!url) return null;
- return () =>
- fetch(url).then(async (response) => {
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
- return { blob: await response.blob() };
- });
+ // Not fetch(): connect-src allows no data:, so a packaged or served build refuses it.
+ return () => urlToBlob(url).then((blob) => ({ blob }));
}
case "document":
return !source.hasOriginal && text !== undefined
@@ -126,12 +124,9 @@ function blobLoader(source: AttachmentSource): (() => Promise<Blob>) | null {
const local = localLoader(source);
if (local) return () => local().then(({ blob }) => blob);
if (src) {
+ if (/^(blob|data):/i.test(src)) return () => urlToBlob(src);
// Only Studio's own URLs get the sign-in; a foreign image must not receive it.
- const request = /^(blob|data):/i.test(src)
- ? () => fetch(src)
- : isStudioUrl(src)
- ? () => authFetch(src)
- : () => fetch(src, { credentials: "omit" });
+ const request = isStudioUrl(src) ? () => authFetch(src) : () => fetch(src, { credentials: "omit" });
return () =>
request().then((response) => {
if (!response.ok) throw new Error(`HTTP ${response.status}`);
diff --git a/studio/frontend/src/components/assistant-ui/attachment-preview.tsx b/studio/frontend/src/components/assistant-ui/attachment-preview.tsx
index fcc4860323b..e75b3614ead 100644
--- a/studio/frontend/src/components/assistant-ui/attachment-preview.tsx
+++ b/studio/frontend/src/components/assistant-ui/attachment-preview.tsx
@@ -50,6 +50,7 @@ import {
} from "@/features/library";
import { useT } from "@/i18n";
import { MAX_HIGHLIGHT_CHARS } from "@/lib/markdown-plugins";
+import { urlToBlob } from "@/lib/native-files";
import { asPng } from "@/lib/copy-to-clipboard";
import { toast } from "@/lib/toast";
import { cn } from "@/lib/utils";
@@ -74,8 +75,6 @@ type TextPreviewState =
// Not .xhtml: the composer sends the backend parser's text for it, which must not render as markup.
const WEB_PAGE_NAME = /\.html?$/i;
-const fetchBlob = (src: string): Promise<Blob> => fetch(src).then((response) => response.blob());
-
const Zoomed: FC<{ scale: number; children: ReactNode }> = ({ scale, children }) => (
<div className="size-full overflow-hidden">
<div
@@ -131,7 +130,7 @@ const useObjectUrl = (file: File | undefined): string | undefined => {
};
const loadGalleryImage = (image: GalleryImage): Promise<Blob> =>
- image.file ? Promise.resolve(image.file) : fetchBlob(image.image ?? "");
+ image.file ? Promise.resolve(image.file) : urlToBlob(image.image ?? "");
const copyImage = (image: GalleryImage, t: ReturnType<typeof useT>): void => {
if (typeof ClipboardItem === "undefined" || !navigator.clipboard?.write) {
@@ -624,7 +623,7 @@ const AttachmentAudioDialog: FC<
file
? () => Promise.resolve(file)
: src || audio
- ? () => fetchBlob(src ?? attachmentAudioSrc(audio!, source.contentType, source.name))
+ ? () => urlToBlob(src ?? attachmentAudioSrc(audio!, source.contentType, source.name))
: undefined
}
>
@@ -670,7 +669,7 @@ const AttachmentVideoDialog: FC<
file
? () => Promise.resolve(file)
: src || video
- ? () => fetchBlob(src ?? attachmentVideoSrc(video!))
+ ? () => urlToBlob(src ?? attachmentVideoSrc(video!))
: undefined
}
>
diff --git a/studio/frontend/tests/video-attachment-open-csp.test.ts b/studio/frontend/tests/video-attachment-open-csp.test.ts
new file mode 100644
index 00000000000..89576a2af34
--- /dev/null
+++ b/studio/frontend/tests/video-attachment-open-csp.test.ts
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Studio's CSP (backend and tauri.conf.json) has no data: in connect-src, so fetch() on a sent
+// attachment's data URL is refused outside `tauri dev`: "Could not open clip.mp4" (#16248).
+
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import test from "node:test";
+
+import type * as OpenModule from "../src/components/assistant-ui/attachment-browser-open.tsx";
+import type * as NativeFilesModule from "../src/lib/native-files.ts";
+import * as dataUri from "../src/lib/data-uri.ts";
+import { loadWithStubs, type StubElement, stubJsxRuntime } from "./helpers/module-stubs.ts";
+
+const CLIP = Uint8Array.of(0, 0, 0, 32, 102, 116, 121, 112);
+const CLIP_B64 = Buffer.from(CLIP).toString("base64");
+
+function load() {
+ const fetched: string[] = [];
+ const opened: { blob: Blob; name: string }[] = [];
+ const realFetch = globalThis.fetch;
+ globalThis.fetch = ((input: unknown) => {
+ fetched.push(String(input));
+ return Promise.reject(new TypeError("Failed to fetch"));
+ }) as typeof fetch;
+ const nativeFiles = loadWithStubs<typeof NativeFilesModule>(new URL("../src/lib/native-files.ts", import.meta.url), {
+ "@/lib/api-base": { isTauri: false },
+ "@/lib/data-uri": dataUri,
+ "@tauri-apps/api/core": {},
+ });
+ const mod = loadWithStubs<typeof OpenModule>(
+ new URL("../src/components/assistant-ui/attachment-browser-open.tsx", import.meta.url),
+ {
+ "react/jsx-runtime": stubJsxRuntime(),
+ react: { useCallback: (fn: unknown) => fn, useLayoutEffect: () => {}, useRef: (v: unknown) => ({ current: v }) },
+ "@/features/auth": { authFetch: () => Promise.reject(new Error("authFetch")) },
+ "@/features/chat": {},
+ "@/features/browser": { openFileInBrowser: (o: { blob: Blob; name: string }) => opened.push(o) },
+ "@/lib/api-base": { isStudioUrl: () => false },
+ "@/lib/data-uri": dataUri,
+ "@/lib/native-files": nativeFiles,
+ "@/lib/toast": { toast: { error: (m: string) => opened.push({ blob: new Blob(), name: `toast:${m}` }) } },
+ "@assistant-ui/react": { useAuiState: () => "id" },
+ "radix-ui": { Slot: { Root: "Slot" } },
+ "./attachment-browser-open-context": { AttachmentBrowserOpenContext: { Provider: "Provider" } },
+ "./link-context-menu": { FileContextMenu: "FileContextMenu" },
+ },
+ );
+ return { mod, fetched, opened, restore: () => (globalThis.fetch = realFetch) };
+}
+
+const source = (over: Record<string, unknown>) =>
+ ({
+ kind: "video",
+ name: "clip.mp4",
+ contentType: "video/mp4",
+ file: undefined,
+ src: undefined,
+ audio: undefined,
+ video: undefined,
+ text: undefined,
+ hasOriginal: false,
+ ...over,
+ }) as Parameters<typeof OpenModule.AttachmentBrowserOpenProvider>[0]["source"];
+
+const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
+
+for (const data of [CLIP_B64, `data:video/mp4;base64,${CLIP_B64}`]) {
+ test(`a sent video opens without fetching its data URL (${data.slice(0, 5)})`, async () => {
+ const { mod, fetched, opened, restore } = load();
+ try {
+ const element = mod.AttachmentBrowserOpenProvider({
+ source: source({ video: { data, mimeType: "video/mp4" } }),
+ children: null,
+ }) as unknown as StubElement;
+ (element.props.open as () => void)();
+ await flush();
+ assert.deepEqual(fetched, []);
+ assert.equal(opened.length, 1);
+ assert.equal(opened[0].name, "clip.mp4");
+ assert.equal(opened[0].blob.type, "video/mp4");
+ assert.deepEqual(new Uint8Array(await opened[0].blob.arrayBuffer()), CLIP);
+ } finally {
+ restore();
+ }
+ });
+}
+
+test("a sent image's context menu loads its data URL without fetching", async () => {
+ const { mod, fetched, restore } = load();
+ try {
+ const element = mod.AttachmentFileContextMenu({
+ source: source({ kind: "image", name: "a.png", contentType: "image/png", src: `data:image/png;base64,${CLIP_B64}` }),
+ children: null as never,
+ }) as unknown as StubElement;
+ const blob = await (element.props.load as () => Promise<Blob>)();
+ assert.deepEqual(fetched, []);
+ assert.equal(blob.type, "image/png");
+ assert.deepEqual(new Uint8Array(await blob.arrayBuffer()), CLIP);
+ } finally {
+ restore();
+ }
+});
+
+test("attachment viewers never fetch() a sent attachment's data URL", () => {
+ const preview = readFileSync(new URL("../src/components/assistant-ui/attachment-preview.tsx", import.meta.url), "utf8");
+ assert.ok(!/\bfetch\(/.test(preview), "attachment-preview.tsx calls fetch(); load data URLs with urlToBlob");
+});