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
Studio: ask before numpy pickle loads, keep audio tags inline, cap the variable-prose regex (#13001)
Daniel Hanaa860c80d3f6
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex 7d1b706eecb..ef4b28f2610 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -2809,6 +2809,9 @@ _AUTO_UNSAFE_PY_ATTRS = frozenset(# Loaders that can execute code embedded in the data they deserialize; gated by receiver module (torch.load,# yaml.load) since bare `load` is too common._AUTO_UNSAFE_PY_LOAD_MODULES = frozenset({"torch", "joblib", "cloudpickle", "yaml"})+# numpy loaders that unpickle under allow_pickle -> its positional index. The gate checks positions 1 and 2 of all of+# them, so an alias pointing at another loader cannot shift the flag out of view.+_NUMPY_PICKLE_FLAG_POS = {"load": 2, "read_array": 1, "NpzFile": 2}# The load entry points on those modules. yaml.load runs whatever its Loader= builds, and !!python/object/apply in the# data is a call, so it asks like the pickle-backed ones. yaml.safe_load is untouched._AUTO_UNSAFE_PY_LOAD_ATTRS = frozenset({"load", "load_all"})@@ -6135,6 +6138,17 @@ def _terminal_is_potentially_unsafe(command: str) -> bool:return False+def _is_literal_false(node: ast.AST) -> bool:+ return isinstance(node, ast.Constant) and node.value is False+++def _is_inert_loader_arg(node: ast.AST, mmap_slot: bool) -> bool:+ # False, None, or a string in load's mmap_mode slot.+ return isinstance(node, ast.Constant) and (+ node.value is None or node.value is False or (mmap_slot and isinstance(node.value, str))+ )++def _python_is_potentially_unsafe(code: str) -> bool:"""Classify python-tool code for auto mode (fail closed)."""if not code or not code.strip():@@ -6177,6 +6191,10 @@ def _python_is_potentially_unsafe(code: str) -> bool:# Module names bound to a pickle-backed loader (import torch as t), so t.load(...) is still gated as a# code-executing deserialize.load_module_aliases = set(_AUTO_UNSAFE_PY_LOAD_MODULES)+ # numpy module aliases, and names / attributes bound to a numpy pickle loader -> its allow_pickle position.+ numpy_aliases = {"numpy"}+ pickle_fn_aliases: "dict[str, int]" = {}+ pickle_fn_attr_aliases: "dict[str, int]" = {}# Names bound to the builtin getattr (g = getattr), so a dynamic lookup aliased through it still fails closed.getattr_aliases = {"getattr"}# Names bound to functools.partial, so a partial that wraps open/a writer fails closed when it is called.@@ -6196,6 +6214,29 @@ def _python_is_potentially_unsafe(code: str) -> bool:# ...) safe.invoker_aliases = set(_HIGHER_ORDER_INVOKERS)+ def _in_numpy(node) -> bool:+ while isinstance(node, ast.Attribute):+ node = node.value+ return isinstance(node, ast.Name) and node.id in numpy_aliases++ def _allow_pickle_position(node) -> "int | None":+ # A conditional or boolean callee counts if any branch is a loader.+ if isinstance(node, ast.NamedExpr):+ return _allow_pickle_position(node.value)+ if isinstance(node, (ast.IfExp, ast.BoolOp)):+ branches = [node.body, node.orelse] if isinstance(node, ast.IfExp) else node.values+ return next(+ (pos for pos in map(_allow_pickle_position, branches) if pos is not None), None+ )+ if isinstance(node, ast.Name):+ return pickle_fn_aliases.get(node.id)+ if isinstance(node, ast.Attribute):+ if node.attr in pickle_fn_attr_aliases:+ return pickle_fn_attr_aliases[node.attr]+ if node.attr in _NUMPY_PICKLE_FLAG_POS and _in_numpy(node.value):+ return _NUMPY_PICKLE_FLAG_POS[node.attr]+ return None+def _is_dynamic_namespace(node) -> bool:# A namespace mapping whose .get/.pop/.setdefault (or subscript) can return open/eval/a mutator:# globals()/locals()/vars(...), any X.__dict__, __builtins__, sys.modules. Looking a name up through one is as@@ -6304,11 +6345,23 @@ def _python_is_potentially_unsafe(code: str) -> bool:os_aliases.add(alias.asname or alias.name)elif alias.name in _AUTO_UNSAFE_PY_LOAD_MODULES:load_module_aliases.add(alias.asname or alias.name)+ elif alias.name.split(".")[0] == "numpy":+ numpy_aliases.add(alias.asname or "numpy")elif alias.name == "operator":operator_aliases.add(alias.asname or "operator")elif alias.name == "fileinput":fileinput_aliases.add(alias.asname or "fileinput")elif isinstance(node, ast.ImportFrom):+ if node.module and node.module.split(".")[0] == "numpy":+ for alias in node.names:+ if alias.name == "*":+ pickle_fn_aliases.update(_NUMPY_PICKLE_FLAG_POS)+ elif alias.name in _NUMPY_PICKLE_FLAG_POS:+ pickle_fn_aliases[alias.asname or alias.name] = _NUMPY_PICKLE_FLAG_POS[+ alias.name+ ]+ else:+ numpy_aliases.add(alias.asname or alias.name)if node.module == "operator":for alias in node.names:if alias.name == "methodcaller":@@ -6360,6 +6413,11 @@ def _python_is_potentially_unsafe(code: str) -> bool:assign_targets = node.targetstargets = [t.id for t in assign_targets if isinstance(t, ast.Name)]attr_targets = [t.attr for t in assign_targets if isinstance(t, ast.Attribute)]+ if _in_numpy(value):+ numpy_aliases.update(targets)+ if (_pos := _allow_pickle_position(value)) is not None:+ pickle_fn_aliases.update(dict.fromkeys(targets, _pos))+ pickle_fn_attr_aliases.update(dict.fromkeys(attr_targets, _pos))if isinstance(value, ast.Name) and value.id in open_aliases:open_aliases.update(targets)attr_open_aliases.update(attr_targets) # box.f = open@@ -6465,9 +6523,16 @@ def _python_is_potentially_unsafe(code: str) -> bool:value.elts):for tgt_el, val_el in zip(target.elts, value.elts):+ if (+ isinstance(tgt_el, ast.Attribute)+ and (_pos := _allow_pickle_position(val_el)) is not None+ ):+ pickle_fn_attr_aliases[tgt_el.attr] = _posif not isinstance(tgt_el, ast.Name):continuetid = tgt_el.id+ if (_pos := _allow_pickle_position(val_el)) is not None:+ pickle_fn_aliases[tid] = _posif isinstance(val_el, ast.Name) and val_el.id in open_aliases:open_aliases.add(tid)elif isinstance(val_el, ast.Name) and val_el.id in getattr_aliases:@@ -6508,6 +6573,8 @@ def _python_is_potentially_unsafe(code: str) -> bool:)) + [(p, d) for p, d in zip(_a.kwonlyargs, _a.kw_defaults) if d is not None]for _param, _default in _defaulted:+ if (_pos := _allow_pickle_position(_default)) is not None:+ pickle_fn_aliases[_param.arg] = _posif isinstance(_default, ast.Name):_did = _default.idif _did in open_aliases:@@ -6622,6 +6689,9 @@ def _python_is_potentially_unsafe(code: str) -> bool:# rm("x")).if node.attr in _AUTO_UNSAFE_PY_ATTRS:return True+ # z = np.load("a.npz"); z.allow_pickle = True turns pickling on for the next z["x"].+ if node.attr == "allow_pickle" and isinstance(node.ctx, ast.Store):+ return True# builtins.exec / eval / __import__ (and compile/breakpoint) are dynamic code execution, matching the# bare-name code_exec_aliases path; __builtins__.__import__(...) is a dynamic import that dodges the# static import check.@@ -6664,6 +6734,22 @@ def _python_is_potentially_unsafe(code: str) -> bool:func = func.valueif isinstance(func, (ast.Call, ast.Subscript)):return True # calling a call/subscript result is dynamic+ # numpy allow_pickle unpickles like torch.load: only a literal False (None/False positionally) is safe.+ if any(+ kw.arg == "allow_pickle" and not _is_literal_false(kw.value)+ for kw in node.keywords+ ) or (+ (_flag_pos := _allow_pickle_position(func)) is not None+ and (+ not all(+ _is_inert_loader_arg(arg, mmap_slot = i == 1 and _flag_pos == 2)+ for i, arg in enumerate(node.args[1:3], start = 1)+ )+ or any(isinstance(arg, ast.Starred) for arg in node.args)+ or any(kw.arg is None for kw in node.keywords)+ )+ ):+ return True# A concrete write callable handed as an argument to any call escapes into a helper that can invoke it# without a direct open()/writer site: the same bypass the map/starmap/reduce branches gate, but# through a user-defined helper. A benign callable argument (run(len)) is unaffected.@@ -6671,6 +6757,11 @@ def _python_is_potentially_unsafe(code: str) -> bool:_passed_write_callable(kw.value) for kw in node.keywords):return True+ # A numpy loader handed to a helper can be called there with allow_pickle positionally.+ if any(_allow_pickle_position(a) is not None for a in node.args) or any(+ _allow_pickle_position(kw.value) is not None for kw in node.keywords+ ):+ return Trueif isinstance(func, ast.Name):if func.id in dynamic_aliases:return True # call through a getattr alias is dynamicdiff --git a/studio/backend/tests/test_permission_mode.py b/studio/backend/tests/test_permission_mode.pyindex 57d40b7af7a..09abe53a0dd 100644--- a/studio/backend/tests/test_permission_mode.py+++ b/studio/backend/tests/test_permission_mode.py@@ -2316,6 +2316,74 @@ def test_python_classifier(code, unsafe):assert is_potentially_unsafe_tool_call("python", {"code": code}) is unsafe+ "code, unsafe",+ [+ ("import numpy as np\nnp.load('a.npy')", False),+ ("import numpy as np\nnp.load('a.npy', allow_pickle=False)", False),+ ("import numpy as np\nnp.load('a.npy', allow_pickle=True)", True),+ ("import numpy as np\nflag = True\nnp.load('a.npy', allow_pickle=flag)", True),+ ("from numpy import load\nload('a.npz', allow_pickle=1)['x']", True),+ ("import numpy as np\nnp.load('a.npy', None, True)", True),+ ("import numpy as np\nnp.load('a.npy', None, False)", False),+ ("import numpy as np\nnp.load('a.npy', **{'allow_pickle': True})", True),+ ("import numpy as np\nargs = ('a.npy', None, True)\nnp.load(*args)", True),+ ("import json\njson.load(open('a.json'))", False),+ ("from numpy import load as read\nread('a.npy', None, True)", True),+ ("import numpy as np\nloader = np.load\nloader('a.npy', None, True)", True),+ ("import numpy as np\nloader = np.load\nloader(*('a.npy', None, True))", True),+ ("import numpy as np\nloader = np.load\nloader('a.npy')", False),+ ("import numpy as np\nbox.reader = np.load\nbox.reader('a.npy', None, True)", True),+ ("from numpy import *\nload('a.npy', None, True)", True),+ ("import json\nargs = (open('a.json'),)\njson.load(*args)", False),+ ("obj.load(*args)", False),+ ("import numpy\nnp = numpy\nnp.load('a.npy', None, True)", True),+ (+ "import numpy as np\ndef read(loader=np.load):\n return loader('a.npy', None, True)\nread()",+ True,+ ),+ ("import numpy as np\ndef read(f):\n return f('a.npy', None, True)\nread(np.load)", True),+ ("import numpy as np\nloader, _ = (np.load, None)\nloader('a.npy', None, True)", True),+ ("from numpy.lib._npyio_impl import load\nload('a.npy', None, True)", True),+ ("import numpy.lib.npyio as io\nio.load('a.npy', None, True)", True),+ ("from numpy.lib import _npyio_impl as io\nio.load('a.npy', None, True)", True),+ ("from numpy.lib.format import read_array\nread_array(open('a.npy', 'rb'), True)", True),+ ("import numpy as np\nnp.lib.format.read_array(open('a.npy', 'rb'))", False),+ ("import numpy.lib.npyio\nnumpy.lib.npyio.load('a.npy', None, True)", True),+ ("import numpy as np\nnp.lib.format.read_array(open('a.npy', 'rb'), True)", True),+ ("def read_array(path, dtype):\n return []\nread_array('x', 'float32')", False),+ (+ "from numpy.lib.format import read_array\nreader = read_array\nreader(open('a.npy', 'rb'), True)",+ True,+ ),+ ("import numpy as np\nnp.lib.npyio.NpzFile('a.npz', False, True)['x']", True),+ ("from numpy.lib.npyio import NpzFile\nNpzFile('a.npz')['x']", False),+ (+ "import numpy as np\nfmt = np.lib.format\nfmt.read_array(open('a.npy', 'rb'), True)",+ True,+ ),+ ("import numpy as np, json\n(np.load if x else json.load)('a.npy', None, True)", True),+ ("import numpy as np\n(loader or np.load)('a.npy', None, True)", True),+ ("import json\n(json.load if x else json.loads)(f)", False),+ ("import numpy as np\nnp.read_array = np.load\nnp.lib.format.read_array(f, True)", True),+ ("import numpy as np\n(np.load if c else np.lib.format.read_array)(f, True)", True),+ ("import numpy as np\n(loader := np.load)('a.npy', None, True)", True),+ (+ "import numpy as np\nbox.reader, _ = (np.load, None)\nbox.reader('a.npy', None, True)",+ True,+ ),+ ("import numpy as np\nnp.load('a.npy', 'r')", False),+ ("import numpy as np\nnp.load('a.npy', None, 'yes')", True),+ ("import numpy as np\nnp.lib.format.read_array(f, 'yes')", True),+ ("import numpy as np\nz = np.load('a.npz')\nz.allow_pickle = True\nz['x']", True),+ ("import numpy as np\nz = np.load('a.npz')\nz['x']", False),+ ("import numpy as np\nnp.load('a.npy', None, False)", False),+ ],+)+def test_python_classifier_numpy_allow_pickle(code, unsafe):+ assert is_potentially_unsafe_tool_call("python", {"code": code}) is unsafe++def test_builtin_readonly_tools_are_safe():assert is_potentially_unsafe_tool_call("web_search", {"query": "hi"}) is Falseassert is_potentially_unsafe_tool_call("search_knowledge_base", {}) is Falsediff --git a/studio/frontend/src/components/assistant-ui/markdown-text.tsx b/studio/frontend/src/components/assistant-ui/markdown-text.tsxindex 3a618656bdb..9a657bef0eb 100644--- a/studio/frontend/src/components/assistant-ui/markdown-text.tsx+++ b/studio/frontend/src/components/assistant-ui/markdown-text.tsx@@ -1083,7 +1083,8 @@ const StreamdownBlock = memo((props: BlockProps) => (</MarkdownBlockBoundary>));StreamdownBlock.displayName = "StreamdownBlock";-const AUDIO_PLAYER_RE = /<audio-player\s+src="([^"]+)"\s*\/>/;+// Only the adapter's inline wav: any other src (remote URL, WebKit-followed audio/mpegurl) fetches on render.+const AUDIO_PLAYER_RE = /<audio-player\s+src="(data:audio\/wav;base64,[A-Za-z0-9+/=]+)"\s*\/>/;// Coalesce only token events that arrive before the browser's next paint, as// textgen does. There is no time or length throttle. Incremental block parsingdiff --git a/studio/frontend/src/lib/latex.ts b/studio/frontend/src/lib/latex.tsindex 8c26975f67c..e4eea83f29f 100644--- a/studio/frontend/src/lib/latex.ts+++ b/studio/frontend/src/lib/latex.ts@@ -37,6 +37,9 @@ const BLOCK_BREAK_RE =// non-ASCII letters and CJK punctuation are prose (KaTeX rejects them in math mode).const VARIABLE_PROSE_RE =/^(?!\w+\s+$)(?:[A-Za-z]{2,}\w*|_\w+|\{[A-Za-z_]\w*\})[\w\s.,;:!?'"()/`|&<>=*\-\p{L}\p{M}\u3000-\u303f\uff00-\uff65]*(?:[\s/:,.;|<>=\-\u3000-\u303f\uff00-\uff65]|[^\P{L}\p{ASCII}]|[\s(]["'(`])$/u;+// VARIABLE_PROSE_RE backtracks ~cubically: longer spans, or any past the per-message budget, stay math.+const MAX_VARIABLE_PROSE_SPAN = 128;+const VARIABLE_PROSE_BUDGET = 2048;const NEW_TOKEN_RE = /[\w{\\]/;const isAsciiLetter = (c: number) => (c | 32) >= 97 && (c | 32) <= 122;const isWordChar = (c: number) =>@@ -698,6 +701,7 @@ export function preprocessLaTeX(content: string, isStreaming = false): string {if (!text.includes("$")) return text;+ let proseBudget = VARIABLE_PROSE_BUDGET;const codeRegions = findCodeBlockRegions(text);const linkRegions = mergeRegions(findLinkDestinationRegions(text),@@ -760,8 +764,10 @@ export function preprocessLaTeX(content: string, isStreaming = false): string {(next + 1 === text.length? isStreaming: NEW_TOKEN_RE.test(text[next + 1])) &&- VARIABLE_PROSE_RE.test(text.slice(offset + 1, next)) &&- !inRawText(offset)+ next - offset - 1 <= MAX_VARIABLE_PROSE_SPAN &&+ !inRawText(offset) &&+ (proseBudget -= next - offset - 1) >= 0 &&+ VARIABLE_PROSE_RE.test(text.slice(offset + 1, next))) {return VARIABLE_DOLLAR;}diff --git a/studio/frontend/tests/audio-player-tag-source.test.ts b/studio/frontend/tests/audio-player-tag-source.test.tsnew file mode 100644index 00000000000..1a02b7e084b--- /dev/null+++ b/studio/frontend/tests/audio-player-tag-source.test.ts@@ -0,0 +1,31 @@+// 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 { readSrc } from "./helpers/kit.ts";++const MARKDOWN_TEXT = readSrc("components/assistant-ui/markdown-text.tsx");++function audioPlayerRe(): RegExp {+ const literal = MARKDOWN_TEXT.match(/const AUDIO_PLAYER_RE = \/(.+)\/;/);+ assert.ok(literal, "AUDIO_PLAYER_RE not found");+ return new RegExp(literal[1]);+}++test("the inline wav the chat adapter writes still becomes a player", () => {+ const match = '<audio-player src="data:audio/wav;base64,UklGRg==" />'.match(audioPlayerRe());+ assert.equal(match?.[1], "data:audio/wav;base64,UklGRg==");+});++test("a reply cannot make the player fetch a remote or same-origin url", () => {+ for (const src of [+ "https://attacker.example/x.wav",+ "//attacker.example/x.wav",+ "/api/health",+ "blob:x",+ "data:audio/mpegurl,%23EXTM3U%0Ahttps://attacker.example/x.mp3",+ ]) {+ assert.equal(`<audio-player src="${src}" />`.match(audioPlayerRe()), null, src);+ }+});diff --git a/studio/frontend/tests/latex-variable-prose-span-cap.test.ts b/studio/frontend/tests/latex-variable-prose-span-cap.test.tsnew file mode 100644index 00000000000..25fe4935aa0--- /dev/null+++ b/studio/frontend/tests/latex-variable-prose-span-cap.test.ts@@ -0,0 +1,28 @@+// 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 { preprocessLaTeX } from "../src/lib/latex.ts";++test("a long dollar span does not freeze the variable-prose check", () => {+ const started = performance.now();+ preprocessLaTeX(`$${"A".repeat(2000)}$x`);+ assert.ok(performance.now() - started < 1000, "variable-prose regex backtracked on a long span");+});++test("many long dollar spans in one reply stay within the per-message budget", () => {+ const started = performance.now();+ preprocessLaTeX(`$${"A".repeat(256)}$x `.repeat(100) + `$${"A".repeat(128)}$x `.repeat(1000));+ assert.ok(performance.now() - started < 1000, "variable-prose regex work is not bounded per message");+});++test("short variable spans and math are unchanged", () => {+ assert.equal(preprocessLaTeX("Set $HOME/bin before $PATH and run it."), "Set $HOME/bin before $PATH and run it.");+ assert.equal(preprocessLaTeX("Energy $E = mc^2$ holds."), "Energy $E = mc^2$ holds.");+});++test("dollar spans inside raw-text elements do not use up the budget", () => {+ const style = `<style>${`$${"A".repeat(127)}$x `.repeat(70)}</style>\n\n`;+ assert.ok(preprocessLaTeX(`${style}Set $HOME/bin before $PATH and run it.`).endsWith("Set $HOME/bin before $PATH and run it."));+});