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: make the OS sandbox work in Colab and other containers (#12801)

Daniel Han813525e3f023
diff --git a/studio/backend/core/inference/os_sandbox.py b/studio/backend/core/inference/os_sandbox.py
index caa5ccf5c29..4ca318e431e 100644
--- a/studio/backend/core/inference/os_sandbox.py
+++ b/studio/backend/core/inference/os_sandbox.py
@@ -833,6 +833,10 @@ def capability_snapshot(
selected_executable = selected_executable,
cancel_event = cancel_event,
)
+ if force and sys.platform == "linux":
+ # A forced re-check (Settings > Refresh) also re-decides the /proc layout, which joins the identity.
+ from .sandbox_linux import forget_proc_layout
+ forget_proc_layout()
identity = _runtime_identity()
if sys.platform == "linux":
from . import sandbox_linux
@@ -863,8 +867,9 @@ def capability_snapshot(
reason = reason,
environment = sys.platform,
protection_state = "preview",
- profile_id = backend.PROFILE_ID,
- limitations = backend.LIMITATIONS,
+ # The Linux layout can vary per host (an empty /proc in containers); macOS has one profile.
+ profile_id = getattr(backend, "profile_id", lambda: backend.PROFILE_ID)(),
+ limitations = getattr(backend, "limitations", lambda: backend.LIMITATIONS)(),
probe_generation = hashlib.sha256((identity + "available").encode()).hexdigest(),
environment_fingerprint = identity,
remediation = (
diff --git a/studio/backend/core/inference/sandbox_linux.py b/studio/backend/core/inference/sandbox_linux.py
index 20e3c03d3ce..f63e4a9164c 100644
--- a/studio/backend/core/inference/sandbox_linux.py
+++ b/studio/backend/core/inference/sandbox_linux.py
@@ -8,6 +8,7 @@ from __future__ import annotations
import fnmatch
import glob
import os
+import re
import shutil
import stat
import site
@@ -120,7 +121,10 @@ def bwrap_identity() -> str:
"""Stable identity included in capability-cache keys."""
path = _trusted_bwrap_path()
info = os.stat(path, follow_symlinks = False)
- return repr((path, info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_mode))
+ layout = "emptyproc" if empty_proc_layout(path) else "proc"
+ return repr(
+ (path, info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_mode, layout)
+ )
# /etc is fresh in the jail; without these nothing dynamically linked starts.
@@ -332,13 +336,126 @@ def _bwrap_long_options(identity: tuple[str, int, int]) -> frozenset[str]:
)
-def _bwrap_supports(bwrap: str, option: str) -> bool:
+def _bwrap_file_identity(bwrap: str) -> tuple[str, int, int]:
try:
info = os.stat(bwrap)
- identity = (bwrap, info.st_ino, info.st_mtime_ns)
+ return (bwrap, info.st_ino, info.st_mtime_ns)
except OSError:
- identity = (bwrap, 0, 0)
- return option in _bwrap_long_options(identity)
+ return (bwrap, 0, 0)
+
+
+def _bwrap_supports(bwrap: str, option: str) -> bool:
+ return option in _bwrap_long_options(_bwrap_file_identity(bwrap))
+
+
+# A container masking parts of its own /proc (Docker, Colab) refuses a fresh one in a child namespace.
+# bwrap <= 0.11 says "on /newroot/proc", 0.12 says "on /proc" (openai/codex#44329).
+_PROC_MOUNT_REFUSED = re.compile(
+ r"Can't mount proc on (?:/newroot)?/proc: "
+ r"(?:Permission denied|Operation not permitted|Invalid argument)"
+)
+
+
+def proc_mount_refused(stderr: str) -> bool:
+ return bool(_PROC_MOUNT_REFUSED.search(stderr or ""))
+
+
+def _preflight(bwrap: str, proc: tuple[str, ...]) -> "subprocess.CompletedProcess | None":
+ """The launch's namespaces, /proc and system binds in its order, running only `true`."""
+ # NixOS has no FHS `true`: run the store file itself, with the store bound as launches bind it.
+ found = next(
+ (
+ p
+ for p in ("/usr/bin/true", "/bin/true", "/run/current-system/sw/bin/true")
+ if os.path.isfile(p)
+ ),
+ None,
+ )
+ if found is None:
+ return None
+ true = os.path.realpath(found)
+ argv = [bwrap, "--unshare-user", "--unshare-pid", *proc, "--dev", "/dev"]
+ roots = _SYSTEM_ROOTS + ((_NIX_STORE,) if _within(true, _NIX_STORE) else ())
+ for root in roots:
+ if os.path.isdir(root):
+ argv += ["--ro-bind-try", root, root]
+ try:
+ return subprocess.run(
+ [*argv, "--", true],
+ stdin = subprocess.DEVNULL,
+ stdout = subprocess.DEVNULL,
+ stderr = subprocess.PIPE,
+ # The detector matches English strerror text.
+ env = {**os.environ, "LC_ALL": "C", "LANG": "C", "LANGUAGE": ""},
+ text = True,
+ encoding = "utf-8",
+ errors = "replace",
+ timeout = 10,
+ close_fds = True,
+ )
+ except (OSError, subprocess.SubprocessError):
+ return None
+
+
+class _PreflightInconclusive(Exception):
+ """The preflight could not run or failed before /proc: not cached, retried after a back-off."""
+
+
+# A wedged bwrap times out at 10 s a run: retry it this often, not on every identity read.
+_INCONCLUSIVE_RETRY_SECONDS = 300.0
+_inconclusive_until: "dict[tuple[str, int, int], float]" = {}
+
+
+@lru_cache(maxsize = 8)
+def _fresh_proc_answer(identity: tuple[str, int, int]) -> bool:
+ fresh = _preflight(identity[0], ("--proc", "/proc"))
+ if fresh is None:
+ raise _PreflightInconclusive
+ if fresh.returncode == 0:
+ return False
+ if not proc_mount_refused(fresh.stderr):
+ # bwrap failed before /proc (e.g. a uid map AppArmor still denies): the layout is unknown until that is fixed.
+ raise _PreflightInconclusive
+ without = _preflight(identity[0], ())
+ if without is None:
+ raise _PreflightInconclusive
+ return without.returncode == 0
+
+
+def _fresh_proc_refused(identity: tuple[str, int, int]) -> bool:
+ """Only the /proc mount fails while the rest of bwrap works; any other failure is the probe's to report."""
+ if time.monotonic() < _inconclusive_until.get(identity, 0.0):
+ return False
+ try:
+ return _fresh_proc_answer(identity)
+ except _PreflightInconclusive:
+ _inconclusive_until[identity] = time.monotonic() + _INCONCLUSIVE_RETRY_SECONDS
+ return False
+
+
+def empty_proc_layout(bwrap: str | None = None) -> bool:
+ """Whether launches mount an empty private /proc instead of a fresh procfs. Never the host's: it lists every cmdline."""
+ try:
+ path = bwrap or _trusted_bwrap_path()
+ except SandboxUnavailableError:
+ return False
+ return _fresh_proc_refused(_bwrap_file_identity(path))
+
+
+def forget_proc_layout() -> None:
+ _fresh_proc_answer.cache_clear()
+ _inconclusive_until.clear()
+
+
+def profile_id() -> str:
+ return f"{PROFILE_ID}-emptyproc" if empty_proc_layout() else PROFILE_ID
+
+
+def limitations() -> tuple[str, ...]:
+ # /dev/fd and /dev/std{in,out,err} link into /proc/self/fd, so they dangle too (no `<(...)`).
+ if empty_proc_layout():
+ return (*LIMITATIONS, "no_process_filesystem", "no_dev_fd_links")
+ return LIMITATIONS
def _host_mount_points() -> tuple[str, ...]:
@@ -524,8 +641,13 @@ def _inspect_cache_component(
return cache_share_hazard(path, witness)
+CACHE_MISSING = "does not exist"
+CACHE_STILL_CHECKING = "is still being checked; it will be shared once the check finishes"
+
# Retain timed-out workers until they finish, preventing a thread leak on a wedged path.
-_cache_scan_pending: "dict[str, threading.Thread]" = {}
+# path -> (worker, its answer list, give-up time): a later launch joins the same check instead of giving up,
+# but only until the give-up time, so a wedged mount costs two waits in total, not one per launch.
+_cache_scan_pending: "dict[str, tuple[threading.Thread, list, float]]" = {}
# Request threads must reserve and remove workers atomically.
_cache_scan_lock = threading.Lock()
@@ -561,25 +683,34 @@ def _cache_hazard_within_deadline(name: str, path: str) -> "str | None":
with _cache_scan_lock:
pending = _cache_scan_pending.get(path)
- if pending is not None:
- if pending.is_alive():
- return "was still being inspected when a previous launch gave up (a wedged mount?)"
+ if pending is not None and not pending[0].is_alive():
del _cache_scan_pending[path]
- worker = threading.Thread(target = check, name = f"unsloth-cache-scan-{name}", daemon = True)
- # Start under the lock, or another caller can replace the not-yet-alive worker.
- _cache_scan_pending[path] = worker
- worker.start()
- worker.join(_CACHE_INSPECT_SECONDS)
+ pending = None
+ if pending is None:
+ worker = threading.Thread(target = check, name = f"unsloth-cache-scan-{name}", daemon = True)
+ # Start under the lock, or another caller can replace the not-yet-alive worker.
+ give_up = time.monotonic() + 2 * _CACHE_INSPECT_SECONDS
+ pending = _cache_scan_pending[path] = (worker, answer, give_up)
+ worker.start()
+ worker, answer, give_up = pending
+ worker.join(max(0.0, min(_CACHE_INSPECT_SECONDS, give_up - time.monotonic())))
if not answer:
- return f"could not be inspected within {_CACHE_INSPECT_SECONDS:.0f}s (a wedged mount?)"
+ return CACHE_STILL_CHECKING
with _cache_scan_lock:
# By identity: another caller may already have replaced it.
- if _cache_scan_pending.get(path) is worker:
+ if _cache_scan_pending.get(path) is pending:
del _cache_scan_pending[path]
return answer[0]
def _cache_hazard_memoized(name: str, path: str) -> "str | None":
+ if not os.path.lexists(path):
+ # A fresh HF home has no datasets/ or assets/ yet; create it so the sandbox shares it too.
+ try:
+ os.makedirs(path, exist_ok = True)
+ except OSError as exc:
+ logger.debug("could not create the %s cache at %s: %s", name, path, exc)
+ return CACHE_MISSING
signature = _cache_component_signature(path)
now = time.monotonic()
with _cache_scan_lock:
@@ -601,6 +732,20 @@ def _cache_hazard_memoized(name: str, path: str) -> "str | None":
return verdict
+_CACHE_WARNING_SECONDS = 60.0
+_cache_warned: "dict[tuple[str, str], float]" = {}
+
+
+def _warn_not_shared(name: str, hazard: str) -> None:
+ now = time.monotonic()
+ with _cache_scan_lock:
+ last = _cache_warned.get((name, hazard))
+ if last is not None and now - last < _CACHE_WARNING_SECONDS:
+ return
+ _cache_warned[(name, hazard)] = now
+ logger.warning("Not sharing the %s cache into the sandbox: it %s", name, hazard)
+
+
def _model_cache_binds(workdir: str) -> dict[str, str]:
"""Inner cache subdirectory -> host directory, per component (HF_HUB_CACHE=/mnt/models is NOT /mnt/models/hub)."""
binds: dict[str, str] = {}
@@ -640,8 +785,10 @@ def _model_cache_binds(workdir: str) -> dict[str, str]:
continue
# Writable caches need the workdir's host-channel checks, including nested bind mounts.
hazard = _cache_hazard_within_deadline(name, path)
+ if hazard == CACHE_MISSING:
+ continue
if hazard is not None:
- logger.warning("Not sharing the %s cache into the sandbox: it %s", name, hazard)
+ _warn_not_shared(name, hazard)
continue
binds[name] = path
return binds
@@ -703,7 +850,11 @@ def prepare(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:
tmp_runtime_paths = tuple(path for path in runtime_paths if _within(path, "/tmp"))
workdir_runtime_paths = _runtime_paths_under(workdir)
- disable_userns = _bwrap_supports(bwrap, "--disable-userns")
+ # --disable-userns writes /proc/sys/user/max_user_namespaces inside, which an empty /proc lacks:
+ # there the seccomp filter refuses nested user namespaces instead, as on bwrap 0.6.1.
+ # One read per launch: a reset between two reads could mix --disable-userns with an empty /proc.
+ empty_proc = empty_proc_layout(bwrap)
+ disable_userns = not empty_proc and _bwrap_supports(bwrap, "--disable-userns")
try:
seccomp = sandbox_seccomp.filter_file(block_userns = not disable_userns)
except RuntimeError as exc:
@@ -730,8 +881,7 @@ def prepare(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:
"ALL",
"--seccomp",
str(seccomp.fileno()),
- "--proc",
- "/proc",
+ *(("--tmpfs", "/proc") if empty_proc else ("--proc", "/proc")),
"--dev",
"/dev",
"--dir",
diff --git a/studio/backend/core/inference/sandbox_probe.py b/studio/backend/core/inference/sandbox_probe.py
index c0e81c0c042..21bfe480520 100644
--- a/studio/backend/core/inference/sandbox_probe.py
+++ b/studio/backend/core/inference/sandbox_probe.py
@@ -56,6 +56,12 @@ def reset_probe_cache() -> None:
with _cache_lock:
_cache.clear()
_generation += 1
+ if sys.platform == "linux":
+ try:
+ from .sandbox_linux import forget_proc_layout
+ forget_proc_layout()
+ except Exception: # noqa: BLE001 - nothing cached to forget
+ pass
try:
from .os_sandbox import forget_tool_isolation
except Exception: # noqa: BLE001 - a partially imported package has nothing cached yet
diff --git a/studio/backend/core/inference/sandbox_setup_job.py b/studio/backend/core/inference/sandbox_setup_job.py
index 77b3abc7dc9..05353e933eb 100644
--- a/studio/backend/core/inference/sandbox_setup_job.py
+++ b/studio/backend/core/inference/sandbox_setup_job.py
@@ -117,7 +117,9 @@ def pkexec_script(steps) -> str:
return "set -e\n" + "\n".join(shlex.join(list(step)) for step in steps) + "\n"
-def _commands(plan: sandbox_setup_plan.SetupPlan) -> tuple[list[list[str]], dict | None]:
+def _commands(
+ plan: sandbox_setup_plan.SetupPlan, interactive: bool = True
+) -> tuple[list[list[str]], dict | None]:
"""(argv list, env). Linux steps run as root: programs pinned to system binaries, never PATH."""
if plan.action != sandbox_setup_plan.LINUX_INSTALL:
return [list(step) for step in plan.steps], None
@@ -126,6 +128,11 @@ def _commands(plan: sandbox_setup_plan.SetupPlan) -> tuple[list[list[str]], dict
raise SetupUnavailable(
"Neither passwordless sudo nor a desktop password prompt is available."
)
+ # A remote start was allowed because nothing prompts; a desktop prompt would appear where nobody is.
+ if not interactive and kind not in sandbox_setup_plan.PROMPTLESS_ELEVATION:
+ raise SetupUnavailable(
+ "This setup needs the password prompt on the computer running Unsloth; start it there."
+ )
try:
steps = sandbox_setup_plan.elevated_steps(plan.steps)
shell = sandbox_setup_plan.trusted_system_binary("sh") if kind == "pkexec" else None
@@ -225,8 +232,11 @@ def _joined(running: SetupJob, operation: str) -> SetupJob:
return running
-def start(operation: str) -> SetupJob:
- """Start the setup for `operation`, or return the run of the same operation already in progress."""
+def start(operation: str, interactive: bool = True) -> SetupJob:
+ """Start the setup for `operation`, or return the run of the same operation already in progress.
+
+ ``interactive=False`` (a remote start) refuses any step that would prompt on this computer.
+ """
global _current
from . import mxc_host_prep_job
@@ -241,7 +251,7 @@ def start(operation: str) -> SetupJob:
plan = sandbox_setup_plan.detect(force = True)
if plan.action != operation:
raise SetupUnavailable(plan.reason or "There is nothing to set up on this computer.")
- commands, env = _commands(plan)
+ commands, env = _commands(plan, interactive)
with HOST_CHANGE_LOCK:
with _lock:
if _current is not None and _current.state == "running":
diff --git a/studio/backend/core/inference/sandbox_setup_plan.py b/studio/backend/core/inference/sandbox_setup_plan.py
index 4b59aaea1a8..34d948f6868 100644
--- a/studio/backend/core/inference/sandbox_setup_plan.py
+++ b/studio/backend/core/inference/sandbox_setup_plan.py
@@ -478,7 +478,7 @@ def setup_fields_for(
*,
available: bool | None = None,
) -> dict:
- """Setup fields for a capability response: the action only for the owner on a local request."""
+ """Setup fields for a capability response: the action only for the owner, here or without a prompt."""
from utils.client_ip import is_direct_local_request
plan = detect(available)
@@ -504,13 +504,34 @@ def setup_fields_for(
}
+# Nothing to answer on this computer, so a remote owner session (e.g. behind Colab's proxy) may start it.
+PROMPTLESS_ELEVATION = ("root", "sudo")
+
+
def can_run_here(plan: SetupPlan, *, owner: bool, local: bool) -> bool:
"""Whether this caller gets the setup button. Blocking: may run the Linux elevation check."""
- if not (plan.action and owner and local):
+ if not (plan.action and owner):
return False
if plan.action == LINUX_INSTALL:
- return linux_elevation()[0] is not None
- return True
+ return linux_install_allowed(local = local)[0]
+ return local
+
+
+def linux_install_allowed(*, local: bool, force: bool = False) -> tuple[bool, str | None]:
+ """(allowed, elevation kind): any elevation for a direct local request, otherwise only one with no prompt."""
+ kind = linux_elevation(force = force)[0]
+ return kind is not None and (local or kind in PROMPTLESS_ELEVATION), kind
+
+
+def remote_start_allowed(operation: str) -> bool:
+ """Whether a request that is not direct-local may start `operation`: only where no prompt appears here."""
+ if operation == WINDOWS_RUNTIME:
+ return True
+ return (
+ operation == LINUX_INSTALL
+ and sys.platform.startswith("linux")
+ and linux_install_allowed(local = False, force = True)[0]
+ )
def _is_owner(user) -> bool:
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index 832b82fa027..444a5d6ac46 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -4800,7 +4800,7 @@ def _sandbox_setup_status(available: bool) -> Optional[SandboxSetupStatus]:
def _for_request(status: SandboxStatusResponse, request: Request) -> SandboxStatusResponse:
- """Blocking. Only a direct local request may be offered the setup button."""
+ """Blocking. The setup button: a direct local request, or a Linux install that prompts nobody here."""
from core.inference import sandbox_setup_plan
from utils.client_ip import is_direct_local_request
@@ -4809,9 +4809,9 @@ def _for_request(status: SandboxStatusResponse, request: Request) -> SandboxStat
return status
local = bool(setup.action) and is_direct_local_request(request)
update: dict = {"can_run": False}
- if local and setup.action == sandbox_setup_plan.LINUX_INSTALL:
- elevation, _path = sandbox_setup_plan.linux_elevation()
- update = {"can_run": elevation is not None, "elevation": elevation}
+ if setup.action == sandbox_setup_plan.LINUX_INSTALL:
+ can_run, elevation = sandbox_setup_plan.linux_install_allowed(local = local)
+ update = {"can_run": can_run, "elevation": elevation}
elif local:
update = {"can_run": True}
return status.model_copy(update = {"setup": setup.model_copy(update = update)})
@@ -5006,7 +5006,8 @@ async def start_sandbox_setup(
) -> SandboxSetupJob:
"""Install or prepare the OS sandbox here; the password or administrator prompt appears on this computer.
- The Windows runtime-only install needs no prompt, so unlike the rest it also works from a remote browser.
+ Steps that need no prompt (the Windows runtime-only install, a Linux install as root or with
+ passwordless sudo) also work from a remote browser.
"""
import sys
@@ -5015,9 +5016,9 @@ async def start_sandbox_setup(
from utils.client_ip import is_direct_local_request
# Stricter than client_ip(): a loopback peer carrying proxy headers is a remote browser relayed here.
- # The runtime-only install has no prompt (it is setup.ps1's unelevated step), so it works remotely.
- if payload.operation != sandbox_setup_plan.WINDOWS_RUNTIME and not is_direct_local_request(
- request
+ local = is_direct_local_request(request)
+ if not local and not await asyncio.to_thread(
+ sandbox_setup_plan.remote_start_allowed, payload.operation
):
raise HTTPException(
status_code = 403,
@@ -5052,7 +5053,7 @@ async def start_sandbox_setup(
)
sandbox_setup_job.add_finish_hook(_forget_sandbox_status)
try:
- job = await asyncio.to_thread(sandbox_setup_job.start, payload.operation)
+ job = await asyncio.to_thread(sandbox_setup_job.start, payload.operation, interactive = local)
except sandbox_setup_job.SetupUnavailable as exc:
raise HTTPException(status_code = 409, detail = str(exc)) from exc
if consent:
diff --git a/studio/backend/tests/test_sandbox_linux.py b/studio/backend/tests/test_sandbox_linux.py
index 7cb341859c0..c96e79e94a0 100644
--- a/studio/backend/tests/test_sandbox_linux.py
+++ b/studio/backend/tests/test_sandbox_linux.py
@@ -12,6 +12,7 @@ import inspect
import json
import os
import platform
+import shlex
import shutil
import socket
import stat
@@ -1212,6 +1213,37 @@ def test_a_wedged_cache_path_is_not_re_scanned_by_every_later_launch(tmp_path, m
assert len(started) == first, "a second launch started another worker on the same path"
+def test_a_wedged_cache_scan_is_waited_on_twice_then_skipped_at_once(monkeypatch, tmp_path):
+ component = tmp_path / "hub"
+ component.mkdir()
+ release = threading.Event()
+ monkeypatch.setattr(
+ sandbox_linux, "_cache_hazard_memoized", lambda name, path: release.wait(30)
+ )
+ monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.4)
+ monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})
+ # A fake clock that a join advances by its timeout: the waits are read off the joins, not timed.
+ now = [100.0]
+ joins = []
+ real_thread = threading.Thread
+
+ class Worker(real_thread):
+ def join(self, timeout = None):
+ joins.append(round(timeout, 6))
+ now[0] += timeout
+ super().join(0)
+
+ monkeypatch.setattr(sandbox_linux.threading, "Thread", Worker)
+ monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: now[0])
+ try:
+ for _ in range(4):
+ hazard = sandbox_linux._cache_hazard_within_deadline("hub", str(component))
+ assert hazard == sandbox_linux.CACHE_STILL_CHECKING
+ finally:
+ release.set()
+ assert joins == [0.4, 0.4, 0.0, 0.0], f"later launches still wait on the wedged scan: {joins}"
+
+
def test_revalidating_a_cached_verdict_on_a_wedged_mount_is_bounded_too(monkeypatch, tmp_path):
"""A memo hit re-stats every directory the walk saw, which blocks on a stalled NFS/FUSE cache just like the walk."""
component = tmp_path / "hub"
@@ -1232,7 +1264,7 @@ def test_revalidating_a_cached_verdict_on_a_wedged_mount_is_bounded_too(monkeypa
release.set()
assert time.monotonic() - start < 10
- assert hazard is not None and "wedged" in hazard, hazard
+ assert hazard == sandbox_linux.CACHE_STILL_CHECKING, hazard
def test_a_runtime_entry_whose_target_leaves_the_workdir_gets_no_rule(tmp_path, monkeypatch):
@@ -1434,7 +1466,7 @@ def test_a_hazardous_cache_drops_the_component_rather_than_failing_the_launch(
workdir.mkdir()
launch = sandbox_linux.prepare(_plan(workdir))
try:
- assert "HF_HOME" not in launch.argv
+ assert os.path.realpath(host / "hub") not in launch.argv
finally:
launch.cleanup()
@@ -2415,3 +2447,388 @@ def test_studio_state_inside_a_runtime_path_is_carved_out(monkeypatch, tmp_path)
assert str(state) not in kept
assert str(lib / "python3.12") in kept
assert str(managed) in kept, "the managed venv inside the Studio home must stay readable"
+
+
+ "stderr,refused",
+ [
+ ("bwrap: Can't mount proc on /newroot/proc: Permission denied", True), # bwrap 0.9 (Colab)
+ ("bwrap: Can't mount proc on /proc: Operation not permitted", True), # bwrap 0.12
+ ("bwrap: Can't mount proc on /newroot/proc: Invalid argument", True),
+ ("bwrap: setting up uid map: Permission denied", False),
+ ("bwrap: Can't mount tmpfs on /newroot/tmp: Permission denied", False),
+ ("bwrap: Can't mount proc on /newroot/proc: No such file or directory", False),
+ ("", False),
+ ],
+)
+def test_only_a_refused_proc_mount_reads_as_a_masked_container(stderr, refused):
+ assert sandbox_linux.proc_mount_refused(stderr) is refused
+
+
+def _fake_bwrap(
+ tmp_path,
+ *,
+ fresh_proc,
+ without_proc = 0,
+):
+ """A bwrap whose fresh /proc fails with `fresh_proc` (stderr) and whose other runs exit `without_proc`."""
+ record = tmp_path / "bwrap_calls"
+ fake = tmp_path / "bwrap"
+ fake.write_text(
+ "#!/bin/sh\n"
+ f'echo "$*" >> "{record}"\n'
+ 'for a in "$@"; do if [ "$a" = "--proc" ]; then\n'
+ + (f" echo {shlex.quote(fresh_proc)} >&2; exit 1\n" if fresh_proc else " exit 0\n")
+ + "fi; done\n"
+ + f"exit {without_proc}\n"
+ )
+ fake.chmod(0o755)
+ sandbox_linux.forget_proc_layout()
+ return str(fake), record
+
+
+def _no_proc_layout_left_behind():
+ sandbox_linux.forget_proc_layout()
+ yield
+ sandbox_linux.forget_proc_layout()
+
+
+def test_a_container_refusing_a_fresh_proc_gets_an_empty_one(tmp_path, _no_proc_layout_left_behind):
+ fake, record = _fake_bwrap(
+ tmp_path, fresh_proc = "bwrap: Can't mount proc on /newroot/proc: Permission denied"
+ )
+ assert sandbox_linux.empty_proc_layout(fake) is True
+ assert sandbox_linux.empty_proc_layout(fake) is True
+ runs = record.read_text().splitlines()
+ assert len(runs) == 2, "the preflight is cached per bwrap"
+ assert "--proc /proc" in runs[0] and "--proc" not in runs[1]
+
+
+def test_the_preflight_reads_bwrap_errors_in_the_c_locale(
+ tmp_path, monkeypatch, _no_proc_layout_left_behind
+):
+ # strerror follows the locale; the detector matches the English text.
+ monkeypatch.setenv("LC_ALL", "de_DE.UTF-8")
+ monkeypatch.setenv("LANG", "de_DE.UTF-8")
+ fake = tmp_path / "bwrap"
+ fake.write_text(
+ "#!/bin/sh\n"
+ 'for a in "$@"; do if [ "$a" = "--proc" ]; then\n'
+ ' if [ "$LC_ALL" = C ]; then echo "bwrap: Can\'t mount proc on /newroot/proc: Permission denied" >&2;\n'
+ ' else echo "bwrap: Can\'t mount proc on /newroot/proc: Keine Berechtigung" >&2; fi; exit 1\n'
+ "fi; done\nexit 0\n"
+ )
+ fake.chmod(0o755)
+ assert sandbox_linux.empty_proc_layout(str(fake)) is True
+
+
+ "fresh_proc,without_proc",
+ [
+ ("", 0), # a fresh procfs works: the normal layout
+ ("bwrap: setting up uid map: Permission denied", 1), # bwrap is broken for another reason
+ ("bwrap: Can't mount proc on /newroot/proc: No such file or directory", 0),
+ (
+ "bwrap: Can't mount proc on /proc: Permission denied",
+ 1,
+ ), # nothing works without /proc either
+ ],
+)
+def test_any_other_outcome_keeps_the_fresh_proc(
+ tmp_path, _no_proc_layout_left_behind, fresh_proc, without_proc
+):
+ fake, _record = _fake_bwrap(tmp_path, fresh_proc = fresh_proc, without_proc = without_proc)
+ assert sandbox_linux.empty_proc_layout(fake) is False
+
+
[email protected]("empty", [True, False])
+def test_the_launch_mounts_an_empty_private_proc_never_the_hosts(tmp_path, monkeypatch, empty):
+ if sandbox_linux.shutil.which("bwrap") is None:
+ pytest.skip("bubblewrap is not installed on this host")
+ monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a: empty)
+ launch = sandbox_linux.prepare(_plan(tmp_path))
+ try:
+ argv = launch.argv
+ assert ("/proc", "/proc") not in _pairs(argv, "--ro-bind")
+ assert ("/proc", "/proc") not in _pairs(argv, "--bind")
+ assert ("/proc", "/proc") not in _pairs(argv, "--ro-bind-try")
+ tmpfs = [argv[i + 1] for i, token in enumerate(argv) if token == "--tmpfs"]
+ procs = [argv[i + 1] for i, token in enumerate(argv) if token == "--proc"]
+ assert ("/proc" in tmpfs) is empty
+ assert procs == ([] if empty else ["/proc"])
+ finally:
+ launch.cleanup()
+
+
[email protected]("empty", [True, False])
+def test_an_empty_proc_blocks_nested_user_namespaces_with_seccomp(tmp_path, monkeypatch, empty):
+ # Colab: --disable-userns fails with "cannot open /proc/sys/user/max_user_namespaces".
+ if sandbox_linux.shutil.which("bwrap") is None:
+ pytest.skip("bubblewrap is not installed on this host")
+ monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a: empty)
+ monkeypatch.setattr(sandbox_linux, "_bwrap_supports", lambda _bwrap, _option: True)
+ built = []
+ real_filter = sandbox_linux.sandbox_seccomp.filter_file
+
+ def filter_file(**kwargs):
+ built.append(kwargs["block_userns"])
+ return real_filter(**kwargs)
+
+ monkeypatch.setattr(sandbox_linux.sandbox_seccomp, "filter_file", filter_file)
+ launch = sandbox_linux.prepare(_plan(tmp_path))
+ try:
+ assert ("--disable-userns" in launch.argv) is not empty
+ assert built == [empty]
+ finally:
+ launch.cleanup()
+
+
+def test_the_two_proc_layouts_never_share_a_profile_or_a_cached_verdict(monkeypatch):
+ if sandbox_linux.shutil.which("bwrap") is None:
+ pytest.skip("bubblewrap is not installed on this host")
+ seen = {}
+ for empty in (False, True):
+ monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a, e = empty: e)
+ seen[empty] = (
+ sandbox_linux.profile_id(),
+ sandbox_linux.limitations(),
+ sandbox_linux.bwrap_identity(),
+ )
+ assert seen[False][0] == sandbox_linux.PROFILE_ID != seen[True][0]
+ assert {"no_process_filesystem", "no_dev_fd_links"} <= set(seen[True][1])
+ assert not {"no_process_filesystem", "no_dev_fd_links"} & set(seen[False][1])
+ assert seen[False][2] != seen[True][2]
+
+
+def test_the_preflight_runs_a_nix_store_true_with_the_store_bound(monkeypatch):
+ nix_true = "/nix/store/abc-coreutils/bin/true"
+ real_isfile, real_isdir, real_realpath = os.path.isfile, os.path.isdir, os.path.realpath
+ monkeypatch.setattr(
+ os.path,
+ "isfile",
+ lambda p: p == "/run/current-system/sw/bin/true"
+ or (real_isfile(p) and p not in ("/usr/bin/true", "/bin/true")),
+ )
+ monkeypatch.setattr(os.path, "isdir", lambda p: p == sandbox_linux._NIX_STORE or real_isdir(p))
+ monkeypatch.setattr(
+ os.path,
+ "realpath",
+ lambda p, **kw: nix_true
+ if p == "/run/current-system/sw/bin/true"
+ else real_realpath(p, **kw),
+ )
+ seen = []
+ monkeypatch.setattr(
+ sandbox_linux.subprocess,
+ "run",
+ lambda argv, **kw: seen.append(argv) or subprocess.CompletedProcess(argv, 0, "", ""),
+ )
+ assert sandbox_linux._preflight("/usr/bin/bwrap", ("--proc", "/proc")) is not None
+ argv = seen[0]
+ assert argv[-1] == nix_true
+ assert ("--ro-bind-try", "/nix/store", "/nix/store") in zip(argv, argv[1:], argv[2:])
+
+
+def test_an_inconclusive_preflight_backs_off_then_retries(monkeypatch, _no_proc_layout_left_behind):
+ refused = subprocess.CompletedProcess(
+ [], 1, "", "bwrap: Can't mount proc on /newroot/proc: Permission denied"
+ )
+ answers = iter([None, refused, subprocess.CompletedProcess([], 0, "", "")])
+ runs = []
+ monkeypatch.setattr(
+ sandbox_linux, "_preflight", lambda bwrap, proc: runs.append(proc) or next(answers)
+ )
+ clock = [1000.0]
+ monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: clock[0])
+ identity = ("/usr/bin/bwrap", 1, 1)
+ assert sandbox_linux._fresh_proc_refused(identity) is False # timed out: no verdict kept
+ assert sandbox_linux._fresh_proc_refused(identity) is False
+ assert len(runs) == 1, "a wedged bwrap is not rerun on every read"
+ clock[0] += sandbox_linux._INCONCLUSIVE_RETRY_SECONDS + 1
+ assert sandbox_linux._fresh_proc_refused(identity) is True
+
+
+def test_a_failure_before_proc_is_retried_once_the_host_is_fixed(
+ monkeypatch, _no_proc_layout_left_behind
+):
+ """An AppArmor uid-map denial says nothing about /proc; repaired by hand, a masked /proc must still be found."""
+ uid_map = subprocess.CompletedProcess([], 1, "", "bwrap: setting up uid map: Permission denied")
+ refused = subprocess.CompletedProcess(
+ [], 1, "", "bwrap: Can't mount proc on /newroot/proc: Permission denied"
+ )
+ answers = iter([uid_map, refused, subprocess.CompletedProcess([], 0, "", "")])
+ monkeypatch.setattr(sandbox_linux, "_preflight", lambda bwrap, proc: next(answers))
+ clock = [1000.0]
+ monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: clock[0])
+ identity = ("/usr/bin/bwrap", 1, 1)
+ assert sandbox_linux._fresh_proc_refused(identity) is False
+ clock[0] += sandbox_linux._INCONCLUSIVE_RETRY_SECONDS + 1
+ assert sandbox_linux._fresh_proc_refused(identity) is True
+
+
+def test_a_reset_retries_an_inconclusive_preflight_at_once(
+ monkeypatch, _no_proc_layout_left_behind
+):
+ answers = iter([None, subprocess.CompletedProcess([], 0, "", "")])
+ monkeypatch.setattr(sandbox_linux, "_preflight", lambda bwrap, proc: next(answers))
+ identity = ("/usr/bin/bwrap", 1, 1)
+ assert sandbox_linux._fresh_proc_refused(identity) is False
+ sandbox_linux.forget_proc_layout()
+ assert sandbox_linux._fresh_proc_refused(identity) is False
+ with pytest.raises(StopIteration):
+ next(answers)
+
+
+def test_a_forced_capability_check_re_decides_the_proc_layout(monkeypatch):
+ if sys.platform != "linux":
+ pytest.skip("Linux layout only")
+ from core.inference import os_sandbox
+
+ class Forgot(Exception):
+ pass
+
+ def forget():
+ raise Forgot
+
+ monkeypatch.setattr(sandbox_linux, "forget_proc_layout", forget)
+ with pytest.raises(Forgot):
+ os_sandbox.capability_snapshot(force = True, execution_kind = "python")
+
+
+def test_one_launch_reads_the_proc_layout_once(tmp_path, monkeypatch):
+ if sandbox_linux.shutil.which("bwrap") is None:
+ pytest.skip("bubblewrap is not installed on this host")
+ answers = iter([True, False, False, False])
+ monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a: next(answers))
+ monkeypatch.setattr(sandbox_linux, "_bwrap_supports", lambda _bwrap, _option: True)
+ launch = sandbox_linux.prepare(_plan(tmp_path))
+ try:
+ argv = launch.argv
+ tmpfs = [argv[i + 1] for i, token in enumerate(argv) if token == "--tmpfs"]
+ assert "--disable-userns" not in argv and "/proc" in tmpfs and "--proc" not in argv
+ finally:
+ launch.cleanup()
+
+
+def test_resetting_the_probe_forgets_the_proc_layout(tmp_path, _no_proc_layout_left_behind):
+ from core.inference import sandbox_probe
+
+ fake, record = _fake_bwrap(
+ tmp_path, fresh_proc = "bwrap: Can't mount proc on /newroot/proc: Permission denied"
+ )
+ assert sandbox_linux.empty_proc_layout(fake) is True
+ sandbox_probe.reset_probe_cache()
+ assert sandbox_linux.empty_proc_layout(fake) is True
+ assert len(record.read_text().splitlines()) == 4, "the layout was not checked again"
+
+
+class _Warnings:
+ def __init__(self):
+ self.warnings = []
+
+ def warning(self, msg, *args, **_kw):
+ self.warnings.append(msg % args if args else msg)
+
+ def __getattr__(self, _name):
+ return lambda *_a, **_k: None
+
+
+def cache_log(monkeypatch):
+ log = _Warnings()
+ monkeypatch.setattr(sandbox_linux, "logger", log)
+ monkeypatch.setattr(sandbox_linux, "_cache_warned", {})
+ monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})
+ sandbox_linux.reset_cache_verdicts()
+ yield log
+ sandbox_linux.reset_cache_verdicts()
+
+
+def test_a_cache_folder_a_fresh_hf_home_lacks_is_created_and_shared(
+ tmp_path, monkeypatch, cache_log
+):
+ cache = tmp_path / "hostcache"
+ (cache / "hub").mkdir(parents = True)
+ _share_cache_paths(monkeypatch, cache)
+ binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
+ for name in ("hub", "datasets", "assets", "xet"):
+ assert name in binds and os.path.isdir(cache / name), name
+ assert cache_log.warnings == []
+
+
+def test_a_cache_folder_that_cannot_be_created_is_skipped_quietly(tmp_path, monkeypatch, cache_log):
+ cache = tmp_path / "hostcache"
+ (cache / "hub").mkdir(parents = True)
+ _share_cache_paths(monkeypatch, cache)
+ real = os.makedirs
+
+ def refuse(path, *a, **k):
+ if os.path.basename(path) == "datasets":
+ raise PermissionError(13, "Permission denied", path)
+ return real(path, *a, **k)
+
+ monkeypatch.setattr(sandbox_linux.os, "makedirs", refuse)
+ binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
+ assert "datasets" not in binds and "hub" in binds
+ assert cache_log.warnings == []
+
+
+def test_a_cache_entry_that_is_not_a_folder_still_warns(tmp_path, monkeypatch, cache_log):
+ cache = tmp_path / "hostcache"
+ (cache / "hub").mkdir(parents = True)
+ (cache / "datasets").write_text("not a folder")
+ _share_cache_paths(monkeypatch, cache)
+ binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
+ assert "datasets" not in binds
+ assert cache_log.warnings == [
+ "Not sharing the datasets cache into the sandbox: it is not a directory"
+ ]
+
+
+def test_the_same_cache_warning_is_logged_once_a_minute(tmp_path, monkeypatch, cache_log):
+ cache = tmp_path / "hostcache"
+ (cache / "hub").mkdir(parents = True)
+ (cache / "datasets").write_text("not a folder")
+ _share_cache_paths(monkeypatch, cache)
+ session = str(tmp_path / "session")
+ clock = [1000.0]
+ monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: clock[0])
+ sandbox_linux._model_cache_binds(session)
+ clock[0] += 30
+ sandbox_linux._model_cache_binds(session)
+ assert len(cache_log.warnings) == 1
+ clock[0] += sandbox_linux._CACHE_WARNING_SECONDS
+ sandbox_linux._model_cache_binds(session)
+ assert len(cache_log.warnings) == 2
+
+
+def test_a_launch_finding_a_check_in_progress_waits_for_its_answer(
+ tmp_path, monkeypatch, cache_log
+):
+ component = tmp_path / "hub"
+ component.mkdir()
+ started = threading.Event()
+ release = threading.Event()
+ walks = []
+
+ def slow(
+ name,
+ path,
+ witness = None,
+ ):
+ walks.append(path)
+ started.set()
+ release.wait(30)
+ return None
+
+ monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", slow)
+ monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.2)
+ assert sandbox_linux._cache_hazard_within_deadline("hub", str(component)) == (
+ sandbox_linux.CACHE_STILL_CHECKING
+ )
+ assert started.wait(5)
+ # Finishes inside the second launch's wait (the walk's budget is two waits).
+ threading.Timer(0.05, release.set).start()
+ assert sandbox_linux._cache_hazard_within_deadline("hub", str(component)) is None
+ assert walks == [str(component)]
diff --git a/studio/backend/tests/test_sandbox_probe.py b/studio/backend/tests/test_sandbox_probe.py
index 0c4f8b15c51..0898f4ea415 100644
--- a/studio/backend/tests/test_sandbox_probe.py
+++ b/studio/backend/tests/test_sandbox_probe.py
@@ -565,3 +565,29 @@ def test_an_unavailable_verdict_is_not_cached_for_the_long_window(monkeypatch, t
"the host was never re-examined, so installing bubblewrap does not "
"take effect until the long TTL expires"
)
+
+
+def test_the_capability_names_the_empty_proc_layout_it_was_probed_with(monkeypatch):
+ if sys.platform != "linux" or shutil.which("bwrap") is None:
+ pytest.skip("the /proc layout is a bubblewrap detail")
+ from core.inference import sandbox_linux
+
+ monkeypatch.setattr(sandbox_probe, "probe", lambda backend, force = False: (True, "probe passed"))
+ monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a: True)
+ capability = os_sandbox.capability_snapshot(force = True)
+ assert capability.available and capability.profile_id == f"{sandbox_linux.PROFILE_ID}-emptyproc"
+ assert "no_process_filesystem" in capability.limitations
+
+
+def test_the_startup_probe_scans_the_model_caches_before_any_tool_call(monkeypatch):
+ if sys.platform != "linux" or shutil.which("bwrap") is None:
+ pytest.skip("the cache scan is part of the bubblewrap launch")
+ from core.inference import sandbox_linux
+
+ scanned = []
+ monkeypatch.setattr(
+ sandbox_linux, "_model_cache_binds", lambda workdir: scanned.append(workdir) or {}
+ )
+ sandbox_probe.reset_probe_cache()
+ sandbox_probe.probe(sandbox_linux, force = True)
+ assert len(scanned) == 1
diff --git a/studio/backend/tests/test_sandbox_runtime_install.py b/studio/backend/tests/test_sandbox_runtime_install.py
index 3868e8f5b88..4b2a140bb84 100644
--- a/studio/backend/tests/test_sandbox_runtime_install.py
+++ b/studio/backend/tests/test_sandbox_runtime_install.py
@@ -189,7 +189,7 @@ def _client(account, via_api_key = False):
def route(x64_windows, monkeypatch):
started = []
- def start(operation):
+ def start(operation, interactive = True):
started.append(operation)
return job_mod.SetupJob(id = "rt1", operation = operation)
diff --git a/studio/backend/tests/test_sandbox_setup_job.py b/studio/backend/tests/test_sandbox_setup_job.py
index 7fa79789947..e7a051b465d 100644
--- a/studio/backend/tests/test_sandbox_setup_job.py
+++ b/studio/backend/tests/test_sandbox_setup_job.py
@@ -186,6 +186,22 @@ def test_no_polkit_agent_is_a_named_failure(monkeypatch, env):
assert job.state == "failed" and "authentication agent" in job.note
+def test_a_remote_start_never_raises_a_password_prompt_here(monkeypatch, env):
+ # The route allowed it for root or passwordless sudo; detection may have moved on to pkexec since.
+ pkexec = env["fake"]("pkexec")
+ _linux_plan(monkeypatch, "pkexec", pkexec)
+ with pytest.raises(job_mod.SetupUnavailable, match = "password prompt"):
+ job_mod.start(plan_mod.LINUX_INSTALL, interactive = False)
+ assert env["recorded"]() == [] and job_mod.current() is None
+
+
[email protected]("elevation", ["root", "sudo"])
+def test_a_remote_start_runs_when_nothing_prompts(monkeypatch, env, elevation):
+ _linux_plan(monkeypatch, elevation, env["fake"]("sudo") if elevation == "sudo" else None)
+ commands, _env = job_mod._commands(plan_mod.detect(), interactive = False)
+ assert len(commands) == len(_STEPS)
+
+
def test_no_elevation_means_no_job(monkeypatch, env):
plan = plan_mod.SetupPlan(platform = "linux", manual_command = "sudo apt-get install -y bubblewrap")
monkeypatch.setattr(plan_mod, "detect", lambda *a, **k: plan)
diff --git a/studio/backend/tests/test_sandbox_setup_plan.py b/studio/backend/tests/test_sandbox_setup_plan.py
index 1d89d35c93e..2cccac75eb9 100644
--- a/studio/backend/tests/test_sandbox_setup_plan.py
+++ b/studio/backend/tests/test_sandbox_setup_plan.py
@@ -247,15 +247,44 @@ def test_the_elevation_check_is_remembered_until_forced(linux):
assert linux["sudo_checks"] == 2
[email protected]("owner,local", [(False, True), (True, False), (False, False)])
-def test_nobody_but_the_owner_here_makes_unsloth_check_sudo(linux, monkeypatch, owner, local):
[email protected]("local", [True, False])
+def test_nobody_but_the_owner_makes_unsloth_check_sudo(linux, monkeypatch, local):
linux["tool"]("apt-get", "sudo")
monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: local)
- fields = plan_mod.setup_fields_for(object(), owner, available = False)
+ fields = plan_mod.setup_fields_for(object(), False, available = False)
assert linux["sudo_checks"] == 0
assert fields["setup_action"] is None and fields["manual_command"]
+ "elevation,offered",
+ [("root", True), ("sudo", True), ("pkexec", False), (None, False)],
+)
+def test_a_remote_owner_gets_the_button_only_when_nothing_prompts_here(
+ linux, monkeypatch, elevation, offered
+):
+ # Colab and other proxied hosts: the browser is never local, but root or passwordless sudo asks nobody.
+ linux["tool"]("apt-get", "sudo", "pkexec")
+ linux["root"] = elevation == "root"
+ linux["sudo_ok"] = elevation == "sudo"
+ if elevation == "pkexec":
+ monkeypatch.setenv("WAYLAND_DISPLAY", "wayland-0")
+ monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
+ assert plan_mod.linux_elevation(force = True)[0] == elevation
+ fields = plan_mod.setup_fields_for(object(), True, available = False)
+ assert fields["can_run_setup"] is offered
+ assert fields["setup_action"] == (plan_mod.LINUX_INSTALL if offered else None)
+ assert fields["setup_blocked"] == (None if offered else "not_local")
+ assert plan_mod.remote_start_allowed(plan_mod.LINUX_INSTALL) is offered
+ assert plan_mod.remote_start_allowed(plan_mod.WINDOWS_SETUP) is False
+
+
+def test_a_remote_linux_install_is_never_allowed_off_linux(linux, monkeypatch):
+ linux["tool"]("apt-get", "sudo")
+ monkeypatch.setattr(sys, "platform", "darwin")
+ assert plan_mod.remote_start_allowed(plan_mod.LINUX_INSTALL) is False
+
+
def test_elevated_steps_ignore_path_and_untrusted_folders(monkeypatch, tmp_path):
planted = tmp_path / "planted"
planted.mkdir()
@@ -433,6 +462,10 @@ def test_setup_fields_name_the_action_only_for_the_owner_here(linux, monkeypatch
other = plan_mod.setup_fields_for(object(), False, available = False)
assert other["setup_action"] is None and other["can_run_setup"] is False
assert other["manual_command"] == owner["manual_command"] != ""
+ linux["sudo_ok"] = False
+ linux["tool"]("pkexec")
+ monkeypatch.setenv("WAYLAND_DISPLAY", "wayland-0")
+ plan_mod.forget_elevation()
monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
remote = plan_mod.setup_fields_for(object(), True, available = False)
assert remote["setup_action"] is None and remote["manual_command"]
diff --git a/studio/backend/tests/test_sandbox_setup_route.py b/studio/backend/tests/test_sandbox_setup_route.py
index 9284734ead4..84f1b9cecea 100644
--- a/studio/backend/tests/test_sandbox_setup_route.py
+++ b/studio/backend/tests/test_sandbox_setup_route.py
@@ -34,6 +34,8 @@ from utils import client_ip, mxc_isolation_settings
from utils.account_context import OWNER, AccountContext, bind_account, reset_account
ALICE = AccountContext("a" * 32, "alice")
+_IS_DIRECT_LOCAL = client_ip.is_direct_local_request
+PKEXEC = ("pkexec", "/usr/bin/pkexec")
def _cap(available):
@@ -48,7 +50,7 @@ def _cap(available):
@pytest.fixture
def host(monkeypatch):
- calls = {"start": [], "revoke": 0}
+ calls = {"start": [], "revoke": 0, "interactive": []}
saved = {"dacl": False, "locked": False, "available": False}
plan = {
"value": sandbox_setup_plan.SetupPlan(
@@ -88,8 +90,9 @@ def host(monkeypatch):
mxc_isolation_settings, "locked_by_environment", lambda _name: saved["locked"]
)
- def start(operation):
+ def start(operation, interactive = True):
calls["start"].append(operation)
+ calls["interactive"].append(interactive)
return sandbox_setup_job.SetupJob(id = "job1", operation = operation)
monkeypatch.setattr(sandbox_setup_job, "start", start)
@@ -110,7 +113,11 @@ def windows(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
-def _client(account, via_api_key = False):
+def _client(
+ account,
+ via_api_key = False,
+ **client_kw,
+):
app = FastAPI()
app.include_router(settings.router)
@@ -123,7 +130,7 @@ def _client(account, via_api_key = False):
app.dependency_overrides[settings.get_current_subject] = subject
app.dependency_overrides[settings.authenticated_via_api_key] = lambda: via_api_key
- return TestClient(app, raise_server_exceptions = False)
+ return TestClient(app, raise_server_exceptions = False, **client_kw)
def test_owner_starts_the_linux_install_from_this_computer(host, linux):
@@ -134,7 +141,7 @@ def test_owner_starts_the_linux_install_from_this_computer(host, linux):
body = response.json()
assert body["state"] == "running" and body["operation"] == "linux-install"
assert client.get("/sandbox/setup").json()["state"] == "idle"
- assert calls["start"] == ["linux-install"]
+ assert calls["start"] == ["linux-install"] and calls["interactive"] == [True]
def test_status_names_the_setup_for_this_host(host, linux):
@@ -144,14 +151,27 @@ def test_status_names_the_setup_for_this_host(host, linux):
assert setup["manual_command"] == "sudo apt-get install -y bubblewrap"
-def test_status_offers_the_button_only_to_this_computer(host, linux, monkeypatch):
+def test_status_offers_a_prompting_setup_only_to_this_computer(host, linux, monkeypatch):
+ _calls, saved, _plan = host
+ saved["elevation"] = PKEXEC
with _client(OWNER) as client:
assert client.get("/sandbox").json()["setup"]["can_run"] is True
monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
+ settings._forget_sandbox_status()
setup = client.get("/sandbox").json()["setup"]
assert setup["can_run"] is False and setup["manual_command"]
[email protected]("elevation", [("root", None), ("sudo", "/usr/bin/sudo")])
+def test_status_offers_a_promptless_setup_to_a_remote_owner(host, linux, monkeypatch, elevation):
+ _calls, saved, _plan = host
+ saved["elevation"] = elevation
+ monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
+ with _client(OWNER) as client:
+ setup = client.get("/sandbox").json()["setup"]
+ assert setup["can_run"] is True and setup["elevation"] == elevation[0]
+
+
def _capability_client(account):
from routes.sandbox_capability import router as capability_router
@@ -183,16 +203,67 @@ def test_capability_reason_is_the_short_setup_reason_when_a_command_is_shown(hos
@pytest.mark.parametrize("who", ["other_account", "remote_owner"])
def test_capability_gives_everyone_else_only_the_command(host, linux, monkeypatch, who):
- calls, _saved, _plan = host
+ calls, saved, _plan = host
account = ALICE
if who == "remote_owner":
account = OWNER
+ saved["elevation"] = PKEXEC
monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
body = _capability_client(account).get("/api/sandbox/capability").json()
assert body["setup_action"] is None and body["can_run_setup"] is False
assert body["manual_command"] == "sudo apt-get install -y bubblewrap"
assert body["setup_blocked"] == ("not_owner" if who == "other_account" else "not_local")
- assert calls["elevation_checks"] == 0
+ if who == "other_account":
+ assert calls["elevation_checks"] == 0
+
+
+def _proxied_capability(account):
+ # The real check, behind a proxy as on Colab: loopback peer and Host, but a forwarding header.
+ from routes.sandbox_capability import router as capability_router
+
+ app = FastAPI()
+ app.include_router(capability_router, prefix = "/api/sandbox")
+
+ async def subject():
+ token = bind_account(account)
+ try:
+ yield account.username
+ finally:
+ reset_account(token)
+
+ app.dependency_overrides[settings.get_current_subject] = subject
+ client = TestClient(
+ app,
+ raise_server_exceptions = False,
+ base_url = "http://127.0.0.1:8888",
+ client = ("127.0.0.1", 50000),
+ )
+ return client.get("/api/sandbox/capability", headers = {"X-Forwarded-For": "203.0.113.7"})
+
+
+ "account,elevation,offered",
+ [
+ (OWNER, ("root", None), True),
+ (OWNER, ("sudo", "/usr/bin/sudo"), True),
+ (OWNER, PKEXEC, False),
+ (OWNER, (None, None), False),
+ (ALICE, ("root", None), False),
+ (ALICE, ("sudo", "/usr/bin/sudo"), False),
+ ],
+)
+def test_capability_through_a_proxy_offers_only_a_promptless_setup_to_the_owner(
+ host, linux, monkeypatch, account, elevation, offered
+):
+ _calls, saved, _plan = host
+ saved["elevation"] = elevation
+ monkeypatch.setattr(client_ip, "is_direct_local_request", _IS_DIRECT_LOCAL)
+ body = _proxied_capability(account).json()
+ assert body["can_run_setup"] is offered
+ assert body["setup_action"] == ("linux-install" if offered else None)
+ assert body["setup_blocked"] == (
+ None if offered else "not_owner" if account is ALICE else "not_local"
+ )
def test_capability_tells_the_owner_here_when_no_password_prompt_is_possible(host, linux):
@@ -208,12 +279,13 @@ def test_capability_names_no_block_when_the_button_is_offered(host, linux):
assert body["can_run_setup"] is True and body["setup_blocked"] is None
-def test_a_remote_status_read_never_checks_elevation(host, linux, monkeypatch):
- calls, _saved, _plan = host
+def test_a_remote_status_read_offers_no_password_prompt(host, linux, monkeypatch):
+ _calls, saved, _plan = host
+ saved["elevation"] = PKEXEC
monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
with _client(OWNER) as client:
setup = client.get("/sandbox").json()["setup"]
- assert setup["can_run"] is False and calls["elevation_checks"] == 0
+ assert setup["can_run"] is False
def test_no_elevation_here_means_the_command_only(host, linux):
@@ -253,15 +325,43 @@ def test_the_capability_read_checks_a_tool_it_has_no_answer_for(host, linux, mon
os_sandbox.forget_tool_isolation()
-def test_a_remote_browser_is_refused(host, linux, monkeypatch):
[email protected]("elevation", [PKEXEC, (None, None)])
+def test_a_remote_browser_is_refused_a_setup_that_prompts(host, linux, monkeypatch, elevation):
monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
- calls, _saved, _plan = host
+ calls, saved, _plan = host
+ saved["elevation"] = elevation
with _client(OWNER) as client:
response = client.post("/sandbox/setup", json = {"operation": "linux-install"})
assert response.status_code == 403 and "computer running Unsloth" in response.json()["detail"]
assert calls["start"] == []
[email protected]("elevation", [("root", None), ("sudo", "/usr/bin/sudo")])
+def test_a_remote_owner_installs_when_nothing_prompts(host, linux, monkeypatch, elevation):
+ calls, saved, _plan = host
+ saved["elevation"] = elevation
+ monkeypatch.setattr(client_ip, "is_direct_local_request", _IS_DIRECT_LOCAL)
+ with _client(OWNER, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 50000)) as client:
+ response = client.post(
+ "/sandbox/setup",
+ json = {"operation": "linux-install"},
+ headers = {"X-Forwarded-For": "203.0.113.7"},
+ )
+ assert response.status_code == 200, response.text
+ assert calls["start"] == ["linux-install"] and calls["interactive"] == [False]
+
+
+def test_a_remote_api_key_or_other_account_is_refused_even_as_root(host, linux, monkeypatch):
+ calls, saved, _plan = host
+ saved["elevation"] = ("root", None)
+ monkeypatch.setattr(client_ip, "is_direct_local_request", lambda _request: False)
+ with _client(OWNER, via_api_key = True) as client:
+ assert client.post("/sandbox/setup", json = {"operation": "linux-install"}).status_code == 403
+ with _client(ALICE) as client:
+ assert client.post("/sandbox/setup", json = {"operation": "linux-install"}).status_code == 403
+ assert calls["start"] == []
+
+
def test_api_keys_and_other_accounts_are_refused(host, linux):
calls, _saved, _plan = host
with _client(OWNER, via_api_key = True) as client:
@@ -304,7 +404,7 @@ def test_the_operation_must_match_the_platform(host, linux, monkeypatch):
def test_nothing_to_set_up_is_a_conflict(host, linux, monkeypatch):
- def unavailable(_operation):
+ def unavailable(_operation, interactive = True):
raise sandbox_setup_job.SetupUnavailable("OS isolation already works on this computer.")
monkeypatch.setattr(sandbox_setup_job, "start", unavailable)
@@ -317,7 +417,7 @@ def test_windows_consent_turns_the_opt_in_on_once_the_setup_is_accepted(host, wi
calls, saved, _plan = host
seen = []
- def start(operation):
+ def start(operation, interactive = True):
# The opt-in must still be off while the job is decided, or the plan reads "already works".
seen.append(saved["dacl"])
return sandbox_setup_job.SetupJob(id = "job1", operation = operation)
@@ -334,7 +434,7 @@ def test_windows_consent_turns_the_opt_in_on_once_the_setup_is_accepted(host, wi
def test_windows_consent_is_not_saved_when_the_setup_is_refused(host, windows, monkeypatch):
_calls, saved, _plan = host
- def unavailable(_operation):
+ def unavailable(_operation, interactive = True):
raise sandbox_setup_job.SetupUnavailable("OS isolation already works on this computer.")
monkeypatch.setattr(sandbox_setup_job, "start", unavailable)
@@ -364,7 +464,7 @@ def test_a_remote_owner_may_install_the_runtime_but_not_prepare(host, windows, m
client.post("/sandbox/setup", json = {"operation": "windows-runtime"}).status_code == 200
)
assert client.post("/sandbox/setup", json = {"operation": "windows-setup"}).status_code == 403
- assert calls["start"] == ["windows-runtime"]
+ assert calls["start"] == ["windows-runtime"] and calls["interactive"] == [False]
def test_windows_setup_without_consent_leaves_the_opt_in_alone(host, windows):