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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Refuse non-http schemes, embedded credentials and cloud metadata hosts; keep LAN and loopback."""
import importlib.util
import socket
import threading
from pathlib import Path
import pytest
_PROVIDERS_PATH = Path(__file__).resolve().parents[1] / "core" / "inference" / "providers.py"
_SPEC = importlib.util.spec_from_file_location("provider_registry_for_test", _PROVIDERS_PATH)
_providers = importlib.util.module_from_spec(_SPEC)
assert _SPEC.loader is not None
_SPEC.loader.exec_module(_providers)
validate_provider_base_url = _providers.validate_provider_base_url
PROVIDER_REGISTRY = _providers.PROVIDER_REGISTRY
BLOCK_PRIVATE_ENV = _providers._BLOCK_PRIVATE_ENV
_SUPPORTED = [
"http://localhost:11434/v1",
"http://localhost:8080/v1",
"http://127.0.0.1:8080/v1",
"http://127.0.0.1:1",
"http://192.168.1.50:8000/v1",
"http://10.1.2.3:8000/v1",
"http://my_ollama:11434/v1",
"http://llama.test",
"https://my-vllm-server.com/v1",
"https://my-resource.openai.azure.com/openai/v1",
"https://gw.example/v1?tenant=a",
"http://1681207502/v1",
"http://169.254.gateway.example.com/v1",
"https://例え.テスト/v1",
"http://[fd00:ec2::255]/v1",
"http://[fd20:ce::255]/v1",
"https://[2606:4700:4700::1111]/v1",
"https://user:[email protected]/v1",
] + [info["base_url"] for info in PROVIDER_REGISTRY.values() if info["base_url"]]
@pytest.fixture(autouse = True)
def _default_policy(monkeypatch):
"""Default deployment: the private-address opt-in is off."""
monkeypatch.delenv(BLOCK_PRIVATE_ENV, raising = False)
# The lookup caches per hostname and caps in-flight slots; reset so stubs don't leak across tests.
def _reset():
_providers._dns_cache.clear()
_providers._dns_in_flight = threading.BoundedSemaphore(_providers._DNS_MAX_IN_FLIGHT)
_reset()
yield
_reset()
@pytest.mark.parametrize("url", _SUPPORTED)
def test_supported_base_urls_are_unchanged(url):
assert validate_provider_base_url(url) == url
@pytest.mark.parametrize("url", _SUPPORTED)
def test_validation_is_idempotent(url):
once = validate_provider_base_url(url)
assert validate_provider_base_url(once) == once
def test_trailing_slash_and_whitespace_are_normalized():
assert validate_provider_base_url(" http://127.0.0.1:8080/v1/ ") == "http://127.0.0.1:8080/v1"
def test_no_dns_lookup_for_shipped_providers_or_ip_literals(monkeypatch):
"""The common path stays resolver-free: shipped hosts and IP literals."""
# Recorded, not raised: the lookup runs on a worker thread that swallows exceptions.
calls = []
def _record(host, port, *args, **kwargs):
calls.append(host)
return []
monkeypatch.setattr(socket, "getaddrinfo", _record)
assert validate_provider_base_url("https://api.openai.com/v1") == "https://api.openai.com/v1"
assert validate_provider_base_url("http://127.0.0.1:11434/v1") == "http://127.0.0.1:11434/v1"
assert validate_provider_base_url("http://[fd00:ec2::255]/v1") == "http://[fd00:ec2::255]/v1"
assert calls == []
@pytest.mark.parametrize(
"url",
[
"http://metadata-alias.attacker.test/latest/meta-data",
"https://metadata-alias.attacker.test/v1",
"http://[email protected]/v1",
"http://metadata-alias.attacker.test./v1",
],
)
def test_dns_alias_of_a_metadata_address_is_refused(url, monkeypatch):
"""A caller-controlled name pointing at the metadata service is metadata."""
monkeypatch.setattr(
socket,
"getaddrinfo",
lambda *a, **k: [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("169.254.169.254", 80))],
)
with pytest.raises(ValueError, match = "metadata"):
validate_provider_base_url(url)
_REAL_GETADDRINFO = socket.getaddrinfo
def _for_test_hosts(fake):
"""Route .example hosts to the fake; other lookups stay real so unrelated threads are not affected."""
def route(host, port, *args, **kwargs):
if isinstance(host, str) and host.endswith(".example"):
return fake(host, port, *args, **kwargs)
return _REAL_GETADDRINFO(host, port, *args, **kwargs)
return route
def test_a_lookup_from_another_thread_is_not_counted(monkeypatch):
"""Other hosts' lookups go to the real resolver, so the fake never counts them."""
import threading
calls = []
def _record(host, port, *args, **kwargs):
calls.append(host)
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))]
monkeypatch.setattr(socket, "getaddrinfo", _for_test_hosts(_record))
other = threading.Thread(target = lambda: socket.getaddrinfo("localhost", 80))
other.start()
other.join(5)
assert validate_provider_base_url("https://route.example/v1") == "https://route.example/v1"
assert calls == ["route.example"]
def test_dns_alias_verdict_is_cached(monkeypatch):
"""Repeat validation of the same host does not re-resolve it."""
calls = []
def _record(host, port, *args, **kwargs):
calls.append(host)
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))]
monkeypatch.setattr(socket, "getaddrinfo", _for_test_hosts(_record))
for _ in range(3):
assert validate_provider_base_url("https://gw.example/v1") == "https://gw.example/v1"
assert len(calls) == 1
def test_the_opt_in_path_shares_the_one_lookup(monkeypatch):
"""Turning the private-address flag on does not double the resolver load."""
calls = []
def _record(host, port, *args, **kwargs):
calls.append(host)
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))]
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
monkeypatch.setattr(socket, "getaddrinfo", _for_test_hosts(_record))
assert validate_provider_base_url("https://gw.example/v1") == "https://gw.example/v1"
assert len(calls) == 1
def test_unresolvable_names_are_refused_only_under_the_opt_in(monkeypatch):
"""Unresolvable names pass by default, since docker-compose names resolve in the client's namespace."""
def _unresolvable(*args, **kwargs):
raise socket.gaierror("not resolvable here")
monkeypatch.setattr(socket, "getaddrinfo", _unresolvable)
assert validate_provider_base_url("http://my_ollama:11434/v1") == "http://my_ollama:11434/v1"
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
with pytest.raises(ValueError, match = "could not be resolved"):
validate_provider_base_url("http://my_ollama:11434/v1")
@pytest.mark.parametrize(
"address",
[
"169.254.3.7",
"169.254.1.1",
"192.168.1.50",
"93.184.216.34",
],
)
def test_a_name_resolving_to_a_non_metadata_address_stays_allowed(address, monkeypatch):
"""Only the metadata services themselves, not the whole link-local range."""
monkeypatch.setattr(
socket,
"getaddrinfo",
lambda *a, **k: [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, 80))],
)
assert validate_provider_base_url("http://box.local:11434/v1") == "http://box.local:11434/v1"
def test_a_link_local_literal_is_still_refused():
"""Typing the address stays refused, which is what main already did."""
with pytest.raises(ValueError, match = "metadata"):
validate_provider_base_url("http://169.254.1.1/v1")
@pytest.mark.parametrize("address", ["169.254.0.23", "169.254.10.10"])
def test_a_dns_alias_of_tencents_metadata_service_is_refused(address, monkeypatch):
"""metadata.tencentyun.com lives on link-local, so it is listed exactly."""
monkeypatch.setattr(
socket,
"getaddrinfo",
lambda *a, **k: [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, 80))],
)
with pytest.raises(ValueError, match = "metadata"):
validate_provider_base_url("http://alias.attacker.test/latest/meta-data")
def test_the_opt_in_path_does_not_re_resolve_after_a_timeout(monkeypatch):
"""One slow host, one deadline, then the unbounded fallback. Not three."""
import time as _time
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
monkeypatch.setattr(_providers, "_DNS_TIMEOUT_SECONDS", 0.05)
calls = []
def _slow(host, port, *args, **kwargs):
calls.append(host)
_time.sleep(0.2)
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))]
monkeypatch.setattr(socket, "getaddrinfo", _for_test_hosts(_slow))
assert validate_provider_base_url("https://slowdns.example/v1") == "https://slowdns.example/v1"
assert len(calls) == 2
def test_an_ascii_host_is_resolved_the_way_httpx_dials_it(monkeypatch):
"""httpx percent-encodes what a reg-name may not hold; so does the lookup."""
seen = []
def _record(host, port, *args, **kwargs):
seen.append(host)
if host == "safe%5Ealias.attacker.test":
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("169.254.169.254", 80))]
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 80))]
monkeypatch.setattr(socket, "getaddrinfo", _record)
with pytest.raises(ValueError, match = "metadata"):
validate_provider_base_url("http://safe^alias.attacker.test/v1")
assert seen == ["safe%5Ealias.attacker.test"]
def test_the_resolved_name_matches_what_httpx_would_dial():
"""Pins _transport_host against httpx itself, for the shapes that differ."""
httpx = pytest.importorskip("httpx")
for host in [
"safe^alias.attacker.test",
"faß.attacker.test",
"例え.テスト",
"API.OpenAI.com",
"my_ollama",
"192.168.1.50",
"gw.example",
]:
dialled = httpx.URL(f"http://{host}/").raw_host.decode("ascii")
assert _providers._transport_host(host) == dialled, host
def test_a_unicode_host_is_resolved_the_way_httpx_dials_it(monkeypatch):
"""getaddrinfo speaks IDNA 2003, httpx IDNA 2008, and they differ on ß."""
seen = []
def _record(host, port, *args, **kwargs):
seen.append(host)
if host == "xn--fa-hia.attacker.test":
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("169.254.169.254", 80))]
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 80))]
monkeypatch.setattr(socket, "getaddrinfo", _record)
with pytest.raises(ValueError, match = "metadata"):
validate_provider_base_url("http://faß.attacker.test/v1")
assert seen == ["xn--fa-hia.attacker.test"]
def test_a_timed_out_lookup_is_not_remembered(monkeypatch):
"""A slow authoritative server cannot buy a 300s window of "safe"."""
import time as _time
monkeypatch.setattr(_providers, "_DNS_TIMEOUT_SECONDS", 0.1)
calls = []
def _slow(host, port, *args, **kwargs):
calls.append(host)
_time.sleep(30)
return []
monkeypatch.setattr(socket, "getaddrinfo", _for_test_hosts(_slow))
for _ in range(2):
assert validate_provider_base_url("http://slow.example/v1") == "http://slow.example/v1"
assert len(calls) == 2
def test_a_resolver_slower_than_the_deadline_still_works_under_the_opt_in(monkeypatch):
"""The opt-in path blocked unboundedly before; a slow answer is not a refusal."""
import time as _time
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
monkeypatch.setattr(_providers, "_DNS_TIMEOUT_SECONDS", 0.05)
def _slow(host, port, *args, **kwargs):
_time.sleep(0.2)
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))]
monkeypatch.setattr(socket, "getaddrinfo", _slow)
assert validate_provider_base_url("https://slowdns.example/v1") == "https://slowdns.example/v1"
def test_a_transient_failure_is_not_remembered(monkeypatch):
"""One SERVFAIL must not refuse the same host for the next 300 seconds."""
attempts = []
def _flaky(host, port, *args, **kwargs):
attempts.append(host)
if len(attempts) == 1:
raise socket.gaierror("temporary failure in name resolution")
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))]
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
monkeypatch.setattr(socket, "getaddrinfo", _flaky)
for _ in range(2):
assert validate_provider_base_url("https://flaky.example/v1") == "https://flaky.example/v1"
assert len(attempts) > 1
def test_stalled_lookups_do_not_pile_up(monkeypatch):
"""Past the in-flight DNS cap the lookup reports no answer rather than spawning another thread."""
import time as _time
monkeypatch.setattr(_providers, "_DNS_TIMEOUT_SECONDS", 0.05)
started = []
def _slow(host, port, *args, **kwargs):
started.append(host)
_time.sleep(30)
return []
monkeypatch.setattr(socket, "getaddrinfo", _for_test_hosts(_slow))
for n in range(_providers._DNS_MAX_IN_FLIGHT + 5):
url = f"http://slow{n}.example/v1"
assert validate_provider_base_url(url) == url
assert len(started) == _providers._DNS_MAX_IN_FLIGHT
def test_a_slow_resolver_does_not_stall_validation(monkeypatch):
"""A resolver that never answers is abandoned, and the URL is allowed."""
import time as _time
monkeypatch.setattr(_providers, "_DNS_TIMEOUT_SECONDS", 0.1)
def _never_answers(*args, **kwargs):
# Return a real empty answer: the abandoned daemon thread wakes later and would raise elsewhere.
_time.sleep(30)
return []
monkeypatch.setattr(socket, "getaddrinfo", _never_answers)
started = _time.monotonic()
assert validate_provider_base_url("http://slow.example/v1") == "http://slow.example/v1"
assert _time.monotonic() - started < 5
@pytest.mark.parametrize(
"url, error",
[
("file:///etc/passwd", "http or https"),
("gopher://example.com/", "http or https"),
("data:text/plain,hi", "http or https"),
("http://exa mple.com/v1", "invalid characters"),
("http://example.com\n/v1", "invalid characters"),
("http://example.com\\@evil.com/v1", "invalid characters"),
("https:///v1", "hostname"),
("", "required"),
(" ", "required"),
],
)
def test_rejected_url_shapes(url, error):
with pytest.raises(ValueError, match = error):
validate_provider_base_url(url)
@pytest.mark.parametrize(
"url",
[
"http://169.254.169.254/latest/meta-data/iam/security-credentials/",
"http://169.254.169.254./latest/meta-data/",
"http://[::ffff:169.254.169.254]/latest/meta-data/",
"http://169.254.170.2/v2/credentials",
"http://169.254.170.23/v1/credentials",
"http://[fd00:ec2::254]/latest/meta-data/",
"http://metadata.google.internal/computeMetadata/v1/",
"http://metadata/computeMetadata/v1/",
"http://100.100.100.200/latest/meta-data/",
"http://[email protected]/latest/meta-data/",
"http://2852039166/latest/meta-data/",
"http://0xA9FEA9FE/latest/meta-data/",
"http://0251.0376.0251.0376/latest/meta-data/",
"http://169.254.43518/latest/meta-data/",
# httpx encodes the host via idna, which splits on all these separators.
"http://169。254。169。254/latest/meta-data/",
"http://169.254.169.254/latest/meta-data/",
"http://169。254。169。254/latest/meta-data/",
"http://169.254.169.254。/latest/meta-data/",
"http://metadata。google。internal/computeMetadata/v1/",
"http://[fd00:0ec2:0000:0000:0000:0000:0000:0254]/latest/meta-data/",
"http://[fd00:ec2::0.0.2.84]/latest/meta-data/",
"http://[FD00:EC2::254]/latest/meta-data/",
"http://[0:0:0:0:0:ffff:a9fe:a9fe]/latest/meta-data/",
"http://[fd20:ce::254]/computeMetadata/v1/",
"http://[fd20:0ce:0:0:0:0:0:254]/computeMetadata/v1/",
"http://[fd00:ec2::254%250]/latest/meta-data/",
"http://[fd00:ec2::254%25eth0]/latest/meta-data/",
],
)
def test_cloud_metadata_endpoints_are_always_refused(url, monkeypatch):
with pytest.raises(ValueError, match = "metadata"):
validate_provider_base_url(url)
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
with pytest.raises(ValueError, match = "metadata"):
validate_provider_base_url(url)
@pytest.mark.parametrize(
"url",
[
"http://127.0.0.1:11434/v1",
"http://localhost:11434/v1",
"http://192.168.1.50:8000/v1",
"http://10.1.2.3:8000/v1",
],
)
def test_private_targets_blocked_only_with_the_opt_in(url, monkeypatch):
assert validate_provider_base_url(url) == url
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
# Names resolve to loopback because conftest blocks real resolution; IP literals skip it.
monkeypatch.setattr(
socket,
"getaddrinfo",
lambda *a, **k: [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 80))],
)
with pytest.raises(ValueError, match = "private address"):
validate_provider_base_url(url)
def test_public_targets_still_allowed_with_the_opt_in(monkeypatch):
monkeypatch.setenv(BLOCK_PRIVATE_ENV, "1")
assert validate_provider_base_url("https://1.1.1.1/v1") == "https://1.1.1.1/v1"
monkeypatch.setattr(
socket,
"getaddrinfo",
lambda *a, **k: [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))],
)
assert validate_provider_base_url("https://api.openai.com/v1") == "https://api.openai.com/v1"