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: isolate the Windows Terminal on MXC's default tier (#12328)

Etherlb1c522a4ead4
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 26ba72a57ff..45d397e4f06 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -9477,9 +9477,9 @@ def _build_safe_env(workdir: str, shell: "str | None" = None) -> dict[str, str]:
Whitelist-built from scratch (parent env NOT inherited): only
PATH/HOME/TMPDIR/LANG/TERM/PYTHONIOENCODING/PYTHONPATH (+VIRTUAL_ENV or Windows SystemRoot and a
minimal PATHEXT) reach the child; all credential vars (HF_TOKEN, AWS_*, etc.) are absent. HOME
- points at the sandbox workdir so SDKs can't read the operator's cached creds, and the temp vars
- at _sandbox_temp_dir just inside it. PYTHONPATH carries only the sandbox sitecustomize shim
- directory.
+ (and on Windows HOMEDRIVE/HOMEPATH) points at the sandbox workdir so SDKs can't read the
+ operator's cached creds, and the temp vars at _sandbox_temp_dir just inside it. PYTHONPATH
+ carries only the sandbox sitecustomize shim directory.
PATH starts with the Unsloth interpreter / venv and OS system dirs so ``python``/``pip`` stay
pinned. On Windows only, Git-for-Windows install dirs from the host PATH are appended so bare
@@ -9551,6 +9551,8 @@ def _build_safe_env(workdir: str, shell: "str | None" = None) -> dict[str, str]:
# and writes outside the workdir.
env["TEMP"] = temp_dir
env["TMP"] = temp_dir
+ # Path.home() ignores HOME on Windows; a workdir USERPROFILE would instead send pip's cache to .\pip in the cwd.
+ env["HOMEDRIVE"], env["HOMEPATH"] = os.path.splitdrive(workdir)
# Restrict PATHEXT so cwd .BAT/.CMD cannot hijack bare names (#7317).
pathext = ".EXE;.COM"
if git_ext and git_ext not in (".EXE", ".COM"):
@@ -10178,12 +10180,10 @@ def _terminal_profile(disable_sandbox: bool = False) -> str:
if disable_sandbox or os.environ.get("UNSLOTH_MXC_TERMINAL_CMD") == "0":
return host_default
try:
- from . import mxc_policy, mxc_probe
+ from . import mxc_probe
if bash:
- # Measured only on MXC's DACL tier; BaseContainer hosts keep bash until it is.
- if not mxc_policy.dacl_fallback_enabled():
- return "bash"
+ # Either MXC tier: BaseContainer hosts hit the same MSYS failure as the DACL tier.
verdict = os_sandbox.capability_snapshot(
execution_kind = "terminal", selected_executable = bash
)
diff --git a/studio/backend/tests/test_sandbox_tools.py b/studio/backend/tests/test_sandbox_tools.py
index 48984566d55..12a011d57f4 100644
--- a/studio/backend/tests/test_sandbox_tools.py
+++ b/studio/backend/tests/test_sandbox_tools.py
@@ -2480,6 +2480,8 @@ class TestSandboxEnvIsolation:
"NoDefaultCurrentDirectoryInExePath", # Windows only; no cwd-first lookup
"TEMP", # Windows only; native programs honour these, not TMPDIR
"TMP",
+ "HOMEDRIVE", # Windows only; Path.home() at the workdir, never inherited
+ "HOMEPATH",
}
extras = set(env.keys()) - allowed
assert not extras, f"sandbox env added unexpected keys: {extras}"
diff --git a/studio/backend/tests/test_windows_cmd_terminal_profile.py b/studio/backend/tests/test_windows_cmd_terminal_profile.py
index 1b8ceeda9f8..7eddbee2083 100644
--- a/studio/backend/tests/test_windows_cmd_terminal_profile.py
+++ b/studio/backend/tests/test_windows_cmd_terminal_profile.py
@@ -6,7 +6,9 @@
Faked platform throughout, since studio-backend-ci is Linux-only; the native tests cover a real host.
"""
+import ntpath
import os
+import subprocess
import sys
from pathlib import Path
@@ -155,6 +157,32 @@ def test_default_env_is_unchanged(windows, monkeypatch, tmp_path):
assert not any(key.startswith("GIT_") for key in env)
[email protected]("shell", [None, "cmd_isolated"])
+def test_safe_env_homes_windows_python_in_the_workdir(windows, monkeypatch, tmp_path, shell):
+ windows()
+ _userland(monkeypatch, tmp_path)
+ monkeypatch.setenv("USERPROFILE", r"C:\Users\someone")
+ env = tools._build_safe_env(str(tmp_path), shell = shell)
+ assert ntpath.join(env["HOMEDRIVE"], env["HOMEPATH"]) == str(tmp_path)
+ assert "USERPROFILE" not in env and "APPDATA" not in env and "LOCALAPPDATA" not in env
+
+
[email protected](sys.platform != "win32", reason = "Windows home resolution")
+def test_a_sandboxed_windows_child_finds_its_home_and_an_absolute_pip_cache(tmp_path):
+ probe = (
+ "import pathlib, os\n"
+ "from pip._vendor import platformdirs\n"
+ "print(pathlib.Path.home())\n"
+ "print(os.path.isabs(platformdirs.user_cache_dir('pip', appauthor = False)))\n"
+ )
+ env = tools._build_safe_env(str(tmp_path))
+ out = subprocess.run(
+ [sys.executable, "-c", probe], env = env, cwd = tmp_path, capture_output = True, text = True
+ )
+ assert out.returncode == 0, out.stderr
+ assert out.stdout.splitlines() == [str(tmp_path), "True"]
+
+
def test_blocklist_still_catches_blocked_commands_under_cmd(windows):
windows() # bash on the host: the lexer must follow the explicit dialect, not the host shell
for command in (
@@ -222,9 +250,17 @@ def test_bash_profile_keeps_multiline_and_bash_argv(windows, monkeypatch, tmp_pa
assert "GIT_CONFIG_COUNT" not in plan.env
-def test_bash_hosts_keep_bash_outside_the_measured_dacl_tier(windows):
- windows(bash_cap = _cap(False, MSYS), cmd_cap = _cap(True), dacl = False)
[email protected]("dacl", [False, True])
+def test_the_msys_verdict_moves_the_terminal_to_cmd_on_either_mxc_tier(windows, dacl):
+ windows(bash_cap = _cap(False, MSYS), cmd_cap = _cap(True), dacl = dacl)
+ assert tools._terminal_profile() == "cmd_isolated"
+
+
[email protected]("dacl", [False, True])
+def test_bash_that_isolates_stays_bash_on_either_mxc_tier(windows, dacl):
+ calls = windows(bash_cap = _cap(True), cmd_cap = _cap(True), dacl = dacl)
assert tools._terminal_profile() == "bash"
+ assert calls == [BASH]
def test_cmd_isolated_strips_a_trailing_newline(windows, monkeypatch, tmp_path):