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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import assert from "node:assert/strict";
import test from "node:test";
import {
SANDBOX_INLINE_IMAGE_EXTS,
markdownSandboxImageSrc,
sandboxFileForSrc,
sandboxFilePath,
sandboxSessionInSrc,
} from "../src/components/assistant-ui/sandbox-files.ts";
import { safeMarkdownUrl } from "../src/lib/safe-markdown-url.ts";
import { readText } from "./helpers/kit.ts";
/* Src rewrite and authed fetch must join; the extension list must match _SANDBOX_MEDIA_TYPES. */
const MARKDOWN_TEXT = readText("../src/components/assistant-ui/markdown-text.tsx");
const HOOK = readText("../src/components/assistant-ui/use-sandbox-image.ts");
const TOOL_UI = readText("../src/components/assistant-ui/tool-ui-python.tsx");
const BACKEND = readText(
"../../backend/routes/inference.py",
);
test("decoded separators cannot bypass sandbox traversal checks", () => {
const context = { threadId: "current", projectId: null };
for (const src of [
"%2e%2e%2fother/plot.png",
"outputs/%2E%2E%2F%2E%2E%2Fother/plot.png",
"%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2foutside/plot.png",
"..%5cother/plot.png",
"..\\other/plot.png",
"/api/inference/sandbox/recorded/%2e%2e%2fother/plot.png",
]) {
assert.equal(sandboxFileForSrc(src), null, src);
assert.equal(markdownSandboxImageSrc(src, context), null, src);
}
assert.equal(
markdownSandboxImageSrc("outputs/loss%20curve%20%231.png", context),
"/api/inference/sandbox/current/outputs/loss%20curve%20%231.png",
);
});
test("recorded session IDs round-trip without a second query decode", () => {
for (const session of ["api%2Fclient", "literal%23id", "literal%25id", "a+b", "session/id", "100%", "project-old"]) {
const src = sandboxFilePath(session, "plot.png");
assert.equal(sandboxSessionInSrc(src), session);
assert.equal(
markdownSandboxImageSrc(src, { threadId: "new-thread", projectId: "new-project" }),
src,
);
}
});
test("a fragment cannot override the recorded sandbox session", () => {
for (const session of ["original", "api%2Fclient"]) {
const src = sandboxFilePath(session, "plot.png");
assert.equal(sandboxSessionInSrc(`${src}#?session=other`), session);
assert.equal(
markdownSandboxImageSrc(`${src}#?session=other`, { threadId: "current", projectId: null }),
src,
);
}
});
test("a scheme-less sandbox src is rewritten before it reaches the DOM, and rendered from a blob:", () => {
assert.ok(
MARKDOWN_TEXT.includes("urlTransform={safeMarkdownUrl}"),
"PRECONDITION: the chat renderer still passes the url transform",
);
assert.ok(
/img: MarkdownImage,/.test(MARKDOWN_TEXT),
"registering `img` replaces Streamdown's own renderer wholesale, so the swap has to live in the " +
"component it replaces -- an unregistered <img src> is the bug this file exists for",
);
assert.ok(
/markdownSandboxImageSrc\(src,\s*\{/.test(MARKDOWN_TEXT),
"a src that RECORDS a session keeps it -- the folder its files were written to is where they " +
"still are after a move, and it is what the tool card above the prose already resolves from; " +
"only one that records nothing falls back to this chat's scope",
);
assert.ok(
MARKDOWN_TEXT.includes("useSandboxImage(file)"),
"and it is fetched rather than rendered raw",
);
const imgNode = { tagName: "img" } as Parameters<typeof safeMarkdownUrl>[2];
const written = "/api/inference/sandbox/__LOCALID_Y3VK67e/plot.png";
assert.equal(safeMarkdownUrl(written, "src", imgNode), written);
// The recorded session (where the file was written) wins over this chat's scope.
assert.equal(
markdownSandboxImageSrc(written, { threadId: "t-1", projectId: null }),
"/api/inference/sandbox/__LOCALID_Y3VK67e/plot.png",
);
assert.equal(
markdownSandboxImageSrc("plot.png", { threadId: "t-1", projectId: null }),
"/api/inference/sandbox/t-1/plot.png",
);
assert.equal(markdownSandboxImageSrc("data:image/png;base64,AAAA", { threadId: "t-1", projectId: null }), null);
});
test("the fetch carries the header and gives the object URL back on cleanup", () => {
// The route authenticates via header; a bare <img src> gets a 401.
assert.ok(/authFetch\(url,\s*\{ signal: controller\.signal \}\)/.test(HOOK), "authenticated fetch");
assert.ok(HOOK.includes("URL.createObjectURL(blob)"), "into an object URL");
// Two assertions, because a comment sits between the two statements in the hook's cleanup.
assert.ok(HOOK.includes("controller.abort();"), "the in-flight fetch is cancelled");
assert.ok(
HOOK.includes("if (objectUrl) URL.revokeObjectURL(objectUrl);"),
"and an object URL a cancelled fetch had already built is revoked, or those bytes stay pinned " +
"for the rest of the session",
);
// Single shared hook: duplication is how the markdown path lost it.
assert.ok(TOOL_UI.includes("useSandboxImage(pythonToolImagePath(sessionId, filename))"));
assert.equal(
TOOL_UI.includes("createObjectURL"),
false,
"the Python card now goes through the shared hook instead of keeping its own copy",
);
});
test("the inline-image set matches what the backend actually serves inline", () => {
const served = BACKEND.slice(
BACKEND.indexOf("_SANDBOX_MEDIA_TYPES = {"),
BACKEND.indexOf("_SANDBOX_MEDIA_TYPES = {") >= 0
? BACKEND.indexOf("}", BACKEND.indexOf("_SANDBOX_MEDIA_TYPES = {"))
: 0,
);
assert.ok(served.length > 50, "PRECONDITION: the backend map was actually read");
for (const ext of SANDBOX_INLINE_IMAGE_EXTS) {
assert.ok(
served.includes(`"${ext}"`),
`${ext} is rendered as an <img> but the route would serve it as application/octet-stream`,
);
}
assert.ok(
!served.includes('".svg"'),
"SVG stays out on purpose: the filename is model-chosen, so inline SVG would be same-origin " +
"script execution",
);
assert.ok(
/X-Content-Type-Options"\s*=\s*"nosniff/.test(BACKEND) ||
BACKEND.includes('"X-Content-Type-Options": "nosniff"'),
"the pin that makes a raster codec safe to add",
);
});
test("a non-image stays a download card, and a scheme-carrying src is left alone", () => {
assert.equal(sandboxFileForSrc("report.csv"), null);
assert.equal(sandboxFileForSrc("diagram.svg"), null);
assert.equal(sandboxFileForSrc("/assets/logo.png"), null);
assert.equal(sandboxFileForSrc("//img.example.com/x.png"), null);
assert.equal(
sandboxFileForSrc("/api/inference/sandbox/_/loss%20curve%20%231.png?session=session%2Fid"),
"loss curve #1.png",
);
});
test("the img restatement keeps what the wholesale replacement silently dropped", () => {
// Registering img replaces Streamdown's renderer wholesale, so some of its checks live here.
assert.ok(
MARKDOWN_TEXT.includes('data-streamdown="image"'),
"the <img> itself carries the attribute (wrapper and fallback had it; img silently did not)",
);
assert.ok(
MARKDOWN_TEXT.includes("bg-black/10") &&
MARKDOWN_TEXT.includes("pointer-events-none absolute inset-0"),
"the hover overlay div, dropped entirely by the wholesale replacement",
);
assert.ok(
MARKDOWN_TEXT.includes('.replace(/\\.[^/.]+$/'),
'download names: a real extension on the path wins whole; otherwise alt\'s extension is stripped ' +
'and one inferred from blob.type is appended -- alt="plot" downloads "plot.png", not "plot"',
);
assert.ok(
MARKDOWN_TEXT.includes("decodeSegment((file ?? src"),
"the tail is cut with raw delimiters split off FIRST, then decoded: `loss curve #1.png` must " +
"save under its real name, not as loss%20curve%20%231.png",
);
});