Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(net_guard): say what to do in the private-address refusal (#1582)

Pranav Deshpandef3d58881b7d2
diff --git a/changelog.d/0.75.1/1582.changed.md b/changelog.d/0.75.1/1582.changed.md
new file mode 100644
index 00000000..636f0597
--- /dev/null
+++ b/changelog.d/0.75.1/1582.changed.md
@@ -0,0 +1,5 @@
+- Changed the private-address refusal on outbound endpoints to say what to do: it now
+ ends with "address the server by its hostname". `http://0.0.0.0` in `soup data generate
+ --api-base` and the chat proxy now says "0.0.0.0 is ambiguous; use 127.0.0.1 or
+ localhost", the same hint `HF_ENDPOINT`, hub endpoints and webhook URLs give, from one
+ shared constant (#1549 by @PranavD2905 in #1582).
diff --git a/src/soup_cli/commands/generate.py b/src/soup_cli/commands/generate.py
index 2333448d..6a3f3cbd 100644
--- a/src/soup_cli/commands/generate.py
+++ b/src/soup_cli/commands/generate.py
@@ -715,11 +715,17 @@ def _generate_openai(
if api_base:
from urllib.parse import urlparse
- from soup_cli.utils.net_guard import LOOPBACK_HOSTS, refuse_private_ip_literal
+ from soup_cli.utils.net_guard import (
+ LOOPBACK_HOSTS,
+ UNSPECIFIED_HOST_HINT,
+ refuse_private_ip_literal,
+ )
parsed = urlparse(api_base)
is_local = parsed.hostname in LOOPBACK_HOSTS
if not is_local and parsed.scheme != "https":
+ if parsed.hostname == "0.0.0.0":
+ raise ValueError(f"api_base {UNSPECIFIED_HOST_HINT}")
raise ValueError(
f"api_base must use HTTPS for remote APIs (got {parsed.scheme}://). "
"HTTP is only allowed for localhost."
@@ -858,7 +864,11 @@ def _generate_server(
if api_base:
from urllib.parse import urlparse
- from soup_cli.utils.net_guard import LOOPBACK_HOSTS, refuse_private_ip_literal
+ from soup_cli.utils.net_guard import (
+ LOOPBACK_HOSTS,
+ UNSPECIFIED_HOST_HINT,
+ refuse_private_ip_literal,
+ )
parsed = urlparse(api_base)
if parsed.scheme not in ("http", "https"):
@@ -867,6 +877,8 @@ def _generate_server(
)
is_local = parsed.hostname in LOOPBACK_HOSTS
if not is_local and parsed.scheme != "https":
+ if parsed.hostname == "0.0.0.0":
+ raise ValueError(f"api_base {UNSPECIFIED_HOST_HINT}")
raise ValueError(
f"api_base must use HTTPS for remote APIs (got {parsed.scheme}://). "
"HTTP is only allowed for localhost."
diff --git a/src/soup_cli/ui/app.py b/src/soup_cli/ui/app.py
index ab202688..ae438441 100644
--- a/src/soup_cli/ui/app.py
+++ b/src/soup_cli/ui/app.py
@@ -1130,7 +1130,7 @@ def create_app(host: str = "127.0.0.1", port: int = 7860):
from fastapi.responses import StreamingResponse
- from soup_cli.utils.net_guard import refuse_private_ip_literal
+ from soup_cli.utils.net_guard import UNSPECIFIED_HOST_HINT, refuse_private_ip_literal
# Validate messages
if not req.messages:
@@ -1156,6 +1156,10 @@ def create_app(host: str = "127.0.0.1", port: int = 7860):
is_local = addr.is_loopback
except ValueError:
is_local = False
+ if not is_local and host == "0.0.0.0":
+ raise HTTPException(
+ status_code=400, detail=f"endpoint {UNSPECIFIED_HOST_HINT}"
+ )
if not is_local:
raise HTTPException(
status_code=400,
diff --git a/src/soup_cli/utils/hf.py b/src/soup_cli/utils/hf.py
index 942cc3df..19ed840c 100644
--- a/src/soup_cli/utils/hf.py
+++ b/src/soup_cli/utils/hf.py
@@ -23,6 +23,7 @@ from typing import Any, Optional
from urllib.parse import urlparse
from soup_cli.utils.net_guard import LOOPBACK_HOSTS as _LOOPBACK_HOSTS
+from soup_cli.utils.net_guard import UNSPECIFIED_HOST_HINT
from soup_cli.utils.net_guard import is_private_or_link_local as _is_private_or_link_local
DEFAULT_ENDPOINT = "https://huggingface.co"
@@ -100,9 +101,7 @@ def resolve_endpoint() -> str:
host = parsed.hostname or ""
if host == "0.0.0.0":
- raise ValueError(
- "HF_ENDPOINT 0.0.0.0 is ambiguous; use 127.0.0.1 or localhost"
- )
+ raise ValueError(f"HF_ENDPOINT {UNSPECIFIED_HOST_HINT}")
host_clean = host.lower().rstrip(".")
if parsed.scheme == "http" and host_clean not in _LOOPBACK_HOSTS:
# Reject plain HTTP for RFC1918 / link-local / cloud metadata too —
diff --git a/src/soup_cli/utils/hubs.py b/src/soup_cli/utils/hubs.py
index 8c4c038f..6913a953 100644
--- a/src/soup_cli/utils/hubs.py
+++ b/src/soup_cli/utils/hubs.py
@@ -26,6 +26,7 @@ from typing import Callable, Mapping, Optional, Tuple
from urllib.parse import urlparse
from soup_cli.utils.net_guard import LOOPBACK_HOSTS as _LOOPBACK_HOSTS
+from soup_cli.utils.net_guard import UNSPECIFIED_HOST_HINT
from soup_cli.utils.net_guard import is_private_or_link_local as _is_private_or_link_local
_LOG = logging.getLogger(__name__)
@@ -180,9 +181,7 @@ def validate_hub_endpoint(endpoint: str, *, hub: str | None = None) -> str:
host = parsed.hostname or ""
if host == "0.0.0.0":
- raise ValueError(
- f"{label} 0.0.0.0 is ambiguous; use 127.0.0.1 or localhost"
- )
+ raise ValueError(f"{label} {UNSPECIFIED_HOST_HINT}")
host_clean = host.lower().rstrip(".")
if parsed.scheme == "http" and host_clean not in _LOOPBACK_HOSTS:
if _is_private_or_link_local(host):
diff --git a/src/soup_cli/utils/net_guard.py b/src/soup_cli/utils/net_guard.py
index 6ecf79dd..dbcf5df3 100644
--- a/src/soup_cli/utils/net_guard.py
+++ b/src/soup_cli/utils/net_guard.py
@@ -45,6 +45,10 @@ import ipaddress
# Loopback hosts that may legitimately use plain HTTP (dev / self-hosted).
LOOPBACK_HOSTS = frozenset({"localhost", "127.0.0.1", "::1"})
+# #1549 — the one wording for a URL that names the bind-any wildcard. Callers
+# prefix it with the setting's name.
+UNSPECIFIED_HOST_HINT = "0.0.0.0 is ambiguous; use 127.0.0.1 or localhost"
+
def _ascii_spellings(host: str) -> list[str]:
"""``host`` plus the ASCII forms an HTTP client may turn it into.
@@ -171,5 +175,6 @@ def refuse_private_ip_literal(host: str | None, *, label: str) -> None:
return
if is_private_or_link_local(clean):
raise ValueError(
- f"{label}: private/link-local/reserved IP hosts are not allowed (SSRF protection)"
+ f"{label}: private/link-local/reserved IP hosts are not allowed (SSRF protection); "
+ "address the server by its hostname"
)
diff --git a/src/soup_cli/utils/webhooks.py b/src/soup_cli/utils/webhooks.py
index d60c8287..9f0fe4b2 100644
--- a/src/soup_cli/utils/webhooks.py
+++ b/src/soup_cli/utils/webhooks.py
@@ -24,6 +24,7 @@ from typing import List, Mapping, Optional, Tuple
from urllib.parse import urlparse
from soup_cli.utils.net_guard import LOOPBACK_HOSTS as _LOOPBACK_HOSTS
+from soup_cli.utils.net_guard import UNSPECIFIED_HOST_HINT
from soup_cli.utils.net_guard import is_private_or_link_local as _is_private_or_link_local
_MAX_WEBHOOK_URL_LEN = 4096
@@ -63,9 +64,7 @@ def validate_webhook_url(url: object, *, allow_private_hosts: bool = False) -> s
raise ValueError("webhook URL is missing a host")
host = parsed.hostname or ""
if host == "0.0.0.0":
- raise ValueError(
- "webhook URL 0.0.0.0 is ambiguous; use 127.0.0.1 or localhost"
- )
+ raise ValueError(f"webhook URL {UNSPECIFIED_HOST_HINT}")
# SSRF gate — runs for BOTH http and https. Nesting this inside the
# http-only branch (the pre-fix bug) let ``https://169.254.169.254`` and
# any ``https://10.x`` / ``192.168.x`` sail straight through to the return.
diff --git a/tests/test_outbound_endpoint_ip_literals.py b/tests/test_outbound_endpoint_ip_literals.py
index f65d471c..690c7b2c 100644
--- a/tests/test_outbound_endpoint_ip_literals.py
+++ b/tests/test_outbound_endpoint_ip_literals.py
@@ -24,6 +24,9 @@ from types import SimpleNamespace
import pytest
_REFUSED = "private/link-local/reserved IP hosts are not allowed"
+# #1549: the refusal says what to do instead.
+_REMEDY = "address the server by its hostname"
+_UNSPECIFIED_HINT = "0.0.0.0 is ambiguous; use 127.0.0.1 or localhost"
# The host part of the URL, as written in its authority.
_NON_PUBLIC_HOSTS = [
@@ -333,10 +336,11 @@ class TestRemoteHttpKeepsItsMessage:
assert sent == []
@pytest.mark.parametrize("validator", [_generate_openai, _generate_server])
- def test_unspecified_address_is_remote_to_the_scheme_check(self, validator, sent):
- """0.0.0.0 is the bind-any wildcard; these two used to count it as loopback."""
- with pytest.raises(ValueError, match="HTTPS for remote"):
+ def test_unspecified_address_over_http_names_the_fix(self, validator, sent):
+ """0.0.0.0 is the bind-any wildcard; #1549 says to use loopback instead."""
+ with pytest.raises(ValueError) as info:
validator("http://0.0.0.0:8000")
+ assert str(info.value) == f"api_base {_UNSPECIFIED_HINT}"
assert sent == []
@@ -424,7 +428,7 @@ class TestChatProxy:
resp = post(f"https://{host}")
assert resp.status_code == 400, resp.text
detail = resp.json()["detail"]
- assert detail == f"endpoint: {_REFUSED} (SSRF protection)"
+ assert detail == f"endpoint: {_REFUSED} (SSRF protection); {_REMEDY}"
assert sent == []
@pytest.mark.parametrize("host", _ALLOWED_HOSTS)
@@ -434,10 +438,10 @@ class TestChatProxy:
assert [url for url, _ in sent] == [f"https://{host}/v1/chat/completions"]
def test_unspecified_address_over_http_is_refused(self, post, sent):
- """0.0.0.0 is the bind-any wildcard; the scheme check used to count it as local."""
+ """0.0.0.0 is the bind-any wildcard; #1549 says to use loopback instead."""
resp = post("http://0.0.0.0:8000")
assert resp.status_code == 400, resp.text
- assert resp.json()["detail"] == "HTTP only allowed for localhost endpoints"
+ assert resp.json()["detail"] == f"endpoint {_UNSPECIFIED_HINT}"
assert sent == []
def test_loopback_range_over_http_is_dispatched(self, post, sent):
@@ -578,7 +582,26 @@ class TestTheSharedHelper:
with pytest.raises(ValueError) as info:
refuse_private_ip_literal(host, label="vLLM URL")
- assert str(info.value) == f"vLLM URL: {_REFUSED} (SSRF protection)"
+ assert str(info.value) == f"vLLM URL: {_REFUSED} (SSRF protection); {_REMEDY}"
+
+ def test_the_unspecified_hint_is_one_constant(self, monkeypatch):
+ """#1549: every 0.0.0.0 refusal reuses the one hint in ``net_guard``."""
+ from soup_cli.utils import hf
+ from soup_cli.utils.hubs import validate_hub_endpoint
+ from soup_cli.utils.net_guard import UNSPECIFIED_HOST_HINT
+ from soup_cli.utils.webhooks import validate_webhook_url
+
+ assert UNSPECIFIED_HOST_HINT == _UNSPECIFIED_HINT
+ monkeypatch.setenv("HF_ENDPOINT", "http://0.0.0.0:8080")
+ with pytest.raises(ValueError) as info:
+ hf.resolve_endpoint()
+ assert str(info.value) == f"HF_ENDPOINT {UNSPECIFIED_HOST_HINT}"
+ with pytest.raises(ValueError) as info:
+ validate_webhook_url("http://0.0.0.0:8080/hook")
+ assert str(info.value) == f"webhook URL {UNSPECIFIED_HOST_HINT}"
+ with pytest.raises(ValueError) as info:
+ validate_hub_endpoint("http://0.0.0.0:8080", hub="modelscope")
+ assert str(info.value) == f"modelscope {UNSPECIFIED_HOST_HINT}"
def test_mapped_loopback_does_not_depend_on_the_interpreter(self, monkeypatch):
"""Whether ``IPv6Address.is_loopback`` looks through an IPv4-mapped address