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: keep chat-installed packages out of plugin discovery on approved host calls (#14948)

Daniel Han6ef7574ae879
diff --git a/studio/backend/core/inference/sandbox_site/sitecustomize.py b/studio/backend/core/inference/sandbox_site/sitecustomize.py
index cb322c9f626..640d1412de1 100644
--- a/studio/backend/core/inference/sandbox_site/sitecustomize.py
+++ b/studio/backend/core/inference/sandbox_site/sitecustomize.py
@@ -255,11 +255,105 @@ try:
except Exception: # noqa: BLE001 - a broken shim must never break user code
pass
-# Approved host calls keep chat-installed packages importable, including Python started by Terminal.
-# Append only after the normal site setup; no .pth execution or stdlib/installed-package shadowing.
+
+# Approved host calls import chat-installed packages through a last-resort finder, NOT sys.path: a folder on
+# sys.path exposes its *.dist-info to importlib.metadata, so entry-point autoload (pytest11, ...) would run
+# chat-planted code the command never named. Metadata answers only an exact name with no explicit path.
+class _HostPackagesFinder:
+ _skip = frozenset(("sitecustomize", "usercustomize"))
+
+ def __init__(self, folder):
+ self._folder = folder
+
+ def find_spec(
+ self,
+ name,
+ path = None,
+ target = None,
+ ):
+ import importlib.machinery
+
+ if "." not in name:
+ if path is not None or name in self._skip:
+ return None
+ return importlib.machinery.PathFinder.find_spec(name, [self._folder])
+ parent, _, _ = name.rpartition(".")
+ module = sys.modules.get(parent)
+ spec = getattr(module, "__spec__", None)
+ if spec is None or spec.submodule_search_locations is None:
+ return None
+ # Only namespace packages: a regular package's submodules come from its own __path__ alone.
+ pkgutil_style = spec.origin not in (None, "namespace")
+ if pkgutil_style and not self._extends_path(spec.origin):
+ return None
+ portion = os.path.join(self._folder, *parent.split("."))
+ if not os.path.isdir(portion):
+ return None
+ found = importlib.machinery.PathFinder.find_spec(name, [portion])
+ if found is not None:
+ # The portion extend_path / PEP 420 would have collected; a _NamespacePath drops it on sys.path changes.
+ search = getattr(module, "__path__", None)
+ if search is not None and hasattr(search, "append") and portion not in list(search):
+ search.append(portion)
+ return found
+
+ _extends = {}
+
+ @classmethod
+ def _extends_path(cls, origin):
+ # Open only if __init__ declares extend_path / declare_namespace at module level (try body / else /
+ # finally count, an except handler may not have run); text, imports or calls under if/def do not.
+ if origin not in cls._extends:
+ try:
+ import ast
+ with open(origin, "rb") as f:
+ tree = ast.parse(f.read())
+ cls._extends[origin] = any(map(cls._declares_namespace, cls._top_level(tree.body)))
+ except Exception: # noqa: BLE001 - an unreadable __init__ is treated as a regular package
+ cls._extends[origin] = False
+ return cls._extends[origin]
+
+ @classmethod
+ def _top_level(cls, body):
+ import ast
+ for node in body:
+ yield node
+ if isinstance(node, ast.Try):
+ for block in (node.body, node.orelse, node.finalbody):
+ yield from cls._top_level(block)
+
+ @staticmethod
+ def _declares_namespace(node):
+ import ast
+
+ def call_of(value, name, args):
+ if not isinstance(value, ast.Call) or value.keywords:
+ return False
+ func = value.func
+ called = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", None)
+ return called == name and [getattr(a, "id", None) for a in value.args] == args
+
+ if isinstance(node, ast.Assign):
+ return any(getattr(t, "id", None) == "__path__" for t in node.targets) and call_of(
+ node.value, "extend_path", ["__path__", "__name__"]
+ )
+ return isinstance(node, ast.Expr) and call_of(node.value, "declare_namespace", ["__name__"])
+
+ def find_distributions(self, context = None):
+ name = getattr(context, "name", None)
+ # An explicit path is the caller's scope, already served by the stock path finder.
+ if not name or "path" in getattr(context, "__dict__", {}):
+ return iter(())
+ import importlib.metadata
+
+ return importlib.metadata.MetadataPathFinder.find_distributions(
+ importlib.metadata.DistributionFinder.Context(name = name, path = [self._folder])
+ )
+
+
_host_packages = os.environ.get("_UNSLOTH_HOST_SESSION_PACKAGES")
-if _host_packages and _host_packages not in sys.path:
- sys.path.append(_host_packages)
+if _host_packages and not any(isinstance(f, _HostPackagesFinder) for f in sys.meta_path):
+ sys.meta_path.append(_HostPackagesFinder(_host_packages))
# Helper modules the approved script imports by name come from the chat folder, after everything
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 11a2483d26e..0726dfde081 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -8866,7 +8866,7 @@ def _host_access_launch(plan, script: "str | None") -> "tuple[tuple, dict, str]"
argv = tuple(plan.argv)
packages = os.path.join(plan.workdir, os_sandbox.SESSION_PACKAGES_RELPATH)
if os.path.isdir(packages):
- # The trusted sitecustomize appends these after installed packages for Python and Terminal alike.
+ # Served by sitecustomize's last-resort finder, not sys.path, so plugin discovery never sees them.
env["_UNSLOTH_HOST_SESSION_PACKAGES"] = packages
env["PYTHONNOUSERSITE"] = "1"
# Chat-installed CLIs the approved call names resolve too, but only after every system executable.
diff --git a/studio/backend/tests/test_sandbox_sitecustomize.py b/studio/backend/tests/test_sandbox_sitecustomize.py
index 8c813788694..b505209a2b5 100644
--- a/studio/backend/tests/test_sandbox_sitecustomize.py
+++ b/studio/backend/tests/test_sandbox_sitecustomize.py
@@ -404,3 +404,112 @@ def test_os_open_trunc_without_creat_missing_stays_truthful(monkeypatch, tmp_pat
assert not os.path.exists(os.path.join(os.getcwd(), "missing_xyz.bin"))
finally:
_restore_patch_targets(saved)
+
+
+ "init, namespace",
+ [
+ ("__path__ = __import__('pkgutil').extend_path(__path__, __name__)\n", True),
+ ("from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n", True),
+ ("__import__('pkg_resources').declare_namespace(__name__)\n", True),
+ (
+ "try:\n __import__('pkg_resources').declare_namespace(__name__)\n"
+ "except ImportError:\n import pkgutil\n"
+ " __path__ = pkgutil.extend_path(__path__, __name__)\n",
+ True,
+ ),
+ ("# extend_path and declare_namespace are not used here\n", False),
+ ("from pkgutil import extend_path\n", False),
+ ("DOC = '__path__ = extend_path(__path__, __name__)'\n", False),
+ ("if False:\n __import__('pkg_resources').declare_namespace(__name__)\n", False),
+ ("def setup():\n __path__ = extend_path(__path__, __name__)\n", False),
+ ("paths = extend_path(['x'], 'other')\n", False),
+ ("this is not python(\n", False),
+ (
+ "try:\n import _no_such_module\n"
+ "except ImportError:\n __path__ = __import__('pkgutil').extend_path(__path__, __name__)\n",
+ False,
+ ),
+ (
+ "try:\n import os\n"
+ "except ImportError:\n pass\n"
+ "else:\n __path__ = __import__('pkgutil').extend_path(__path__, __name__)\n",
+ True,
+ ),
+ ],
+)
+def test_host_packages_finder_only_treats_declared_namespaces_as_open(tmp_path, init, namespace):
+ """Only a real extend_path / declare_namespace declaration opens a regular-looking package to
+ the chat folder's portion; text that merely mentions them does not."""
+ finder = _load_shim()._HostPackagesFinder
+ origin = tmp_path / "__init__.py"
+ origin.write_text(init, encoding = "utf-8")
+ assert finder._extends_path(str(origin)) is namespace
+
+
+def _run_with_host_packages(tmp_path, folder, code):
+ """Run ``code`` in a fresh interpreter with the shim installed for ``folder``."""
+ import subprocess
+ import sys
+
+ script = f"import runpy; runpy.run_path({str(_SHIM)!r})\n" + code
+ env = {**os.environ, "_UNSLOTH_HOST_SESSION_PACKAGES": str(folder)}
+ env.pop("PYTHONPATH", None)
+ done = subprocess.run(
+ [sys.executable, "-I", "-c", script],
+ cwd = tmp_path,
+ env = env,
+ capture_output = True,
+ text = True,
+ timeout = 60,
+ )
+ return done.stdout + done.stderr
+
+
+def test_host_packages_metadata_honours_an_explicit_search_path(tmp_path):
+ """An exact-name lookup scoped to explicit paths never answers from the chat folder."""
+ folder = tmp_path / "packages"
+ trusted = tmp_path / "trusted"
+ trusted.mkdir()
+ info = folder / "chatmeta-1.0.dist-info"
+ info.mkdir(parents = True)
+ (info / "METADATA").write_text(
+ "Metadata-Version: 2.1\nName: chatmeta\nVersion: 1.0\n", encoding = "utf-8"
+ )
+ out = _run_with_host_packages(
+ tmp_path,
+ folder,
+ "import importlib.metadata as m\n"
+ "print('DEFAULT', m.version('chatmeta'))\n"
+ f"print('TRUSTED', len(list(m.distributions(name = 'chatmeta', path = [{str(trusted)!r}]))))\n"
+ f"print('LISTED', len(list(m.distributions(name = 'chatmeta', path = [{str(folder)!r}]))))\n",
+ )
+ assert "DEFAULT 1.0" in out, out
+ assert "TRUSTED 0" in out, out
+ assert "LISTED 1" in out, out
+
+
+def test_host_packages_pep420_portion_joins_the_namespace_path(tmp_path):
+ """A submodule served from the chat folder's PEP 420 portion sees that portion through the
+ namespace's __path__ (importlib.resources, pkgutil.iter_modules); the host portion stays first."""
+ folder = tmp_path / "packages"
+ host = tmp_path / "host"
+ (host / "chatns").mkdir(parents = True)
+ (host / "chatns" / "host_mod.py").write_text("V = 'host'\n", encoding = "utf-8")
+ (folder / "chatns").mkdir(parents = True)
+ (folder / "chatns" / "data.txt").write_text("chat-data", encoding = "utf-8")
+ (folder / "chatns" / "chat_mod.py").write_text(
+ "import importlib.resources\n"
+ "V = importlib.resources.files('chatns').joinpath('data.txt').read_text()\n",
+ encoding = "utf-8",
+ )
+ out = _run_with_host_packages(
+ tmp_path,
+ folder,
+ f"import sys; sys.path.insert(0, {str(host)!r})\n"
+ "import os, pkgutil, chatns.host_mod, chatns.chat_mod\n"
+ "print('NS', chatns.chat_mod.V,"
+ " [os.path.basename(os.path.dirname(p)) for p in chatns.__path__],"
+ " sorted(i.name for i in pkgutil.iter_modules(chatns.__path__)))\n",
+ )
+ assert "NS chat-data ['host', 'packages'] ['chat_mod', 'host_mod']" in out, out
diff --git a/studio/backend/tests/test_tool_sandbox_hardening.py b/studio/backend/tests/test_tool_sandbox_hardening.py
index 1e9cda403af..64bbbc9b0e7 100644
--- a/studio/backend/tests/test_tool_sandbox_hardening.py
+++ b/studio/backend/tests/test_tool_sandbox_hardening.py
@@ -19,6 +19,36 @@ from core.inference.os_sandbox import ToolLaunchPlan
_POSIX = pytest.mark.skipif(sys.platform == "win32", reason = "POSIX launch layout")
+# Old importlib.metadata.entry_points() returns a dict of groups; 3.10+ selects.
+_PYTEST11_SCAN = (
+ "_eps = m.entry_points()\n"
+ "_eps = _eps.select(group = 'pytest11') if hasattr(_eps, 'select') else _eps.get('pytest11', ())\n"
+ "print('SCAN', sorted(e.name for e in _eps if e.name.startswith('chat')))\n"
+)
+
+
[email protected]("shape", ["dict", "selectable"])
+def test_the_pytest11_scan_reads_both_entry_point_api_shapes(shape, capsys):
+ """The scan the approved-call checks run works on the 3.9 dict API and the 3.10+ select API."""
+ import types
+
+ hit = types.SimpleNamespace(name = "chatplugin")
+ other = types.SimpleNamespace(name = "hostplugin")
+
+ class Selectable(list):
+ def select(self, group):
+ return [hit, other] if group == "pytest11" else []
+
+ def entry_points(**kwargs):
+ if shape == "dict":
+ if kwargs:
+ raise TypeError("entry_points() got an unexpected keyword argument 'group'")
+ return {"pytest11": (hit, other), "console_scripts": ()}
+ return Selectable()
+
+ exec(_PYTEST11_SCAN, {"m": types.SimpleNamespace(entry_points = entry_points)})
+ assert capsys.readouterr().out == "SCAN ['chatplugin']\n"
+
def _planted_workdir(tmp_path):
workdir = tmp_path / "chat"
@@ -86,6 +116,154 @@ def test_an_approved_terminal_call_reuses_chat_packages_without_shadowing(tmp_pa
assert "PLANTED" not in out, out
+def _plant_chat_plugin(packages, marker):
+ """A pytest11 entry point a chat pip-installed into its package folder, which copies a host file when loaded."""
+ secret = marker.parent / "host-secret.txt"
+ secret.write_text("outside-chat-secret", encoding = "utf-8")
+ (packages / "chat_autoload.py").write_text(
+ f"import pathlib\npathlib.Path({str(marker)!r}).write_text(pathlib.Path({str(secret)!r}).read_text())\n",
+ encoding = "utf-8",
+ )
+ info = packages / "chat_autoload-1.0.dist-info"
+ info.mkdir()
+ (info / "METADATA").write_text(
+ "Metadata-Version: 2.1\nName: chat-autoload\nVersion: 1.0\n", encoding = "utf-8"
+ )
+ (info / "entry_points.txt").write_text(
+ "[pytest11]\nchat_autoload = chat_autoload\n", encoding = "utf-8"
+ )
+
+
+@_POSIX
+def test_an_approved_pytest_call_does_not_autoload_chat_installed_plugins(tmp_path, monkeypatch):
+ """Session packages must not be visible to entry-point discovery: pytest would import a planted plugin."""
+ workdir = _planted_workdir(tmp_path)
+ (workdir / "csv.py").unlink()
+ packages = workdir / os_sandbox.SESSION_PACKAGES_RELPATH
+ host = tmp_path / "host"
+ host.mkdir()
+ marker = host / "plugin-ran.txt"
+ _plant_chat_plugin(packages, marker)
+ (workdir / "test_ok.py").write_text(
+ "import chatpkg\n\ndef test_ok():\n assert chatpkg.VALUE == 'from-session-packages'\n",
+ encoding = "utf-8",
+ )
+ host_file = host / "notes.csv"
+ host_file.write_text("a,b\n", encoding = "utf-8")
+ monkeypatch.setattr(tools, "_get_workdir", lambda session_id = None: str(workdir))
+ command = (
+ shlex.quote(sys.executable)
+ + " -m pytest -q -o addopts= -p no:cacheprovider test_ok.py && cat "
+ + shlex.quote(str(host_file))
+ )
+ assert tools._reaches_host_paths("terminal", command)
+ out = tools._bash_exec(command, None, 60, "hardening-chat", host_access_approved = True)
+ assert "1 passed" in out and "a,b" in out, out
+ assert not marker.exists(), "a chat-installed pytest plugin ran on the host"
+
+
+@_POSIX
+def test_an_approved_script_still_imports_chat_installed_packages(tmp_path, monkeypatch):
+ """Explicit imports (packages, submodules, their own metadata) keep working; name-less scans see nothing."""
+ workdir = _planted_workdir(tmp_path)
+ packages = workdir / os_sandbox.SESSION_PACKAGES_RELPATH
+ (packages / "chatlib").mkdir()
+ (packages / "chatlib" / "__init__.py").write_text("from .sub import X\n", encoding = "utf-8")
+ (packages / "chatlib" / "sub.py").write_text("X = 'chatlib-sub'\n", encoding = "utf-8")
+ info = packages / "chatlib-2.0.dist-info"
+ info.mkdir()
+ (info / "METADATA").write_text(
+ "Metadata-Version: 2.1\nName: chatlib\nVersion: 2.0\n", encoding = "utf-8"
+ )
+ marker = tmp_path / "plugin-ran.txt"
+ _plant_chat_plugin(packages, marker)
+ monkeypatch.setattr(tools, "_get_workdir", lambda session_id = None: str(workdir))
+ code = (
+ "import importlib.metadata as m, chatlib, chatlib.sub, chatpkg\n"
+ "print('LIB', chatlib.X, chatlib.sub.X, chatpkg.VALUE, m.version('chatlib'))\n"
+ + _PYTEST11_SCAN
+ + f"open({str(tmp_path / 'host.txt')!r}, 'w').close()\n"
+ )
+ out = tools._python_exec(code, None, 60, "hardening-chat", host_access_approved = True)
+ assert "LIB chatlib-sub chatlib-sub from-session-packages 2.0" in out, out
+ assert "SCAN []" in out, out
+ assert not marker.exists(), out
+
+
+@_POSIX
+def test_an_approved_script_imports_chat_portions_of_host_namespace_packages(tmp_path, monkeypatch):
+ """A PEP 420 namespace the host already provides still reaches the portion the chat installed."""
+ workdir = _planted_workdir(tmp_path)
+ packages = workdir / os_sandbox.SESSION_PACKAGES_RELPATH
+ host = tmp_path / "host"
+ for root, owner in ((host, "host"), (packages, "chat")):
+ (root / "chatns" / "inner").mkdir(parents = True)
+ (root / "chatns" / f"{owner}_mod.py").write_text(f"V = '{owner}'\n", encoding = "utf-8")
+ (root / "chatns" / "inner" / f"{owner}_leaf.py").write_text(
+ f"V = '{owner}-leaf'\n", encoding = "utf-8"
+ )
+ (root / "chatns" / "shared.py").write_text(f"V = '{owner}-shared'\n", encoding = "utf-8")
+ (packages / "chatns" / "chatreg").mkdir()
+ (packages / "chatns" / "chatreg" / "__init__.py").write_text("V = 'reg'\n", encoding = "utf-8")
+ monkeypatch.setattr(tools, "_get_workdir", lambda session_id = None: str(workdir))
+ code = (
+ f"import sys; sys.path.insert(0, {str(host)!r})\n"
+ "import chatns.host_mod, chatns.chat_mod, chatns.shared, chatns.chatreg\n"
+ "import chatns.inner.host_leaf, chatns.inner.chat_leaf\n"
+ "print('NS', chatns.host_mod.V, chatns.chat_mod.V, chatns.shared.V, chatns.chatreg.V,"
+ " chatns.inner.host_leaf.V, chatns.inner.chat_leaf.V)\n"
+ f"open({str(tmp_path / 'host.txt')!r}, 'w').close()\n"
+ )
+ out = tools._python_exec(code, None, 60, "hardening-chat", host_access_approved = True)
+ # The host's own portion still wins for a name both provide.
+ assert "NS host chat host-shared reg host-leaf chat-leaf" in out, out
+
+
+@_POSIX
+def test_an_approved_script_imports_chat_portions_of_host_pkgutil_namespaces(tmp_path, monkeypatch):
+ """A pkgutil.extend_path namespace (backports, ...) the host provides reaches the chat's portion; a regular one does not."""
+ workdir = _planted_workdir(tmp_path)
+ packages = workdir / os_sandbox.SESSION_PACKAGES_RELPATH
+ host = tmp_path / "host"
+ extend = "__path__ = __import__('pkgutil').extend_path(__path__, __name__)\n"
+ # Only mentions the namespace helpers (a comment, an unused import, a string, a dead branch):
+ # still a regular package.
+ mentions = (
+ "# not a namespace: no extend_path or declare_namespace here\n"
+ "from pkgutil import extend_path\n"
+ "NOTE = 'declare_namespace(__name__)'\n"
+ "if False:\n __path__ = extend_path(__path__, __name__)\n"
+ )
+ for root, owner in ((host, "host"), (packages, "chat")):
+ for pkg, init in (("chatpkgutil", extend), ("chatregular", ""), ("chatmention", mentions)):
+ (root / pkg).mkdir(parents = True)
+ (root / pkg / "__init__.py").write_text(init, encoding = "utf-8")
+ (root / pkg / f"{owner}_mod.py").write_text(f"V = '{owner}'\n", encoding = "utf-8")
+ (root / pkg / "shared.py").write_text(f"V = '{owner}-shared'\n", encoding = "utf-8")
+ marker = tmp_path / "plugin-ran.txt"
+ _plant_chat_plugin(packages, marker)
+ monkeypatch.setattr(tools, "_get_workdir", lambda session_id = None: str(workdir))
+ code = (
+ f"import sys; sys.path.insert(0, {str(host)!r})\n"
+ "import importlib.metadata as m, pkgutil\n"
+ "import chatpkgutil.host_mod, chatpkgutil.chat_mod, chatpkgutil.shared, chatregular.host_mod\n"
+ "print('PKG', chatpkgutil.host_mod.V, chatpkgutil.chat_mod.V, chatpkgutil.shared.V,"
+ " sorted(i.name for i in pkgutil.iter_modules(chatpkgutil.__path__)))\n"
+ "for name in ('chatregular', 'chatmention'):\n"
+ " try:\n __import__(name + '.chat_mod')\n print('REGULAR leaked', name)\n"
+ " except ModuleNotFoundError:\n print('REGULAR closed', name)\n"
+ + _PYTEST11_SCAN
+ + f"open({str(tmp_path / 'host.txt')!r}, 'w').close()\n"
+ )
+ out = tools._python_exec(code, None, 60, "hardening-chat", host_access_approved = True)
+ # The host's own portion still wins for a name both provide.
+ assert "PKG host chat host-shared ['chat_mod', 'host_mod', 'shared']" in out, out
+ assert "REGULAR closed chatregular" in out and "REGULAR closed chatmention" in out, out
+ assert "REGULAR leaked" not in out, out
+ assert "SCAN []" in out, out
+ assert not marker.exists(), out
+
+
@_POSIX
def test_approved_calls_import_chat_helpers_without_shadowing_the_stdlib(tmp_path, monkeypatch):
workdir = _planted_workdir(tmp_path)