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): use one shared loopback set for every outbound gate (#1554)

Akshat Shah8d2180e49ff8
diff --git a/changelog.d/0.75.1/1554.fixed.md b/changelog.d/0.75.1/1554.fixed.md
new file mode 100644
index 00000000..cff3901c
--- /dev/null
+++ b/changelog.d/0.75.1/1554.fixed.md
@@ -0,0 +1,6 @@
+- The outbound loopback allowlist is now a single shared set:
+ ``http://[::1]:<port>`` judge URLs are accepted by ``GateTask.judge_model``,
+ ``soup ship --judge-model``, and the online-DPO trainer's
+ ``_parse_judge_url``, matching the judge API-base and vLLM checks. Each
+ gate previously kept its own ``("localhost", "127.0.0.1")`` literal, which
+ refused the IPv6 loopback (#1548 by @akshatshahh in #1554).
diff --git a/src/soup_cli/commands/ship.py b/src/soup_cli/commands/ship.py
index a5a99026..dc55e525 100644
--- a/src/soup_cli/commands/ship.py
+++ b/src/soup_cli/commands/ship.py
@@ -183,7 +183,7 @@ def _validate_judge_model_url(url: str) -> None:
"""
from urllib.parse import urlparse
- from soup_cli.utils.net_guard import refuse_private_ip_literal
+ from soup_cli.utils.net_guard import LOOPBACK_HOSTS, refuse_private_ip_literal
parsed = urlparse(url)
if parsed.scheme == "ollama":
@@ -194,7 +194,7 @@ def _validate_judge_model_url(url: str) -> None:
except ValueError as exc:
_fail(str(exc), _EXIT_USAGE)
return
- if parsed.scheme == "http" and parsed.hostname in ("localhost", "127.0.0.1"):
+ if parsed.scheme == "http" and parsed.hostname in LOOPBACK_HOSTS:
return
_fail(
f"--judge-model {url!r} uses a disallowed scheme/host; "
diff --git a/src/soup_cli/data/providers/vllm.py b/src/soup_cli/data/providers/vllm.py
index 71299184..eb253a3e 100644
--- a/src/soup_cli/data/providers/vllm.py
+++ b/src/soup_cli/data/providers/vllm.py
@@ -22,7 +22,7 @@ def validate_vllm_url(base_url: str) -> None:
Raises ValueError if validation fails.
"""
- from soup_cli.utils.net_guard import refuse_private_ip_literal
+ from soup_cli.utils.net_guard import LOOPBACK_HOSTS, refuse_private_ip_literal
parsed = urlparse(base_url)
if parsed.scheme not in ("http", "https"):
@@ -32,8 +32,7 @@ def validate_vllm_url(base_url: str) -> None:
# 0.0.0.0 is the bind-any wildcard, NOT loopback (v0.71.6 #232 hardening,
# matching the newer SSRF validators). It drops out of the local set, so
# http://0.0.0.0 is now rejected (remote needs HTTPS).
- local_hosts = ("localhost", "127.0.0.1", "::1")
- is_local = parsed.hostname in local_hosts
+ is_local = parsed.hostname in LOOPBACK_HOSTS
if not is_local and parsed.scheme != "https":
raise ValueError(
f"vLLM URL must use HTTPS for remote servers (got {parsed.scheme}://). "
diff --git a/src/soup_cli/eval/gate.py b/src/soup_cli/eval/gate.py
index 4ae73484..7d71d294 100644
--- a/src/soup_cli/eval/gate.py
+++ b/src/soup_cli/eval/gate.py
@@ -93,6 +93,8 @@ class GateTask(BaseModel):
if value is None:
return None
# Allowlist of schemes — SSRF hardening consistent with the project.
+ from soup_cli.utils.net_guard import LOOPBACK_HOSTS, refuse_private_ip_literal
+
parsed = urlparse(value)
if parsed.scheme == "ollama":
@@ -101,12 +103,10 @@ class GateTask(BaseModel):
if parsed.scheme == "https":
# A private IP literal is refused when the suite is parsed, so a
# suite file cannot name one any more than --judge-model can.
- from soup_cli.utils.net_guard import refuse_private_ip_literal
-
refuse_private_ip_literal(parsed.hostname, label="judge_model URL")
return value
- if (parsed.scheme == "http" and parsed.hostname in {"localhost", "127.0.0.1"}):
+ if parsed.scheme == "http" and parsed.hostname in LOOPBACK_HOSTS:
return value
raise ValueError(
@@ -415,6 +415,10 @@ def _parse_judge_url(judge_model: str) -> tuple[str, str, Optional[str]]:
if parsed.scheme == "ollama":
return ("ollama", judge_model[len("ollama://"):], None)
+ # One loopback set for every outbound gate (#1548): the shared
+ # net_guard.LOOPBACK_HOSTS decides ::1 once, here and in every caller.
+ from soup_cli.utils.net_guard import LOOPBACK_HOSTS
+
if parsed.scheme == "https":
# Only the OpenAI API host is given the OpenAI provider (and so
# OPENAI_API_KEY); any other https judge is an OpenAI-compatible
@@ -425,7 +429,7 @@ def _parse_judge_url(judge_model: str) -> tuple[str, str, Optional[str]]:
default_provider = "server"
elif (
parsed.scheme == "http"
- and parsed.hostname in ("localhost", "127.0.0.1")
+ and parsed.hostname in LOOPBACK_HOSTS
):
default_provider = "server"
else:
diff --git a/src/soup_cli/eval/judge.py b/src/soup_cli/eval/judge.py
index 13d07d5e..7306ec0c 100644
--- a/src/soup_cli/eval/judge.py
+++ b/src/soup_cli/eval/judge.py
@@ -180,7 +180,7 @@ def validate_judge_api_base(api_base: Optional[str]) -> None:
if api_base is None:
return
- from soup_cli.utils.net_guard import refuse_private_ip_literal
+ from soup_cli.utils.net_guard import LOOPBACK_HOSTS, refuse_private_ip_literal
parsed = urlparse(api_base)
if parsed.scheme not in ("http", "https"):
@@ -192,7 +192,7 @@ def validate_judge_api_base(api_base: Optional[str]) -> None:
# Block non-HTTPS for remote URLs (allow HTTP only for localhost)
if parsed.scheme == "http":
hostname = parsed.hostname or ""
- if hostname not in ("localhost", "127.0.0.1", "::1"):
+ if hostname not in LOOPBACK_HOSTS:
raise ValueError(
"HTTP is only allowed for localhost. "
"Use HTTPS for remote URLs."
diff --git a/tests/test_issue1548_loopback_set_is_shared.py b/tests/test_issue1548_loopback_set_is_shared.py
new file mode 100644
index 00000000..3eefe8c1
--- /dev/null
+++ b/tests/test_issue1548_loopback_set_is_shared.py
@@ -0,0 +1,190 @@
+"""#1548 — one loopback set for every outbound gate.
+
+``http://[::1]:8000`` was accepted by the judge API-base and vLLM checks but
+refused by ``GateTask.judge_model`` (``eval/gate.py``), ``soup ship
+--judge-model`` (``commands/ship.py``) and the online-DPO trainer's
+``_parse_judge_url`` — each kept its own ``("localhost", "127.0.0.1")``
+literal that predates the ``::1`` entry of the shared set. ``vllm.py`` and
+``eval/judge.py`` kept a third spelling of the same literal.
+
+Every gate below now imports ``net_guard.LOOPBACK_HOSTS`` instead of
+rebuilding it, so ``::1`` is decided once. The structural tests pin that
+with an AST walk (a comment or message string mentioning ``localhost``
+can't trip them — only a real collection literal of host strings can),
+and the behaviour tests pin ``http://[::1]:<port>`` acceptance per gate.
+
+Mirrors the shape of ``tests/test_issue616_net_guard_is_shared.py``.
+"""
+
+from __future__ import annotations
+
+import ast
+from pathlib import Path
+
+import pytest
+
+_SRC_ROOT = Path(__file__).resolve().parent.parent / "src" / "soup_cli"
+
+# Files that gate an outbound plain-HTTP URL on loopback membership.
+_GATE_FILES = (
+ "eval/gate.py",
+ "commands/ship.py",
+ "data/providers/vllm.py",
+ "eval/judge.py",
+)
+
+# Uses the shared ``_parse_judge_url`` from eval.gate — must not grow its own.
+_TRAINER_FILE = "trainer/online_dpo.py"
+
+_LOOPBACK_STRINGS = frozenset({"localhost", "127.0.0.1", "::1"})
+
+
+def _parse(path: Path) -> ast.Module:
+ return ast.parse(path.read_text(encoding="utf-8"))
+
+
+def _loopback_literals(path: Path) -> list[str]:
+ """Locations of collection literals holding 2+ loopback host strings.
+
+ Catches ``("localhost", "127.0.0.1")``, ``{"localhost", "127.0.0.1",
+ "::1"}``, ``frozenset([...])`` and friends — any shape a rebuilt
+ loopback set actually takes. Message strings such as ``"use ollama://,
+ https://, or http://localhost"`` hold a single host and are ignored.
+ """
+ found: list[str] = []
+ for node in ast.walk(_parse(path)):
+ elts: list[ast.expr] | None = None
+ if isinstance(node, (ast.Tuple, ast.Set, ast.List)):
+ elts = node.elts
+ elif (
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Name)
+ and node.func.id in ("frozenset", "tuple", "set", "list")
+ and node.args
+ and isinstance(node.args[0], (ast.Tuple, ast.Set, ast.List))
+ ):
+ elts = node.args[0].elts
+ if elts is None:
+ continue
+ hosts = {
+ elt.value
+ for elt in elts
+ if isinstance(elt, ast.Constant) and isinstance(elt.value, str)
+ }
+ if len(hosts & _LOOPBACK_STRINGS) >= 2:
+ found.append(f"{path.relative_to(_SRC_ROOT.parent)}:{node.lineno}")
+ return found
+
+
+def _imports_loopback_hosts(path: Path) -> bool:
+ """True if ``path`` really imports LOOPBACK_HOSTS from net_guard."""
+ return any(
+ isinstance(node, ast.ImportFrom)
+ and node.module == "soup_cli.utils.net_guard"
+ and any(alias.name == "LOOPBACK_HOSTS" for alias in node.names)
+ for node in ast.walk(_parse(path))
+ )
+
+
+class TestNoLoopbackLiteralsInGates:
+ @pytest.mark.parametrize("rel", _GATE_FILES)
+ def test_no_rebuilt_loopback_set(self, rel: str):
+ path = _SRC_ROOT / rel
+ assert _loopback_literals(path) == [], (
+ f"{rel} rebuilds the loopback set instead of importing "
+ f"net_guard.LOOPBACK_HOSTS: {_loopback_literals(path)}"
+ )
+
+ @pytest.mark.parametrize("rel", _GATE_FILES)
+ def test_imports_shared_loopback_hosts(self, rel: str):
+ path = _SRC_ROOT / rel
+ assert _imports_loopback_hosts(path), (
+ f"{rel} must import LOOPBACK_HOSTS from soup_cli.utils.net_guard"
+ )
+
+ def test_trainer_uses_shared_parse_judge_url(self):
+ """online_dpo must not define its own _parse_judge_url or literal."""
+ path = _SRC_ROOT / _TRAINER_FILE
+ tree = _parse(path)
+ defined = [
+ node.name
+ for node in ast.walk(tree)
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
+ and node.name == "_parse_judge_url"
+ ]
+ assert defined == [], f"{_TRAINER_FILE} defines its own _parse_judge_url"
+ imported = any(
+ isinstance(node, ast.ImportFrom)
+ and node.module == "soup_cli.eval.gate"
+ and any(alias.name == "_parse_judge_url" for alias in node.names)
+ for node in ast.walk(tree)
+ )
+ assert imported, f"{_TRAINER_FILE} must use eval.gate._parse_judge_url"
+ assert _loopback_literals(path) == []
+
+
+def _gate_task(judge_model: str):
+ from soup_cli.eval.gate import GateTask
+
+ return GateTask(name="t", type="judge", threshold=0.5, judge_model=judge_model)
+
+
+class TestIpv6LoopbackAcceptedEverywhere:
+ """``http://[::1]:<port>`` behaves the same in all gates — accepted."""
+
+ @pytest.mark.parametrize("host", ["localhost", "127.0.0.1", "[::1]"])
+ def test_gate_task_validator(self, host: str):
+ task = _gate_task(f"http://{host}:8000/Qwen2.5")
+ assert task.judge_model == f"http://{host}:8000/Qwen2.5"
+
+ @pytest.mark.parametrize("host", ["localhost", "127.0.0.1", "[::1]"])
+ def test_parse_judge_url(self, host: str):
+ from soup_cli.eval.gate import _parse_judge_url
+
+ assert _parse_judge_url(f"http://{host}:8000/Qwen2.5") == (
+ "server",
+ "Qwen2.5",
+ f"http://{host}:8000",
+ )
+
+ @pytest.mark.parametrize("host", ["localhost", "127.0.0.1", "[::1]"])
+ def test_ship_judge_model_url(self, host: str):
+ from soup_cli.commands.ship import _validate_judge_model_url
+
+ assert _validate_judge_model_url(f"http://{host}:8000/Qwen2.5") is None
+
+ @pytest.mark.parametrize("host", ["localhost", "127.0.0.1", "[::1]"])
+ def test_vllm_url(self, host: str):
+ from soup_cli.data.providers.vllm import validate_vllm_url
+
+ assert validate_vllm_url(f"http://{host}:8000") is None
+
+ @pytest.mark.parametrize("host", ["localhost", "127.0.0.1", "[::1]"])
+ def test_judge_api_base(self, host: str):
+ from soup_cli.eval.judge import validate_judge_api_base
+
+ assert validate_judge_api_base(f"http://{host}:8000") is None
+
+
+class TestGuardsStillRefuse:
+ """Unifying the set must not loosen the SSRF guards."""
+
+ def test_remote_http_still_refused(self):
+ from soup_cli.eval.gate import _parse_judge_url
+
+ with pytest.raises(ValueError, match="unsupported scheme"):
+ _parse_judge_url("http://example.com/model")
+
+ def test_prefix_bypass_still_refused(self):
+ import typer
+
+ from soup_cli.commands.ship import _validate_judge_model_url
+
+ with pytest.raises(typer.Exit):
+ _validate_judge_model_url("http://localhost.attacker.com/model")
+
+ def test_private_ip_literal_still_refused(self):
+ from soup_cli.eval.judge import validate_judge_api_base
+
+ with pytest.raises(ValueError, match="not allowed"):
+ validate_judge_api_base("https://10.0.0.5:8000")
diff --git a/tests/test_outbound_endpoint_ip_literals.py b/tests/test_outbound_endpoint_ip_literals.py
index ee185077..f65d471c 100644
--- a/tests/test_outbound_endpoint_ip_literals.py
+++ b/tests/test_outbound_endpoint_ip_literals.py
@@ -183,6 +183,12 @@ def _online_dpo_trainer(base: str) -> None:
)
+def _ship_judge_model(base: str) -> None:
+ from soup_cli.commands.ship import _validate_judge_model_url
+
+ _validate_judge_model_url(f"{base}/Qwen2.5")
+
+
_VALIDATORS = [
pytest.param(_vllm_url, id="vllm-url"),
pytest.param(_vllm_generate, id="vllm-generate"),
@@ -268,12 +274,13 @@ class TestPublicAndLoopbackStillPass:
"validator",
[
_vllm_url, _vllm_generate, _generate_openai, _generate_server, _judge_api_base,
- _judge_evaluator, _online_dpo_field,
+ _judge_evaluator, _online_dpo_field, _gate_suite_task, _ship_judge_model,
+ _online_dpo_trainer,
],
)
def test_ipv6_loopback_over_http(self, validator, sent):
- """``::1`` is loopback for these gates. The eval-gate suite, ``soup ship`` and
- the online-DPO trainer accept only ``localhost`` and ``127.0.0.1`` over http."""
+ """``::1`` is loopback for every outbound gate: all of them accept it over
+ http through the shared ``LOOPBACK_HOSTS`` set (#1548)."""
try:
validator("http://[::1]:8000")
except _RequestAttemptedError: