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
"""The terminal tool runs bash on Windows: cmd /c runs only the first line of a multi-line command."""
import os
import sys
from pathlib import Path
import pytest
_BACKEND_ROOT = Path(__file__).resolve().parents[1]
if str(_BACKEND_ROOT) not in sys.path:
sys.path.insert(0, str(_BACKEND_ROOT))
from core.inference import tools
@pytest.fixture(autouse = True)
def _clear_bash_cache():
# Bound first: a monkeypatched _windows_bash has no cache_clear for teardown.
cached = tools._windows_bash
cached.cache_clear()
yield
cached.cache_clear()
def _fake_trusted_root(monkeypatch, root):
"""Fake _windows_program_roots: the resolver never reads %ProgramFiles%, as callers could
relocate it."""
monkeypatch.setattr(tools, "_windows_program_roots", lambda: [str(root)])
def test_posix_shell_is_unchanged(monkeypatch):
monkeypatch.setattr(sys, "platform", "linux")
assert tools._get_shell_cmd("echo hi") == ["bash", "-c", "echo hi"]
def test_windows_uses_bash_when_present(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: r"C:\Program Files\Git\bin\bash.exe")
assert tools._get_shell_cmd("echo hi") == [
r"C:\Program Files\Git\bin\bash.exe",
"-c",
"echo hi",
]
def test_windows_falls_back_to_cmd_without_bash(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: None)
assert tools._get_shell_cmd("echo hi") == ["cmd", "/c", "echo hi"]
def test_prefers_git_for_windows_over_path(monkeypatch, tmp_path):
git_bash = tmp_path / "Git" / "bin" / "bash.exe"
git_bash.parent.mkdir(parents = True)
git_bash.write_text("")
_fake_trusted_root(monkeypatch, tmp_path)
monkeypatch.setattr(os, "environ", {})
monkeypatch.setattr(tools.shutil, "which", lambda _name: r"C:\somewhere\else\bash.exe")
assert tools._windows_bash() == str(git_bash)
def test_untrusted_path_bash_is_rejected(monkeypatch, tmp_path):
# A bash in a user-writable dir is attacker-controlled; cmd stays trusted.
scoop_bash = tmp_path / "scoop" / "shims" / "bash.exe"
scoop_bash.parent.mkdir(parents = True)
scoop_bash.write_text("")
_fake_trusted_root(monkeypatch, tmp_path / "Program Files")
monkeypatch.setattr(os, "environ", {"PATH": str(scoop_bash.parent)})
monkeypatch.setattr(tools.shutil, "which", lambda _name: str(scoop_bash))
assert tools._windows_bash() is None
def test_trusted_path_bash_behind_an_untrusted_shim_is_found(monkeypatch, tmp_path):
shim = tmp_path / "shims" / "bash.exe"
shim.parent.mkdir(parents = True)
shim.write_text("")
trusted_dir = tmp_path / "Program Files" / "Git" / "bin"
trusted_dir.mkdir(parents = True)
(trusted_dir / "bash.exe").write_text("")
_fake_trusted_root(monkeypatch, tmp_path / "Program Files")
monkeypatch.setattr(
os, "environ", {"PATH": os.pathsep.join([str(shim.parent), str(trusted_dir)])}
)
monkeypatch.setattr(tools.shutil, "which", lambda _name: str(shim))
assert tools._windows_bash() == str(trusted_dir / "bash.exe")
@pytest.mark.parametrize(
"wsl_path",
[
r"C:\Windows\System32\bash.exe",
r"C:\Users\me\AppData\Local\Microsoft\WindowsApps\bash.exe",
"C:/Windows/System32/bash.exe",
],
)
def test_wsl_launcher_is_rejected(monkeypatch, wsl_path):
# WSL bash runs in another filesystem, so the sandbox workdir would not apply.
# The dir is trusted here so the WSL marker alone must reject these.
monkeypatch.setattr(tools, "_is_trusted_windows_program_dir", lambda _dir: True)
assert tools._is_trusted_windows_bash(wsl_path) is False
def test_windowsapps_under_program_files_is_still_rejected(monkeypatch, tmp_path):
store_bash = tmp_path / "WindowsApps" / "bash.exe"
store_bash.parent.mkdir(parents = True)
store_bash.write_text("")
_fake_trusted_root(monkeypatch, tmp_path)
monkeypatch.setattr(os, "environ", {})
monkeypatch.setattr(tools.shutil, "which", lambda _name: str(store_bash))
assert tools._windows_bash() is None
def test_no_bash_anywhere_returns_none(monkeypatch):
monkeypatch.setattr(tools, "_windows_program_roots", lambda: [])
monkeypatch.setattr(os, "environ", {})
monkeypatch.setattr(tools.shutil, "which", lambda _name: None)
assert tools._windows_bash() is None
def test_blocklist_lexes_bash_syntax_when_the_shell_is_bash(monkeypatch):
# The non-posix lexer never splits on `;`, so the scan must follow the shell.
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: r"C:\Program Files\Git\bin\bash.exe")
assert "rm" in tools._find_blocked_commands("if true; then rm -rf x; fi")
assert "curl" in tools._find_blocked_commands("for i in 1; do curl http://x; done")
@pytest.mark.parametrize("command", ["'rm' -rf x", '"rm" -rf x', "echo hi; 'rm' -rf x"])
def test_blocklist_sees_through_quoting_when_the_shell_is_bash(monkeypatch, command):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: r"C:\Program Files\Git\bin\bash.exe")
assert "rm" in tools._find_blocked_commands(command)
def test_blocklist_still_catches_the_plain_form_under_cmd(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: None)
assert "rm" in tools._find_blocked_commands("rm -rf x")
@pytest.mark.skipif(sys.platform != "win32", reason = "Windows shell behaviour")
def test_multiline_script_runs_every_line_on_windows():
if tools._windows_bash() is None:
pytest.skip("no native Win32 bash on this host")
script = "\n".join(
[
"value=unsloth",
"for i in 1 2 3; do",
' echo "line $i $value"',
"done",
]
)
out = tools._bash_exec(script)
for expected in ("line 1 unsloth", "line 2 unsloth", "line 3 unsloth"):
assert expected in out, out
def test_paths_note_names_the_real_platform():
note = tools._SANDBOX_PATHS_NOTE
if sys.platform == "win32":
assert "Windows" in note
assert "/mnt/data" not in note
assert "/tmp/outputs" not in note
else:
assert "/mnt/data" in note
def test_shell_note_names_the_shell_that_will_run(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: r"C:\Program Files\Git\bin\bash.exe")
assert "The shell is bash" in tools._build_terminal_shell_note()
monkeypatch.setattr(tools, "_windows_bash", lambda: None)
cmd_note = tools._build_terminal_shell_note()
assert "cmd, not bash" in cmd_note
assert "one command per call" in cmd_note
def test_posix_shell_note_is_empty(monkeypatch):
monkeypatch.setattr(sys, "platform", "linux")
assert tools._build_terminal_shell_note() == ""
@pytest.mark.parametrize("bash", [r"C:\Program Files\Git\bin\bash.exe", None])
def test_notes_never_recommend_a_blocked_program(monkeypatch, bash):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: bash)
text = (tools._build_sandbox_paths_note() + tools._build_terminal_shell_note()).lower()
for program in tools._BLOCKED_COMMANDS_WIN:
assert program not in text
assert "start-process" not in text
assert "/c start" not in text
def test_terminal_tool_description_is_the_two_notes_appended():
description = tools.TERMINAL_TOOL["function"]["description"]
assert description == (
"Execute a terminal command and return stdout/stderr."
+ tools._SANDBOX_PATHS_NOTE
+ tools._TERMINAL_SHELL_NOTE
)
@pytest.mark.parametrize("bash", [r"C:\Program Files\Git\bin\bash.exe", None])
def test_windows_shell_note_is_never_empty(monkeypatch, bash):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: bash)
note = tools._build_terminal_shell_note()
assert note.startswith(" The shell is ")
assert note not in tools._build_sandbox_paths_note()
def test_python_tool_description_omits_the_shell():
description = tools.PYTHON_TOOL["function"]["description"]
assert tools._SANDBOX_PATHS_NOTE in description
assert "shell" not in description.lower()
if sys.platform == "win32":
assert tools._TERMINAL_SHELL_NOTE not in description
def test_notes_say_where_commands_run(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: r"C:\Program Files\Git\bin\bash.exe")
assert "user's own machine" in tools._build_sandbox_paths_note()
assert "opens a window on the user's desktop" in tools._build_terminal_shell_note()
@pytest.fixture
def _windows_blocklist(monkeypatch):
# _BLOCKED_COMMANDS resolves at import, so fake the resolved set instead.
monkeypatch.setattr(
tools,
"_BLOCKED_COMMANDS",
tools._BLOCKED_COMMANDS_COMMON | tools._BLOCKED_COMMANDS_WIN,
)
@pytest.mark.parametrize(
"command",
[
"cmd /c powershell -Command ls",
"cmd //c powershell -Command ls",
"cmd //k powershell -Command ls",
'cmd //c start "" powershell -Command ls',
'cmd //c start /b "" pwsh -Command ls',
'cmd //c start //min "" powershell -Command ls',
r'cmd //c start /d C:/tmp "" powershell -Command ls',
],
)
def test_cmd_shellout_is_screened_through_mangled_switches(command, _windows_blocklist):
# Git Bash turns a lone /c into a path, so models write //c.
assert tools._find_blocked_commands(command)
@pytest.mark.parametrize(
"command",
[
'cmd //c start "" bash -c "bash /c/x.sh"',
"cmd //c start wt",
"cmd //c dir",
"start notepad",
],
)
def test_detached_windows_stay_launchable(command, _windows_blocklist):
# Faked blocklist, or the Linux set has no powershell and a blanket block is invisible.
assert not tools._find_blocked_commands(command)
# Run each command through both lexers, since Git Bash presence picks the lexer.
_WINDOWS_SHELLS = [r"C:\Program Files\Git\bin\bash.exe", None]
def _screen_on_windows(monkeypatch, bash, command):
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(tools, "_windows_bash", lambda: bash)
return tools._find_blocked_commands(command)
@pytest.mark.parametrize("bash", _WINDOWS_SHELLS)
@pytest.mark.parametrize(
"command",
[
'cmd /c "powershell -Command ls"',
'start "My Title" powershell -Command ls',
'cmd //c start "my window" /min powershell',
"cmd /v:on /c powershell -Command ls",
"cmd //v:on //c powershell -Command ls",
# MSYS rewrites a POSIX path, so the program arrives with a leading slash.
'cmd //c start "" /c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command ls',
'start "" powershell -Command ls',
'cmd //c env start "" powershell',
],
)
def test_windows_shellouts_are_screened_on_either_shell(
monkeypatch, bash, command, _windows_blocklist
):
assert _screen_on_windows(monkeypatch, bash, command)
@pytest.mark.parametrize("bash", _WINDOWS_SHELLS)
@pytest.mark.parametrize(
"command",
[
'start "" notepad readme.txt',
'start "Build" npm run build',
'cmd /c "echo hello"',
r'echo "C:\Windows\System32\cmd.exe"',
r'cmd //c start "" "C:\Users\me\My Documents\report.docx"',
"npm start",
"./start.sh",
"echo start notepad powershell",
"grep start README powershell",
],
)
def test_ordinary_windows_commands_stay_runnable(monkeypatch, bash, command, _windows_blocklist):
assert not _screen_on_windows(monkeypatch, bash, command)
@pytest.mark.parametrize(
"command",
[
'cmd //c start "job" powershell -Command ls',
'cmd /c start "t" pwsh -c ls',
],
)
def test_a_single_word_start_title_is_screened_under_the_cmd_lexer(
monkeypatch, command, _windows_blocklist
):
assert _screen_on_windows(monkeypatch, None, command)
@pytest.mark.parametrize("bash", _WINDOWS_SHELLS)
@pytest.mark.parametrize(
"command",
[
'FOO=1 bash -c "rm -rf x"',
'env FOO=1 bash -c "rm -rf x"',
'FOO=1 start "" powershell',
'cmd //c start "" cmd /c powershell',
'cmd //c env -u FOO start "" powershell',
'cmd //c nice -n 5 start "" powershell',
],
)
def test_a_prefixed_shell_is_still_screened(monkeypatch, bash, command, _windows_blocklist):
if bash is None and not command.startswith("cmd "):
pytest.skip("assignment prefixes are not cmd syntax")
assert _screen_on_windows(monkeypatch, bash, command)
def test_a_program_path_is_not_read_as_a_cmd_switch(monkeypatch, _windows_blocklist):
assert not tools._CMD_SWITCH_RE.fullmatch("/bin/bash")
assert _screen_on_windows(monkeypatch, _WINDOWS_SHELLS[0], '/bin/bash -c "rm -rf x"') == {"rm"}
@pytest.fixture
def _windows_backstop(monkeypatch, _windows_blocklist):
# The command-position backstop is compiled from _BLOCKED_COMMANDS at import as well.
monkeypatch.setattr(tools, "_BLOCKED_WORD_RE", tools._blocked_word_re(False))
monkeypatch.setattr(tools, "_BLOCKED_WORD_RE_WITH_ASSIGNMENTS", tools._blocked_word_re(True))
@pytest.mark.parametrize("bash", _WINDOWS_SHELLS)
@pytest.mark.parametrize(
"command",
[
# #12054: each was refused as "Blocked command(s) for safety: .".
r"C:\flutter\bin\flutter.bat create C:\src\my_app",
"C:/flutter/bin/flutter.bat create my_app",
r"C:\Python312\python.exe -m pip list",
"c:/tools/node.exe -v",
r"cd C:\src && D:\sdk\dart.exe --version",
r"C:\Users\me\.venv\Scripts\python.exe -m pip list",
r"C:\Users\me\.cargo\bin\cargo.exe build",
".venv/bin/python -m pip list",
"ls; /home/me/.local/bin/tool",
],
)
def test_an_absolute_windows_program_path_stays_runnable(
monkeypatch, bash, command, _windows_backstop
):
assert not _screen_on_windows(monkeypatch, bash, command)
@pytest.mark.parametrize("bash", _WINDOWS_SHELLS)
@pytest.mark.parametrize(
("command", "expected"),
[
(r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -c ls", "powershell"),
("C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe", "powershell"),
(r"C:\pwsh.exe -c ls", "pwsh"),
(r"echo hi; C:\tools\rm.exe -rf x", "rm"),
],
)
def test_a_blocked_program_behind_a_drive_path_is_still_screened(
monkeypatch, bash, command, expected, _windows_backstop
):
assert expected in _screen_on_windows(monkeypatch, bash, command)
@pytest.mark.parametrize("command", ["echo hi; . script.sh", "x=$(. script.sh)", ". ./env.sh"])
def test_dot_as_a_command_is_still_screened(monkeypatch, command, _windows_backstop):
assert "." in _screen_on_windows(monkeypatch, _WINDOWS_SHELLS[0], command)
@pytest.mark.parametrize("bash", _WINDOWS_SHELLS)
@pytest.mark.parametrize(
("command", "expected"),
[
# A quoted program path with a space, handed on by cmd or START: re-lexing it as a command line
# split it at the space and screened `C:\Program` instead.
(r'cmd /c "C:\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
(r'cmd //c "C:\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
(r'start "" "C:\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
(r'cmd //c start "" "C:\x y\powershell.exe"', "powershell"),
('cmd /c "C:/x y/rm.exe" -rf z', "rm"),
# START's program slot holds only a program, so a relative path counts there too.
(r'start "" "tools dir\pwsh.exe" -c ls', "pwsh"),
(r'cmd /c "%SystemDrive%\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
# START resolves an extensionless program through PATHEXT.
(r'start "" "C:\Program Files\PowerShell\7\pwsh" -c ls', "pwsh"),
(r'cmd /c "C:\Program Files\PowerShell\7\pwsh" -c ls', "pwsh"),
],
)
def test_a_quoted_program_path_handed_to_cmd_or_start_is_screened(
monkeypatch, bash, command, expected, _windows_blocklist
):
assert expected in _screen_on_windows(monkeypatch, bash, command)
def test_cmd_strips_the_outer_quotes_of_a_doubled_quote_payload(monkeypatch, _windows_blocklist):
# cmd strips outer quotes; Git Bash would strip backslashes first, so only cmd runs pwsh.
assert "pwsh" in _screen_on_windows(monkeypatch, None, r'cmd /c ""C:\x\pwsh.exe" -c ls"')
@pytest.mark.parametrize("bash", _WINDOWS_SHELLS)
@pytest.mark.parametrize(
"command",
[
r'cmd /c "C:\Program Files\nodejs\node.exe" -v',
r'start "" "C:\Program Files\Microsoft VS Code\Code.exe" .',
# A blocked name that is only an argument, a folder or a document.
r'cmd /c "dir C:\src\curl"',
r'cmd /c "git -C C:\src\ssh status"',
r'start "" "tools dir\notepad.exe" readme.txt',
# A rooted command line whose last argument names a blocked program.
r'cmd /c "\Windows\System32\where.exe \src\rm"',
r'cmd /c type "C:\Users\me\My Documents\rm.txt"',
],
)
def test_a_quoted_program_path_that_is_not_blocked_stays_runnable(
monkeypatch, bash, command, _windows_blocklist
):
assert not _screen_on_windows(monkeypatch, bash, command)
def test_a_quoted_start_title_is_not_screened_as_the_program(monkeypatch, _windows_blocklist):
# cmd keeps the quotes, so START reads this as the title and runs notepad. Git Bash hands START the
# path unquoted, which it then runs, so that reading stays blocked.
assert not _screen_on_windows(monkeypatch, None, r'start "C:\x\rm.exe" notepad')
assert "rm" in _screen_on_windows(
monkeypatch, _WINDOWS_SHELLS[0], r'start "C:\x\rm.exe" notepad'
)