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: block more metadata host spellings for approved Python (#16937)

Nilay58a14430b1b7
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 7796003ffac..e5cafec6680 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -41,6 +41,7 @@ import sys
import tempfile
import contextlib
import threading
+import unicodedata
import zlib
from contextvars import ContextVar
@@ -104,6 +105,8 @@ from core.inference.mcp_client import (
from storage import mcp_servers_db
from utils.account_context import account_thread, current_account_id, is_owner_context
from utils.current_date_prompt_settings import strip_current_date_update_note
+from utils.host_policy import _literal_ip_address
+from core.inference.providers import _metadata_host as _provider_metadata_host
from utils import sandbox_memory_limit, tool_result_limit
from core.inference.tool_confinement import ToolConfinementUnavailable, account_confinement
from pathlib import Path
@@ -18327,9 +18330,17 @@ def _check_signal_escape_patterns(code: str):
return h
def _is_metadata_host(host: str) -> bool:
- h = _normalize_host(host)
+ h = _normalize_host(unicodedata.normalize("NFKC", host or ""))
if not h:
return False
+ if _provider_metadata_host(h):
+ return True
+ try:
+ ip = _literal_ip_address(h)
+ except ValueError:
+ return True
+ if ip is not None:
+ h = str(ip)
if h in _METADATA_HOST_LITERALS:
return True
if any(h.startswith(p) for p in _METADATA_HOST_PREFIXES):
diff --git a/studio/backend/tests/test_sandbox_tools.py b/studio/backend/tests/test_sandbox_tools.py
index 283da785fa3..148288522b5 100644
--- a/studio/backend/tests/test_sandbox_tools.py
+++ b/studio/backend/tests/test_sandbox_tools.py
@@ -193,6 +193,50 @@ class TestApprovedHostLiftsOnlyTheAllowlist:
assert msg is not None and phrase in msg, msg
assert "host not in sandbox allowlist" not in msg
+ @pytest.mark.parametrize(
+ "host",
+ [
+ "2852039166",
+ "0xa9fea9fe",
+ "0251.0376.0251.0376",
+ "[::ffff:169.254.169.254]",
+ "[::ffff:a9fe:a9fe]",
+ "[fd00:ec2:0::254]",
+ "[::ffff:100.100.100.200]",
+ "[fd00:ec2::254%1]",
+ "[fd20:ce::254]",
+ "[2002:a9fe:a9fe::]",
+ "metadata.goog",
+ "instance-data.ec2.internal",
+ ],
+ )
+ def test_approved_call_blocks_other_spellings_of_a_metadata_host(self, host):
+ code = f'import urllib.request; urllib.request.urlopen("http://{host}/")'
+ msg = _check_code_safety(code, host_approved = True)
+ assert msg is not None and "Blocked: cloud-metadata host" in msg, msg
+
+ @pytest.mark.parametrize(
+ "host",
+ [
+ "fd00:ec2::254%1",
+ "169\u3002254\u3002169\u3002254",
+ "\uff11\uff16\uff19.\uff12\uff15\uff14.\uff11\uff16\uff19.\uff12\uff15\uff14",
+ "169.254.169.254\\x00",
+ ],
+ )
+ def test_approved_socket_call_blocks_a_metadata_host(self, host):
+ code = f'import socket; socket.create_connection(("{host}", 80))'
+ msg = _check_code_safety(code, host_approved = True)
+ assert msg is not None and "Blocked: cloud-metadata host" in msg, msg
+ assert is_high_risk_tool_call("python", {"code": code})
+
+ @pytest.mark.parametrize(
+ "host", ["1.1.1.1", "10.0.0.1", "127.0.0.1", "[2606:4700:4700::1111]", "huggingface.co"]
+ )
+ def test_approved_call_allows_an_ordinary_host(self, host):
+ code = f'import urllib.request; urllib.request.urlopen("http://{host}/")'
+ assert _check_code_safety(code, host_approved = True) is None
+
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