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: gate repo-hosted embedder modules, MLX model_file, and _socket (#12659)

Daniel Han5971d280d4b6
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 79a3e8be68b..2dfdc069736 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -2667,6 +2667,7 @@ _AUTO_UNSAFE_PY_MODULES = frozenset(
"subprocess",
"shutil",
"socket",
+ "_socket",
"ctypes",
"multiprocessing",
"pty",
@@ -17132,6 +17133,8 @@ def _web_search_images_suffix(client, query, wanted, cancel_event, website_polic
_NETWORK_ROOT_NAMES = frozenset(
{
"socket",
+ # The C module behind `socket`: same primitives, so it screens the same.
+ "_socket",
"urllib",
"urllib3",
"http",
@@ -17541,6 +17544,9 @@ def _check_signal_escape_patterns(code: str):
"socket.socket",
"socket.create_connection",
"socket.getaddrinfo",
+ "_socket.socket",
+ "_socket.SocketType",
+ "_socket.getaddrinfo",
"urllib.request.urlopen",
"urllib.request.urlretrieve",
"urllib3.",
@@ -17568,6 +17574,7 @@ def _check_signal_escape_patterns(code: str):
_NETWORK_MODULES = frozenset(
{
"socket",
+ "_socket",
"urllib.request",
"urllib3",
"urllib3.connection",
@@ -17598,6 +17605,7 @@ def _check_signal_escape_patterns(code: str):
{
"socket.create_connection",
"socket.getaddrinfo",
+ "_socket.getaddrinfo",
"urllib.request.urlopen",
"urllib.request.urlretrieve",
"http.client.HTTPConnection",
@@ -17609,7 +17617,7 @@ def _check_signal_escape_patterns(code: str):
),
}
)
- _HOST_ARG_ROOTS = ("socket.", "http.client.")
+ _HOST_ARG_ROOTS = ("socket.", "_socket.", "http.client.")
_NETWORK_DESTINATION_ARG = {
fq: (
0,
@@ -17654,7 +17662,13 @@ def _check_signal_escape_patterns(code: str):
"urllib3.poolmanager.proxy_from_url",
"urllib3.contrib.socks.SOCKSProxyManager",
)
- _SOCKET_CLIENTS = ("socket.socket", "paramiko.SSHClient", "paramiko.client.SSHClient")
+ # SocketType is an alias of the socket class in both modules.
+ _SOCKET_TYPES = ("socket.socket", "socket.SocketType", "_socket.socket", "_socket.SocketType")
+ _SOCKET_CLIENTS = (
+ *_SOCKET_TYPES,
+ "paramiko.SSHClient",
+ "paramiko.client.SSHClient",
+ )
_OPENER_CLIENTS = ("urllib.request.build_opener", "urllib.request.OpenerDirector")
_CLIENT_CLASSES = frozenset(
(*_VERB_CLIENTS, *_POOL_CLIENTS, *_SOCKET_CLIENTS, *_OPENER_CLIENTS)
@@ -17731,13 +17745,17 @@ def _check_signal_escape_patterns(code: str):
for conn in ("HTTPConnection", "HTTPSConnection")
},
"urllib3.util.connection.create_connection": (0, ("address",), "host"),
- **{f"socket.socket.{m}": (0, ("address",), "host") for m in ("connect", "connect_ex")},
+ **{
+ f"{sock}.{m}": (0, ("address",), "host")
+ for sock in _SOCKET_TYPES
+ for m in ("connect", "connect_ex")
+ },
**{f"{opener}.open": (0, ("fullurl",), "url") for opener in _OPENER_CLIENTS},
"urllib.request.ProxyHandler": (None, (), "proxy"),
# A datagram names its address per send. `sendto(data, flags, address)` puts the int
# flags at index 1, which reads as unreadable and fails closed.
- "socket.socket.sendto": (1, (), "host"),
- "socket.socket.sendmsg": (3, (), "host"),
+ **{f"{sock}.sendto": (1, (), "host") for sock in _SOCKET_TYPES},
+ **{f"{sock}.sendmsg": (3, (), "host") for sock in _SOCKET_TYPES},
**{
f"{client}.connect": (0, ("hostname", "host"), "host")
for client in ("paramiko.SSHClient", "paramiko.client.SSHClient")
diff --git a/studio/backend/core/rag/embeddings.py b/studio/backend/core/rag/embeddings.py
index 303e5869ef8..8148828ca8a 100644
--- a/studio/backend/core/rag/embeddings.py
+++ b/studio/backend/core/rag/embeddings.py
@@ -404,6 +404,107 @@ def _st_accepts_local_files_only(st_cls) -> bool:
return False
+_ST_PACKAGE_PREFIX = "sentence_transformers."
+_ST_GATE_MARKER = "_unsloth_custom_module_gate"
+
+
+def _refuse_custom_module(class_ref, model_name_or_path, trust_remote_code) -> None:
+ if (
+ isinstance(class_ref, str)
+ and not class_ref.startswith(_ST_PACKAGE_PREFIX)
+ and model_name_or_path is not None
+ and not trust_remote_code
+ ):
+ raise ValueError(
+ f"The model {model_name_or_path} references the module class {class_ref!r}, which is not "
+ "part of Sentence Transformers. Importing it executes third-party code, so Studio refuses "
+ "to load it as an embedding model."
+ )
+
+
+def _gate_st_custom_modules() -> None:
+ """Backport sentence-transformers 6.0's trust gate (CVE-2026-68770): before 6.0 a cached
+ model's modules.json could import repo-hosted classes without trust_remote_code."""
+ try:
+ import inspect
+ import sentence_transformers as st
+ from packaging.version import Version
+
+ if Version(st.__version__).major >= 6:
+ return
+ from sentence_transformers import SentenceTransformer
+ except Exception:
+ return
+ # 5.0-5.4 resolve on the model class; 5.5+ also via util.misc.import_module_class.
+ owner = next(
+ (c for c in SentenceTransformer.__mro__ if "_load_module_class_from_ref" in vars(c)),
+ None,
+ )
+ if owner is not None:
+ original = vars(owner)["_load_module_class_from_ref"]
+ if not getattr(original, _ST_GATE_MARKER, False):
+ signature = inspect.signature(original)
+
+ def _load_module_class_from_ref(self, *args, **kwargs):
+ bound = signature.bind_partial(self, *args, **kwargs).arguments
+ _refuse_custom_module(
+ bound.get("class_ref"),
+ bound.get("model_name_or_path"),
+ bound.get("trust_remote_code"),
+ )
+ return original(self, *args, **kwargs)
+
+ setattr(_load_module_class_from_ref, _ST_GATE_MARKER, True)
+ _load_module_class_from_ref.__wrapped__ = original
+ setattr(owner, "_load_module_class_from_ref", _load_module_class_from_ref)
+ # 5.0-5.4 Router imports via import_from_string, past both resolvers.
+ try:
+ import importlib
+
+ router = importlib.import_module("sentence_transformers.models.Router")
+ original_from_string = getattr(router, "import_from_string", None)
+ if original_from_string is not None and not getattr(
+ original_from_string, _ST_GATE_MARKER, False
+ ):
+
+ def import_from_string(dotted_path, *args, **kwargs):
+ _refuse_custom_module(dotted_path, "behind this Router", False)
+ return original_from_string(dotted_path, *args, **kwargs)
+
+ setattr(import_from_string, _ST_GATE_MARKER, True)
+ import_from_string.__wrapped__ = original_from_string
+ router.import_from_string = import_from_string
+ except Exception:
+ pass
+ try:
+ import sys
+ from sentence_transformers.util import misc
+ except Exception:
+ return
+ original_import = getattr(misc, "import_module_class", None)
+ if original_import is None or getattr(original_import, _ST_GATE_MARKER, False):
+ return
+
+ def import_module_class(
+ class_ref,
+ model_name_or_path = None,
+ *args,
+ **kwargs,
+ ):
+ _refuse_custom_module(class_ref, model_name_or_path, kwargs.get("trust_remote_code"))
+ return original_import(class_ref, model_name_or_path, *args, **kwargs)
+
+ setattr(import_module_class, _ST_GATE_MARKER, True)
+ import_module_class.__wrapped__ = original_import
+ # Rebind every module that imported the function by name.
+ for module in list(sys.modules.values()):
+ if (
+ getattr(module, "__name__", "").startswith("sentence_transformers")
+ and getattr(module, "import_module_class", None) is original_import
+ ):
+ setattr(module, "import_module_class", import_module_class)
+
+
def _get(model_name: str | None = None):
"""Cached SentenceTransformer, (re)loading on a name change. Loaded in fp16 on an
accelerator for a ~1.5x speedup at negligible accuracy loss, fp32 on CPU."""
@@ -427,6 +528,8 @@ def _get(model_name: str | None = None):
from sentence_transformers import SentenceTransformer
from utils.hf_cache_settings import active_hf_hub_cache
+ _gate_st_custom_modules()
+
logger.info("loading embedding model %s on %s", name, device)
st_kwargs = dict(
device = device,
diff --git a/studio/backend/tests/test_consent_gate.py b/studio/backend/tests/test_consent_gate.py
index 58dbf13c757..f1e8c1afa15 100644
--- a/studio/backend/tests/test_consent_gate.py
+++ b/studio/backend/tests/test_consent_gate.py
@@ -1971,3 +1971,21 @@ class TestDiscardRemoteCodeDownload:
src = (_BACKEND / "routes/models.py").read_text(encoding = "utf-8")
assert "created_by_scan" in src
assert "discard-remote-code" in src
+
+
+class TestModelFileCountsAsRemoteCode:
+ """mlx-lm / mlx-vlm exec_module a config's ``model_file`` with no trust check, so it is remote
+ code exactly like ``auto_map`` and must reach the scan and approval dialog."""
+
+ def test_local_model_file_is_remote_code(self, tmp_path):
+ (tmp_path / "config.json").write_text(json.dumps({"model_file": "custom_model.py"}))
+ (tmp_path / "custom_model.py").write_text("class Model: pass\nclass ModelArgs: pass\n")
+ assert consent._config_has_auto_map(str(tmp_path)) is True
+
+ def test_plain_local_config_is_not_remote_code(self, tmp_path):
+ (tmp_path / "config.json").write_text(json.dumps({"model_type": "llama"}))
+ assert consent._config_has_auto_map(str(tmp_path)) is False
+
+ def test_empty_model_file_is_not_remote_code(self, tmp_path):
+ (tmp_path / "config.json").write_text(json.dumps({"model_file": ""}))
+ assert consent._config_has_auto_map(str(tmp_path)) is False
diff --git a/studio/backend/tests/test_embedding_model_security_gate.py b/studio/backend/tests/test_embedding_model_security_gate.py
index 45b4e99a499..f0e47113a90 100644
--- a/studio/backend/tests/test_embedding_model_security_gate.py
+++ b/studio/backend/tests/test_embedding_model_security_gate.py
@@ -752,3 +752,88 @@ def test_offline_cached_acceptance_still_asks_who_is_asking(client, monkeypatch)
assert r.status_code == 409
assert saved.get("model") != "acme/private-embedder"
hf_tokens.reset_repo_access_cache()
+
+
+def _custom_module_repo(tmp_path):
+ """A local embedding repo whose modules.json names a repo-hosted module class. Importing it
+ drops a marker file, so a test can tell whether the repo's code ran."""
+ import json
+
+ marker = tmp_path / "ran.txt"
+ (tmp_path / "custom_mod.py").write_text(
+ "from pathlib import Path\n"
+ f"Path({str(marker)!r}).write_text('ran')\n"
+ "class Pooling:\n"
+ " pass\n"
+ )
+ (tmp_path / "modules.json").write_text(
+ json.dumps([{"idx": 0, "name": "0", "path": "", "type": "custom_mod.Pooling"}])
+ )
+ return marker
+
+
+def test_st_gate_refuses_repo_hosted_module_class_on_local_path(tmp_path):
+ # sentence-transformers < 6 trusted repo code for any local path (CVE-2026-68770), and the
+ # embedder loads the cached snapshot directory. The gate must refuse before the code runs.
+ st = pytest.importorskip("sentence_transformers")
+ import core.rag.embeddings as embeddings
+
+ marker = _custom_module_repo(tmp_path)
+ embeddings._gate_st_custom_modules()
+ with pytest.raises(ValueError, match = "not part of Sentence Transformers"):
+ st.SentenceTransformer(str(tmp_path), device = "cpu")
+ assert not marker.exists()
+
+
+def test_st_gate_allows_stock_classes_and_explicit_trust(tmp_path):
+ st = pytest.importorskip("sentence_transformers")
+ import core.rag.embeddings as embeddings
+
+ embeddings._gate_st_custom_modules()
+ owner = next(
+ c for c in st.SentenceTransformer.__mro__ if "_load_module_class_from_ref" in vars(c)
+ )
+ resolve = vars(owner)["_load_module_class_from_ref"]
+ model = object.__new__(st.SentenceTransformer)
+ pooling = resolve(
+ model, "sentence_transformers.models.Pooling", str(tmp_path), False, None, None
+ )
+ assert pooling.__name__ == "Pooling"
+ # An explicit opt-in still reaches the original resolver.
+ _custom_module_repo(tmp_path)
+ resolve(model, "custom_mod.Pooling", str(tmp_path), True, None, None)
+
+
+def test_st_gate_is_idempotent():
+ st = pytest.importorskip("sentence_transformers")
+ import core.rag.embeddings as embeddings
+
+ embeddings._gate_st_custom_modules()
+ owner = next(
+ c for c in st.SentenceTransformer.__mro__ if "_load_module_class_from_ref" in vars(c)
+ )
+ first = vars(owner)["_load_module_class_from_ref"]
+ embeddings._gate_st_custom_modules()
+ assert vars(owner)["_load_module_class_from_ref"] is first
+ if int(st.__version__.split(".")[0]) < 6:
+ assert getattr(first, embeddings._ST_GATE_MARKER, False)
+ assert not getattr(first.__wrapped__, embeddings._ST_GATE_MARKER, False)
+
+
+def test_st_gate_covers_router_sub_module_types():
+ # sentence-transformers 5.0-5.4 Router resolves its sub-module types with import_from_string,
+ # past both class resolvers, so the gate has to cover that name too.
+ st = pytest.importorskip("sentence_transformers")
+ if int(st.__version__.split(".")[0]) >= 6:
+ pytest.skip("sentence-transformers 6 gates this itself")
+ import importlib
+
+ import core.rag.embeddings as embeddings
+
+ router = importlib.import_module("sentence_transformers.models.Router")
+ if not hasattr(router, "import_from_string"):
+ pytest.skip("this Router resolves sub-modules through the gated import_module_class")
+ embeddings._gate_st_custom_modules()
+ with pytest.raises(ValueError, match = "not part of Sentence Transformers"):
+ router.import_from_string("custom_mod.Pooling")
+ assert router.import_from_string("sentence_transformers.models.Pooling").__name__ == "Pooling"
diff --git a/studio/backend/tests/test_sandbox_tools.py b/studio/backend/tests/test_sandbox_tools.py
index 12a011d57f4..9b7c9eddd3b 100644
--- a/studio/backend/tests/test_sandbox_tools.py
+++ b/studio/backend/tests/test_sandbox_tools.py
@@ -52,6 +52,10 @@ class TestMetadataHostDenylist:
'import socket; s=socket.socket(); s.connect(("100.100.100.200", 80))',
id = "alibaba_ecs_literal_blocked",
),
+ pytest.param(
+ 'import _socket; s=_socket.socket(); s.connect(("169.254.169.254", 80))',
+ id = "c_socket_imds_literal_blocked",
+ ),
pytest.param(
'import urllib.request; urllib.request.urlopen("http://[fd00:ec2::254]/")',
id = "ipv6_imds_literal_blocked",
@@ -131,6 +135,23 @@ class TestUntrustedHostBlock:
'import socket; s=socket.socket(); s.connect(("evil.example", 80))',
id = "socket_connect_random_host_blocked",
),
+ # `_socket` is the C module behind `socket`: same primitives, same verdict.
+ pytest.param(
+ 'import _socket; s=_socket.socket(); s.connect(("evil.example", 80))',
+ id = "c_socket_connect_random_host_blocked",
+ ),
+ pytest.param(
+ 'from _socket import socket; s=socket(); s.connect(("evil.example", 80))',
+ id = "c_socket_from_import_connect_blocked",
+ ),
+ pytest.param(
+ 'import _socket; _socket.SocketType().connect_ex(("evil.example", 80))',
+ id = "c_socket_sockettype_alias_blocked",
+ ),
+ pytest.param(
+ 'import _socket as s; s.getaddrinfo("evil.example", 80)',
+ id = "c_socket_getaddrinfo_alias_blocked",
+ ),
],
)
def test_untrusted_host_block_blocked(self, code):
@@ -1301,6 +1322,7 @@ class TestNetworkTargetResolution:
"import asyncssh\nopts = asyncssh.SSHClientConnectionOptions(proxy_command='nc 203.0.113.5 22')\n"
"asyncssh.connect('pypi.org', options=opts)",
"import aiohttp\naiohttp.ClientSession('https://pypi.org').get('//' + host)",
+ "import _socket\nh = '.'.join(['10', '0', '0', '1'])\n_socket.socket().connect((h, 80))",
],
)
def test_unreadable_destination_refused(self, code):
diff --git a/studio/backend/utils/security/consent.py b/studio/backend/utils/security/consent.py
index 37deaf8a9f9..4ad097744ea 100644
--- a/studio/backend/utils/security/consent.py
+++ b/studio/backend/utils/security/consent.py
@@ -94,11 +94,18 @@ def _config_has_auto_map(
# Every nesting level, not just the top: a composite model declares auto_map on a
# sub-config, and the loader resolves it from there, so a top-level-only read
# returned "ships no remote code" for a repo whose code the load would run.
- if not any(config_declares_auto_map(cfg or {}) for cfg in configs):
+ if not any(
+ config_declares_auto_map(cfg or {}) or _config_declares_model_file(cfg) for cfg in configs
+ ):
return False
return True
+def _config_declares_model_file(cfg) -> bool:
+ """MLX loaders exec a config's ``model_file`` like an ``auto_map`` entry."""
+ return isinstance(cfg, dict) and bool(cfg.get("model_file"))
+
+
def _is_direct_gguf_file_ref(model_name: str) -> bool:
"""Whether ``model_name`` names a specific ``.gguf`` FILE (llama.cpp) rather than a repo: a local ``.gguf`` path or a remote ``org/repo/.../file.gguf`` (>= 2 slashes). A bare ``org/name.gguf`` is a repo id that can still ship safetensors + auto_map, so it falls through to the scan."""
name = model_name or ""