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: let an approved Python call reach a host outside the sandbox allowlist (#16674)

Daniel Hane60ca319b576
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 735ac264b5a..14f6ad57264 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -20048,9 +20048,9 @@ def _check_signal_escape_patterns(code: str):
}
-def _check_code_safety(code: str) -> str | None:
+def _check_code_safety(code: str, *, host_approved: bool = False) -> str | None:
"""Validate code safety via static analysis. Returns an error message string if the code is
- unsafe, or None if OK."""
+ unsafe, or None if OK. host_approved lets an approved call reach hosts off the allowlist."""
safe, info = _check_signal_escape_patterns(code)
if not safe:
# Let SyntaxError through so the child reports a normal traceback.
@@ -20062,7 +20062,11 @@ def _check_code_safety(code: str) -> str | None:
exception_reasons = [
item.get("description", "") for item in info.get("exception_catching", [])
]
- network_reasons = [item.get("description", "") for item in info.get("network_calls", [])]
+ network_reasons = [
+ item.get("description", "")
+ for item in info.get("network_calls", [])
+ if not (host_approved and item.get("type") == "untrusted_host_blocked")
+ ]
file_reasons = [
item.get("description", "") for item in info.get("sensitive_file_reads", [])
]
@@ -21976,7 +21980,10 @@ def _python_exec(
# Validate imports and code safety (skipped when the sandbox is disabled)
if not disable_sandbox:
- error = _check_code_safety(code)
+ # The allowlist is advisory (the network is not confined), so a user's approval lifts it; API keys keep it.
+ error = _check_code_safety(
+ code, host_approved = host_access_approved and not _api_host_access_refused()
+ )
if error:
# Capped: repeated forbidden constructs can produce an oversized error.
return _truncate(error)
diff --git a/studio/backend/tests/test_sandbox_tools.py b/studio/backend/tests/test_sandbox_tools.py
index e17d8b4f9e1..283da785fa3 100644
--- a/studio/backend/tests/test_sandbox_tools.py
+++ b/studio/backend/tests/test_sandbox_tools.py
@@ -165,6 +165,57 @@ class TestUntrustedHostBlock:
)
+class TestApprovedHostLiftsOnlyTheAllowlist:
+ def test_approved_untrusted_host_allowed(self):
+ code = 'import requests; requests.get("https://example.com/")'
+ assert _check_code_safety(code) is not None
+ assert _check_code_safety(code, host_approved = True) is None
+
+ @pytest.mark.parametrize(
+ "code, phrase",
+ [
+ (
+ 'import requests; requests.get("http://169.254.169.254/latest/meta-data/")',
+ "Blocked: cloud-metadata host",
+ ),
+ (
+ "import requests\nrequests.post('https://example.com/', files={'x': open('s')})",
+ "Blocked: file upload disallowed in sandbox",
+ ),
+ (
+ 'import os, requests; requests.get(os.environ["URL"])',
+ "Blocked: network destination is not a literal",
+ ),
+ ],
+ )
+ def test_approved_call_keeps_every_other_block(self, code, phrase):
+ msg = _check_code_safety(code, host_approved = True)
+ assert msg is not None and phrase in msg, msg
+ assert "host not in sandbox allowlist" not in msg
+
+ def test_executor_lifts_it_for_an_approved_call_only(self, tmp_path, monkeypatch):
+ from core.inference import tools
+ from utils.account_context import OWNER, as_api_caller, bind_account, reset_account
+
+ monkeypatch.setattr(tools, "_get_workdir", lambda session_id = None: str(tmp_path))
+ code = (
+ 'import urllib.request\nurllib.request.urlopen("http://unlisted.invalid/", timeout = 2)'
+ )
+ refused = "Blocked: host not in sandbox allowlist"
+ assert refused in tools._python_exec(code, None, 60, "host-approval")
+ assert refused not in tools._python_exec(
+ code, None, 60, "host-approval", host_access_approved = True
+ )
+ monkeypatch.delenv("UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS", raising = False)
+ token = bind_account(as_api_caller(OWNER))
+ try:
+ assert refused in tools._python_exec(
+ code, None, 60, "host-approval", host_access_approved = True
+ )
+ finally:
+ reset_account(token)
+
+
class TestLocalDatabaseFileIsNotAHost:
@pytest.mark.parametrize(
"code",