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: let an approved rm run, never a recursive one (#14238)
Daniel Han28a222f20c80
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex 6b3b59cc7ad..de39d24bcee 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -252,6 +252,40 @@ _BLOCKED_COMMANDS = (else _BLOCKED_COMMANDS_COMMON)+# An approved call may run `rm`, but only as one plain command naming its files, never recursively. An allowlist, not a+# recursive-flag scan: globs and xargs can feed rm a file named `-rf`, a line continuation splits a flag, `./rm` may be+# another program, so any shell syntax keeps the block.+_APPROVED_RM_SHELL_CHARS = frozenset(";&|<>()$`\\{}*?[]!#%^\n\r")+# No -i / -I / --interactive: stdin is /dev/null, so rm reads "no", deletes nothing and still exits 0.+_APPROVED_RM_SHORT_OPTION_RE = re.compile(r"-[fvd]+")+_APPROVED_RM_LONG_OPTIONS = frozenset({"--force", "--verbose", "--dir", "--interactive=never"})+_APPROVED_RM_REFUSED = (+ "Blocked command(s) for safety: rm. Approval allows only one plain rm naming each file, "+ "e.g. rm notes.txt old.log. Recursive deletion (-r, -R, -rf, --recursive) is never allowed, "+ "nor are globs, pipes, xargs, chained commands or $ expansion."+)+++def _is_approvable_rm(command: str) -> bool:+ # A trailing newline only ends the command; one inside starts another.+ command = command.strip()+ if _APPROVED_RM_SHELL_CHARS & set(command):+ return False+ try:+ words = shlex.split(command)+ except ValueError:+ return False+ if len(words) < 2 or words[0] != "rm":+ return False+ for i, word in enumerate(words[1:], 1):+ if word == "--":+ return i + 1 < len(words)+ if word.startswith("-") and not (+ _APPROVED_RM_SHORT_OPTION_RE.fullmatch(word) or word in _APPROVED_RM_LONG_OPTIONS+ ):+ return False+ return any(not w.startswith("-") for w in words[1:])+_SHELL_SEPARATORS = frozenset({";", "&&", "||", "|", "&", "\n", "(", ")", "`", "{", "}"})# Bash keywords starting a new command position. `if`/`while`/`until` are followed by a CONDITION the shell executes,@@ -23762,6 +23796,11 @@ def _bash_exec()else:blocked = _find_blocked_commands(command)+ if host_access_approved and blocked == {"rm"}:+ # The allowlist reads words the POSIX way; cmd quotes differently and deletes with `del`, not rm.+ if profile in _CMD_PROFILES or not _is_approvable_rm(command):+ return _APPROVED_RM_REFUSED+ blocked = set()if blocked:# Capped for the same reason the Python analyzer's error is: it lists what it found in the command it was# handed.diff --git a/studio/backend/tests/test_approved_rm.py b/studio/backend/tests/test_approved_rm.pynew file mode 100644index 00000000000..d38accc7135--- /dev/null+++ b/studio/backend/tests/test_approved_rm.py@@ -0,0 +1,126 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""An `rm` the user approved in the prompt runs; a recursive one never does (#9972)."""++from __future__ import annotations++import os+import sys++import pytest++from core.inference import tools++_SESSION = "__LOCALID_approved_rm"++posix_only = pytest.mark.skipif(sys.platform == "win32", reason = "terminal runs bash here")+++ "command",+ [+ "rm notes.txt",+ "rm -f notes.txt",+ "rm -v a.txt b.txt",+ "rm -fv --interactive=never my-report.txt",+ "rm 'my file.txt'",+ "rm -- -weird.txt",+ "rm ~/notes.txt",+ "rm notes.txt\n",+ " rm notes.txt ",+ ],+)+def test_plain_rm_is_approvable(command):+ assert tools._find_blocked_commands(command) == {"rm"}+ assert tools._is_approvable_rm(command)+++ "command",+ [+ "rm -rf build",+ "rm -fr build",+ "rm -Rf build",+ "rm -r build",+ "rm -R build",+ "rm -v -r build",+ "rm --recursive build",+ "rm --rec build",+ "rm '-rf' build",+ 'rm "-""r"f build',+ "rm -\\rf build",+ "rm -\\\nrf build",+ "rm -{r,f} build",+ "rm {-rf,build}",+ "rm -?f build",+ "rm *",+ "rm *.log",+ "ls | xargs rm",+ "./rm build -delete",+ "PATH=. rm build",+ "rm() { find build -delete; }; rm",+ "bash -c 'rm -rf build'",+ "find . -name x -exec rm -rf {} +",+ "X=-rf; rm $X build",+ "rm $(echo -rf) build",+ "rm `echo -rf` build",+ "rm a.txt && rm -rf build",+ "rm -f",+ "rm --",+ "rm notes.txt\nrm -rf build",+ "rm -i notes.txt",+ "rm -fI a b c d",+ "rm --interactive notes.txt",+ "rm --interactive=once a b c d",+ ],+)+def test_anything_but_a_plain_rm_is_not_approvable(command):+ assert "rm" in tools._find_blocked_commands(command)+ assert not tools._is_approvable_rm(command)+++@posix_only+def test_approved_rm_deletes_and_unapproved_is_blocked():+ workdir = tools._get_workdir(_SESSION)+ target = os.path.join(workdir, "notes.txt")+ open(target, "w").close()++ out = tools._bash_exec("rm notes.txt", None, 60, _SESSION)+ assert out == "Blocked command(s) for safety: rm"+ assert os.path.exists(target)++ tools._bash_exec("rm notes.txt", None, 60, _SESSION, host_access_approved = True)+ assert not os.path.exists(target)++ open(target, "w").close()+ tools._bash_exec("rm notes.txt\n", None, 60, _SESSION, host_access_approved = True)+ assert not os.path.exists(target)+++@posix_only+ "command",+ ["rm -rf build", "rm -r build", "rm --recursive build", "rm *", "ls | xargs rm"],+)+def test_approved_recursive_rm_is_refused(command):+ workdir = tools._get_workdir(_SESSION)+ tree = os.path.join(workdir, "build")+ os.makedirs(os.path.join(tree, "sub"), exist_ok = True)+ open(os.path.join(tree, "sub", "f.txt"), "w").close()+ # A file named like the flag, which a glob or xargs would hand rm as `-rf`.+ open(os.path.join(workdir, "-rf"), "w").close()++ out = tools._bash_exec(command, None, 60, _SESSION, host_access_approved = True)+ assert out == tools._APPROVED_RM_REFUSED+ assert os.path.exists(os.path.join(tree, "sub", "f.txt"))+++@posix_only+ "command,expected",+ [("sudo true", "sudo"), ("rm notes.txt && sudo true", "rm, sudo")],+)+def test_approval_does_not_lift_other_blocked_commands(command, expected):+ out = tools._bash_exec(command, None, 60, _SESSION, host_access_approved = True)+ assert out == f"Blocked command(s) for safety: {expected}"diff --git a/studio/backend/tests/test_windows_cmd_terminal_profile.py b/studio/backend/tests/test_windows_cmd_terminal_profile.pyindex 40e66a6935e..747fa2f2752 100644--- a/studio/backend/tests/test_windows_cmd_terminal_profile.py+++ b/studio/backend/tests/test_windows_cmd_terminal_profile.py@@ -432,3 +432,10 @@ def test_host_launches_keep_cmd_quoting(monkeypatch):assert mxc_policy.host_spawn_args(["bash", "-c", "ls"]) == ["bash", "-c", "ls"]multiline = ["cmd", "/c", "echo a\necho b"]assert mxc_policy.host_spawn_args(multiline) is multiline+++def test_cmd_profile_keeps_an_approved_rm_blocked(windows, monkeypatch, tmp_path):+ windows(bash_cap = _cap(False, MSYS), cmd_cap = _cap(True))+ plan, result = _exec(monkeypatch, tmp_path, "rm notes.txt", host_access_approved = True)+ assert plan is None+ assert result == tools._APPROVED_RM_REFUSED