Skip to content

HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.

gateway: an upload destination's address is checked before this server calls it (#429)

github-actions[bot]1c4b1fc1f897
diff --git a/gateway/app.py b/gateway/app.py
index b44e9667..8d707970 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -18144,8 +18144,32 @@ async def _cloud_records() -> dict:
return doc
+async def _cloud_base_refused(base: str) -> str | None:
+ """Why an upload destination's address will not be called, or None: the rule an MCP address
+ follows (_ssrf_check). On a self-hosted box any http(s) host, since the network is the operator's
+ own; on a shared deployment https only, and never an address that resolves to a private, local or
+ metadata range. The test route fetches the address for any caller with a key and returns what it
+ answers, so without this it reached whatever this server can reach (reported privately,
+ GHSA-c5g7-c47h-f6c3)."""
+ from urllib.parse import urlparse
+ try:
+ u = urlparse(str(base or ""))
+ except ValueError:
+ return "invalid url"
+ if not u.hostname:
+ return "url has no host"
+ if _pool_is_local():
+ return None if u.scheme in ("http", "https") else "the address must be http or https"
+ if u.scheme != "https":
+ return "only https addresses are allowed"
+ return await _internal_target(u.hostname, u.port or 443)
+
+
async def _cloud_me(base_url: str, api_key: str) -> dict:
"""Resolve a key to where an upload lands. Raises HTTPException with the hosted side's words."""
+ refused = await _cloud_base_refused(base_url)
+ if refused:
+ raise HTTPException(400, f"that address cannot be used: {refused}")
try:
async with httpx.AsyncClient(timeout=httpx.Timeout(20.0, connect=8.0)) as c:
r = await c.get(f"{base_url}/v1/me", headers={"authorization": f"Bearer {api_key}"})
@@ -18232,6 +18256,9 @@ async def _cloud_upload_one(org: str, hid: str, target: dict, records: dict) ->
v = await _vertex_get(hid)
if not v or str(v.get("org") or "") != org or str(v.get("deleted") or "") in ("1", "true"):
return {"id": hid, "ok": False, "action": "skip", "error": "not found"}
+ refused = await _cloud_base_refused(target.get("base_url") or "")
+ if refused: # a destination stored before the address was checked
+ return {"id": hid, "ok": False, "action": "skip", "error": f"that address cannot be used: {refused}"}
plugins_ok = False
try:
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0, connect=5.0)) as c:
diff --git a/gateway/tests/test_cloud_upload.py b/gateway/tests/test_cloud_upload.py
index 07e260ad..71a4e0bf 100644
--- a/gateway/tests/test_cloud_upload.py
+++ b/gateway/tests/test_cloud_upload.py
@@ -259,3 +259,28 @@ def test_discovery_reports_the_version_this_build_is(api, monkeypatch):
assert api.get("/v1/uhp").json()["implementation"]["version"] == "dev"
monkeypatch.setenv("HR_VERSION", "0.9.1")
assert api.get("/v1/uhp").json()["implementation"]["version"] == "0.9.1"
+
+
+def test_a_destination_on_a_private_network_is_refused_on_a_shared_deployment(api, cloud, monkeypatch):
+ """The test route fetches the destination for any caller with a key and returns what it
+ answers (reported privately). On a shared deployment the rule an MCP address follows applies:
+ https only, and no address that resolves to a private, local or metadata range; nothing is
+ fetched. A self-hosted box's own network stays allowed."""
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: False)
+ for base in ("https://10.0.0.5", "https://169.254.169.254", "https://127.0.0.1:8080", "https://[::1]",
+ "http://cloud.example", "https:///no-host"):
+ for route in ("/v1/cloud-upload/targets/test", "/v1/cloud-upload/targets"):
+ r = api.post(route, json={"api_key": "sk-hr-good", "base_url": base})
+ assert r.status_code == 400 and "cannot be used" in r.text, (base, route, r.status_code, r.text[:200])
+ assert cloud.calls == []
+ assert asyncio.run(gw._cloud_base_refused("https://140.82.112.3")) is None # a public address
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: True)
+ assert asyncio.run(gw._cloud_base_refused("http://10.0.0.5:3000")) is None # the operator's own network
+
+
+def test_a_destination_stored_before_the_check_is_not_called(api, cloud, monkeypatch):
+ hid = _harness(api)
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: False)
+ before = list(cloud.calls)
+ out = asyncio.run(gw._cloud_upload_one(ORG, hid, {"base_url": "https://10.0.0.5", "api_key": "sk-hr-good"}, {}))
+ assert out["ok"] is False and "cannot be used" in out["error"] and cloud.calls == before