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.

A checked address is the one connected to (#432)

github-actions[bot]d5ea34c5d8fe
diff --git a/gateway/app.py b/gateway/app.py
index e0091865..f22179ba 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -37,6 +37,7 @@ import subprocess
import tarfile
import tempfile
+import httpcore
import httpx
import redis.asyncio as aioredis
from fastapi import Depends, FastAPI, File, Form, Header, HTTPException, Request, Response, UploadFile
@@ -8706,7 +8707,7 @@ async def _start_retention_sweep() -> None:
n = {k: len(v) for k, v in res["removed"].items() if v}
if n or res["left"]:
print(f"[retention] removed {n or 'nothing'}"
- + (f"; counted from the last write: {res['no_delete_time']}" if res["no_delete_time"] else "")
+ + (f"; deleted before delete times were kept: {res['no_delete_time']}" if res["no_delete_time"] else "")
+ (f"; left: {res['left']}" if res["left"] else ""), flush=True)
except Exception as e: # noqa: BLE001
print(f"[retention] sweep failed: {type(e).__name__}", flush=True)
@@ -10990,21 +10991,115 @@ async def _internal_target(host: str, port: int) -> str | None:
1.01 s lookup let ZERO other tasks run. Every provider file URL is classified on every poll, so
one relay naming a slow-resolving host froze the gateway for everybody, ten seconds apart.
"""
- loop = asyncio.get_running_loop()
+ return (await _classify_target(host, port))[1]
+
+
+def _ip_internal(addr: str) -> bool:
+ ip = ipaddress.ip_address(addr.split("%", 1)[0])
+ return bool(ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved
+ or ip.is_multicast or ip.is_unspecified
+ or any(ip in net for net in _INTERNAL_NETS if ip.version == net.version))
+
+
+async def _resolve(host: str, port: int) -> list:
+ """The one DNS lookup every check makes (a test answers it differently each time)."""
+ return await asyncio.get_running_loop().getaddrinfo(host, port, proto=socket.IPPROTO_TCP)
+
+
+async def _classify_target(host: str, port: int) -> tuple[list[str], str | None]:
+ """Resolve a name once and classify every answer: (the addresses, None) when all are outside,
+ ([], why) when any is inside or the lookup failed. The addresses are what a connection must
+ then use (`_CheckedNetwork`), so the name is never resolved a second time between the check
+ and the socket."""
try:
- infos = await loop.getaddrinfo(host, port, proto=socket.IPPROTO_TCP)
+ infos = await _resolve(host, port)
except socket.gaierror as e:
no_such = {socket.EAI_NONAME, getattr(socket, "EAI_NODATA", socket.EAI_NONAME)}
- return "host does not resolve" if e.errno in no_such else "host could not be resolved"
+ return [], ("host does not resolve" if e.errno in no_such else "host could not be resolved")
except Exception: # noqa: BLE001
- return "host could not be resolved"
+ return [], "host could not be resolved"
+ addrs: list[str] = []
for info in infos:
- ip = ipaddress.ip_address(info[4][0])
- if (ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved
- or ip.is_multicast or ip.is_unspecified
- or any(ip in net for net in _INTERNAL_NETS if ip.version == net.version)):
- return "target resolves to a disallowed (internal) address"
- return None
+ if _ip_internal(info[4][0]):
+ return [], "target resolves to a disallowed (internal) address"
+ if info[4][0] not in addrs:
+ addrs.append(info[4][0])
+ return addrs, None
+
+
+class _CheckedNetwork(httpcore.AsyncNetworkBackend):
+ """Opens a socket only to an address it classified itself, in the same step.
+
+ `_internal_target` answers for a name, and the client then resolved the name again when it
+ opened the socket: a name that answers a public address to the check and an internal one a
+ moment later (a DNS rebind) passed. Here the name is resolved once, every answer is classified,
+ and the socket goes to one of those very addresses. TLS still verifies the certificate against
+ the name, since httpcore takes the server name from the request, not from this socket. Redirects
+ are not followed by these clients, and if one ever were, its connection would come through here
+ as well.
+
+ `always`: classify on every deployment (a provider's file address), or only on one shared by
+ several organizations (an address a member typed: MCP servers, upload destinations), where a
+ self-hosted box's own network is not refused."""
+
+ def __init__(self, always: bool) -> None:
+ self._inner = httpcore.AnyIOBackend()
+ self._always = always
+
+ async def connect_tcp(self, host, port, timeout=None, local_address=None, socket_options=None):
+ if not (self._always or not _pool_is_local()):
+ return await self._inner.connect_tcp(host, port, timeout=timeout, local_address=local_address,
+ socket_options=socket_options)
+ addrs, why = await _classify_target(host, port)
+ if why:
+ raise httpcore.ConnectError(f"that address cannot be used: {why}")
+ last: Exception | None = None
+ for addr in addrs:
+ try:
+ return await self._inner.connect_tcp(addr, port, timeout=timeout, local_address=local_address,
+ socket_options=socket_options)
+ except (httpcore.ConnectError, httpcore.ConnectTimeout) as e:
+ last = e
+ raise last or httpcore.ConnectError("no address to connect to")
+
+ async def connect_unix_socket(self, path, timeout=None, socket_options=None):
+ raise httpcore.ConnectError("a local socket is not a network address")
+
+ async def sleep(self, seconds: float) -> None:
+ await self._inner.sleep(seconds)
+
+
+async def _sql_peer_check(addr: str) -> str | None:
+ """sql_plane.PEER_CHECK: on a shared deployment, a database connection that reached a private,
+ local or metadata address is closed before any query. A self-hosted box's own database is the
+ normal case there, as it is for the check made when the connection is saved."""
+ if _pool_is_local():
+ return None
+ try:
+ return "it is on a private, local or metadata network" if _ip_internal(addr) else None
+ except ValueError:
+ return "the address it reached is not a network address"
+
+
+sql_plane.PEER_CHECK = _sql_peer_check
+
+_checked_clients: dict[bool, httpx.AsyncClient] = {}
+
+
+def _checked_http(always: bool = False) -> httpx.AsyncClient:
+ """The client for an address somebody else named. Its pool opens connections through
+ `_CheckedNetwork`; httpx 0.28 takes no network backend, so the pool it built is handed one.
+ No environment proxies (a transport given explicitly turns them off): a proxy would resolve
+ the name itself, and the check would be about the proxy."""
+ c = _checked_clients.get(always)
+ if c is None:
+ transport = httpx.AsyncHTTPTransport()
+ transport._pool._network_backend = _CheckedNetwork(always)
+ c = _checked_clients[always] = httpx.AsyncClient(
+ transport=transport, follow_redirects=False,
+ timeout=httpx.Timeout(connect=30, read=600, write=120, pool=60),
+ limits=httpx.Limits(max_connections=200, max_keepalive_connections=40))
+ return c
async def _mcp_list_tools(url: str, token: str) -> dict:
@@ -11017,7 +11112,7 @@ async def _mcp_list_tools(url: str, token: str) -> dict:
headers = {"content-type": "application/json", "accept": "application/json, text/event-stream"}
if token:
headers["authorization"] = token if token.lower().startswith("bearer ") else f"Bearer {token}"
- c = _client()
+ c = _checked_http()
init = {"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {"protocolVersion": "2024-11-05", "capabilities": {},
"clientInfo": {"name": "harnessrouter", "version": "1"}}}
@@ -11993,6 +12088,12 @@ def _media_client() -> httpx.AsyncClient:
return _client()
+def _media_fetch_client() -> httpx.AsyncClient:
+ """The client a finished file is fetched with: the address is the provider's to name, so the
+ connection goes only to an address classified in the same step, on every deployment."""
+ return _checked_http(always=True)
+
+
async def _media_providers() -> dict[str, dict]:
"""{provider: {api_key, base_url, auth}} for every catalog provider this deployment can reach.
@@ -12594,7 +12695,7 @@ async def _media_fetch(url: str) -> bytes:
raise media_plane.MediaEmpty(refused)
buf = bytearray()
try:
- async with _media_client().stream("GET", url, timeout=300) as r:
+ async with _media_fetch_client().stream("GET", url, timeout=300) as r:
if r.status_code >= 400:
raise media_plane.MediaEmpty(f"the finished file could not be fetched "
f"(HTTP {r.status_code})")
@@ -17269,13 +17370,15 @@ class EnvironmentImportBody(BaseModel):
replace: bool = False
-async def _git_url_refused(url: str) -> str | None:
- """Why this server will not clone from that address, or None: the rule an MCP address follows
- (_ssrf_check). A self-hosted box's private network is its operator's own and is allowed; on a
- shared deployment an address that resolves to a private, local or metadata range is refused,
- since the clone runs on this side (reported privately, GHSA-mw9j-m5jf-5r56)."""
+async def _git_url_refused(url: str) -> tuple[str | None, str]:
+ """Why this server will not clone from that address (or None), and the address the clone must
+ connect to: the rule an MCP address follows (_ssrf_check). A self-hosted box's private network
+ is its operator's own and is allowed, with no pin; on a shared deployment an address that
+ resolves to a private, local or metadata range is refused, since the clone runs on this side
+ (reported privately, GHSA-mw9j-m5jf-5r56), and the clone is pinned to an address classified
+ here as "<host>:<port>:<address>", so the name is not resolved again when git connects."""
if _pool_is_local():
- return None
+ return None, ""
from urllib.parse import urlparse
u = str(url or "")
if u.startswith("git@"):
@@ -17285,10 +17388,11 @@ async def _git_url_refused(url: str) -> str | None:
p = urlparse(u)
host, port = p.hostname or "", p.port or {"http": 80, "ssh": 22}.get(p.scheme, 443)
except ValueError:
- return "invalid url"
+ return "invalid url", ""
if not host:
- return "url has no host"
- return await _internal_target(host, port)
+ return "url has no host", ""
+ addrs, why = await _classify_target(host, port)
+ return (why, "") if why else (None, f"{host}:{port}:{addrs[0]}")
@app.post("/v1/environments/{env_id}/import")
@@ -17302,12 +17406,12 @@ async def environment_import(env_id: str, request: Request, replace: int = 0) ->
git = body.git or {}
if not str(git.get("url") or ""):
raise uhp_error(422, "environment_invalid", "Name a git url, or send an archive as the body.", "git.url")
- refused = await _git_url_refused(str(git.get("url")))
+ refused, pin = await _git_url_refused(str(git.get("url")))
if refused:
raise uhp_error(422, "environment_invalid", f"That git address cannot be used: {refused}.", "git.url")
r = await _env_runner("POST", f"/environments/{env_id}/import", env_id, timeout=900.0,
params={"replace": int(bool(replace or body.replace)), "git_url": str(git.get("url")),
- "git_ref": str(git.get("ref") or "")}, content=b"")
+ "git_ref": str(git.get("ref") or ""), "git_pin": pin}, content=b"")
else:
declared = int(request.headers.get("content-length") or 0)
if declared > _ENV_IMPORT_MAX:
@@ -18644,8 +18748,8 @@ async def _cloud_me(base_url: str, api_key: str) -> dict:
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}"})
+ r = await _checked_http().get(f"{base_url}/v1/me", headers={"authorization": f"Bearer {api_key}"},
+ timeout=httpx.Timeout(20.0, connect=8.0))
except Exception as e: # noqa: BLE001
raise HTTPException(502, f"could not reach {base_url}: {str(e)[:120]}")
if r.status_code == 401:
@@ -18734,9 +18838,9 @@ async def _cloud_upload_one(org: str, hid: str, target: dict, records: dict) ->
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:
- d = (await c.get(f"{target['base_url']}/v1/uhp")).json()
- plugins_ok = bool((d.get("capabilities") or {}).get("plugins"))
+ d = (await _checked_http().get(f"{target['base_url']}/v1/uhp",
+ timeout=httpx.Timeout(10.0, connect=5.0))).json()
+ plugins_ok = bool((d.get("capabilities") or {}).get("plugins"))
except Exception: # noqa: BLE001
plugins_ok = False
body = await _cloud_harness_body(org, hid, v, plugins_ok)
@@ -18764,9 +18868,9 @@ async def _cloud_upload_one(org: str, hid: str, target: dict, records: dict) ->
last_error = ""
for attempt in (remote_id, "chrn" + uuid.uuid4().hex):
try:
- async with httpx.AsyncClient(timeout=httpx.Timeout(60.0, connect=8.0)) as c:
- r = await c.put(f"{target['base_url']}/v1/harnesses/{attempt}", json=body,
- headers={"authorization": f"Bearer {target['api_key']}"})
+ r = await _checked_http().put(f"{target['base_url']}/v1/harnesses/{attempt}", json=body,
+ headers={"authorization": f"Bearer {target['api_key']}"},
+ timeout=httpx.Timeout(60.0, connect=8.0))
except Exception as e: # noqa: BLE001
return {"id": hid, "ok": False, "action": action, "error": f"could not reach cloud: {str(e)[:120]}"}
if r.status_code in (200, 201):
diff --git a/gateway/requirements.txt b/gateway/requirements.txt
index 5febb308..dfb6775f 100644
--- a/gateway/requirements.txt
+++ b/gateway/requirements.txt
@@ -1,6 +1,7 @@
fastapi==0.115.6
uvicorn[standard]==0.34.0
httpx==0.28.1
+httpcore==1.0.9
pydantic==2.11.9
python-multipart==0.0.20
azure-identity==1.19.0
diff --git a/gateway/sql_plane.py b/gateway/sql_plane.py
index 7d8609be..f99b5a22 100644
--- a/gateway/sql_plane.py
+++ b/gateway/sql_plane.py
@@ -149,6 +149,34 @@ def _jsonable(v):
return str(v)
+# Set by the gateway on a deployment shared by several organizations: given the address a
+# connection actually reached, why it must not be used, or None. The host was checked when the
+# connection was saved, by name; a name can answer differently by the time a query runs (a DNS
+# change, or a rebind), so the address is checked again where it is a fact, on the open socket,
+# before any query is sent.
+PEER_CHECK = None
+
+
+def _peer_of(conn) -> str:
+ """The address a driver's connection reached: asyncpg keeps its transport, aiomysql its
+ stream writer. Empty when neither answers (a local socket, or a driver that changed)."""
+ for holder in (getattr(conn, "_transport", None), getattr(conn, "_writer", None)):
+ if holder is not None:
+ peer = holder.get_extra_info("peername")
+ if isinstance(peer, (tuple, list)) and peer:
+ return str(peer[0])
+ return ""
+
+
+async def _vet_peer(conn) -> None:
+ if PEER_CHECK is None:
+ return
+ addr = _peer_of(conn)
+ why = await PEER_CHECK(addr) if addr else "the address it reached could not be read"
+ if why:
+ raise SqlError(f"This server cannot use that database: {why}.")
+
+
async def _pg_query(dsn: str, sql: str, timeout: float, max_rows: int) -> dict:
try:
import asyncpg
@@ -157,6 +185,7 @@ async def _pg_query(dsn: str, sql: str, timeout: float, max_rows: int) -> dict:
conn = None
try:
conn = await asyncio.wait_for(asyncpg.connect(dsn), timeout=timeout)
+ await _vet_peer(conn)
# A read-only transaction: the gate above is a parser, this is the database's own opinion.
# Belt and braces, and this one cannot be fooled by clever text.
async with conn.transaction(readonly=True):
@@ -183,6 +212,7 @@ async def _mysql_query(dsn: str, sql: str, timeout: float, max_rows: int) -> dic
conn = None
try:
conn = await asyncio.wait_for(aiomysql.connect(**parsed), timeout=timeout)
+ await _vet_peer(conn)
async with conn.cursor() as cur:
# MySQL has no read-only transaction that survives a plain connection cleanly, so the
# session is set read-only where the server supports it and the parser carries the
diff --git a/gateway/tests/test_cloud_upload.py b/gateway/tests/test_cloud_upload.py
index 71a4e0bf..b023d11a 100644
--- a/gateway/tests/test_cloud_upload.py
+++ b/gateway/tests/test_cloud_upload.py
@@ -49,9 +49,10 @@ def cloud(monkeypatch):
class Patched(real):
def __init__(self, *a, **kw):
- kw["transport"] = httpx.MockTransport(c.handle)
+ kw["transport"] = httpx.MockTransport(lambda r: c.handle(r)) # a test may swap the handler
super().__init__(*a, **kw)
monkeypatch.setattr(gw.httpx, "AsyncClient", Patched)
+ monkeypatch.setattr(gw, "_checked_clients", {}) # built afresh, so on this test's cloud
return c
diff --git a/gateway/tests/test_environments.py b/gateway/tests/test_environments.py
index 2933b12e..568dfb79 100644
--- a/gateway/tests/test_environments.py
+++ b/gateway/tests/test_environments.py
@@ -287,6 +287,8 @@ def test_a_git_address_on_a_private_network_is_refused_on_a_shared_deployment(ap
r = api.post(f"/v1/environments/{eid}/import", json={"git": {"url": url}})
assert r.status_code == 422 and "cannot be used" in r.text, (url, r.text)
assert forwarded == []
- assert __import__("asyncio").run(app._git_url_refused("https://140.82.112.3/team/repo.git")) is None # a public address
+ # a public address passes, pinned to the address it was classified as
+ assert __import__("asyncio").run(app._git_url_refused("https://140.82.112.3/team/repo.git")) == \
+ (None, "140.82.112.3:443:140.82.112.3")
monkeypatch.setattr(app, "_pool_is_local", lambda: True)
- assert __import__("asyncio").run(app._git_url_refused("https://10.0.0.5/team/repo.git")) is None
+ assert __import__("asyncio").run(app._git_url_refused("https://10.0.0.5/team/repo.git")) == (None, "")
diff --git a/gateway/tests/test_media_attack.py b/gateway/tests/test_media_attack.py
index 46316da0..483f7353 100644
--- a/gateway/tests/test_media_attack.py
+++ b/gateway/tests/test_media_attack.py
@@ -182,6 +182,7 @@ def hostile(monkeypatch):
h = Hostile()
cl = httpx.AsyncClient(transport=httpx.MockTransport(lambda r: h.handle(r)), timeout=30)
monkeypatch.setattr(app, "_media_client", lambda: cl)
+ monkeypatch.setattr(app, "_media_fetch_client", lambda: cl)
app._media_quarantine.clear()
yield h
asyncio.run(cl.aclose())
diff --git a/gateway/tests/test_media_mcp.py b/gateway/tests/test_media_mcp.py
index a880fd39..a48117c4 100644
--- a/gateway/tests/test_media_mcp.py
+++ b/gateway/tests/test_media_mcp.py
@@ -350,6 +350,7 @@ def provider(monkeypatch):
client = httpx.AsyncClient(transport=httpx.MockTransport(lambda r: p.handle(r)),
timeout=30)
monkeypatch.setattr(app, "_media_client", lambda: client)
+ monkeypatch.setattr(app, "_media_fetch_client", lambda: client)
app._media_quarantine.clear()
yield p
asyncio.run(client.aclose())
diff --git a/gateway/tests/test_pinned_address.py b/gateway/tests/test_pinned_address.py
new file mode 100644
index 00000000..01d97ac6
--- /dev/null
+++ b/gateway/tests/test_pinned_address.py
@@ -0,0 +1,171 @@
+"""An address somebody else names is connected to only as it was classified.
+
+The check resolved the name and the client resolved it again when it opened the socket, so a name
+that answered a public address to the check and an internal one a moment later (a DNS rebind)
+reached the internal one. `_CheckedNetwork` resolves once, classifies every answer and connects to
+one of those very addresses. These tests answer the lookup themselves, differently each time.
+"""
+import asyncio
+import pathlib
+import socket
+import sys
+
+import httpx
+import pytest
+
+sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
+import app as gw # noqa: E402
+
+
+async def _server():
+ """A plain HTTP server on 127.0.0.1 that counts the connections it accepts."""
+ seen = {"connections": 0, "hosts": []}
+
+ async def handle(reader, writer):
+ seen["connections"] += 1
+ head = (await reader.readuntil(b"\r\n\r\n")).decode()
+ seen["hosts"] += [ln.split(":", 1)[1].strip() for ln in head.split("\r\n") if ln.lower().startswith("host:")]
+ writer.write(b"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: 11\r\n"
+ b"connection: close\r\n\r\n{\"ok\":true}")
+ await writer.drain()
+ writer.close()
+ srv = await asyncio.start_server(handle, "127.0.0.1", 0)
+ return srv, srv.sockets[0].getsockname()[1], seen
+
+
+def _answers(*addrs):
+ """A resolver that answers each call with the next address, and counts the calls."""
+ calls = []
+
+ async def resolve(host, port):
+ calls.append(host)
+ addr = addrs[min(len(calls), len(addrs)) - 1]
+ return [(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", (addr, port))]
+ return resolve, calls
+
+
[email protected](autouse=True)
+def fresh(monkeypatch):
+ monkeypatch.setattr(gw, "_checked_clients", {})
+
+
+def test_a_name_that_rebinds_to_an_internal_address_is_never_connected_to(monkeypatch):
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: False) # shared by several organizations
+
+ async def go():
+ srv, port, seen = await _server()
+ resolve, calls = _answers("140.82.112.3", "127.0.0.1") # public to the check, then internal
+ monkeypatch.setattr(gw, "_resolve", resolve)
+ assert await gw._cloud_base_refused(f"https://rebind.test:{port}") is None
+ with pytest.raises(httpx.ConnectError) as e:
+ await gw._checked_http().get(f"https://rebind.test:{port}/v1/me")
+ srv.close()
+ return seen, calls, str(e.value)
+ seen, calls, err = asyncio.run(go())
+ assert "cannot be used" in err and "internal" in err
+ assert len(calls) == 2 # the connection classified the name itself
+ assert seen["connections"] == 0 # and opened nothing to the internal answer
+
+
+def test_the_socket_goes_to_the_address_that_was_classified(monkeypatch):
+ """`pinned.test` resolves nowhere for the system: a request that reaches the server got there
+ through the classified address. The Host header still names the name."""
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: False)
+ monkeypatch.setattr(gw, "_ip_internal", lambda addr: False) # let this one test address pass
+
+ async def go():
+ srv, port, seen = await _server()
+ resolve, _ = _answers("127.0.0.1")
+ monkeypatch.setattr(gw, "_resolve", resolve)
+ r = await gw._checked_http().get(f"http://pinned.test:{port}/")
+ srv.close()
+ return r, seen, port
+ r, seen, port = asyncio.run(go())
+ assert r.status_code == 200 and seen["connections"] == 1 and seen["hosts"] == [f"pinned.test:{port}"]
+
+
+def test_a_self_hosted_box_still_reaches_its_own_network_for_addresses_its_owner_typed(monkeypatch):
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: True)
+
+ async def go():
+ srv, port, seen = await _server()
+ r = await gw._checked_http().get(f"http://127.0.0.1:{port}/")
+ srv.close()
+ return r, seen
+ r, seen = asyncio.run(go())
+ assert r.status_code == 200 and seen["connections"] == 1
+
+
+def test_a_providers_file_address_is_checked_on_every_deployment(monkeypatch):
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: True)
+
+ async def go():
+ srv, port, seen = await _server()
+ resolve, _ = _answers("140.82.112.3", "127.0.0.1")
+ monkeypatch.setattr(gw, "_resolve", resolve)
+ assert await gw._provider_url_check(f"http://cdn.test:{port}/clip.mp4") is None
+ with pytest.raises(httpx.ConnectError):
+ await gw._media_fetch_client().get(f"http://cdn.test:{port}/clip.mp4")
+ srv.close()
+ return seen
+ assert asyncio.run(go())["connections"] == 0
+
+
+def test_these_clients_follow_no_redirect_and_use_no_proxy(monkeypatch):
+ monkeypatch.setenv("HTTPS_PROXY", "http://127.0.0.1:9")
+ c = gw._checked_http()
+ assert c.follow_redirects is False and not c._mounts
+
+
+# ── a database connection: the address it reached, before any query ───────────────────────────
+class _Tx:
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, *exc):
+ return False
+
+
+def _fake_pg(monkeypatch, peer):
+ """asyncpg as far as run_query uses it, with a connection that reached `peer`."""
+ import types
+ ran = []
+
+ class Conn:
+ _transport = types.SimpleNamespace(get_extra_info=lambda k: (peer, 5432) if k == "peername" else None)
+
+ def transaction(self, readonly=False):
+ return _Tx()
+
+ async def fetch(self, sql):
+ ran.append(sql)
+ return []
+
+ async def close(self):
+ pass
+
+ async def connect(dsn):
+ return Conn()
+ monkeypatch.setitem(sys.modules, "asyncpg", types.SimpleNamespace(connect=connect))
+ return ran
+
+
+def test_a_database_that_now_answers_inside_is_closed_before_any_query(monkeypatch):
+ import sql_plane
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: False)
+ ran = _fake_pg(monkeypatch, "10.0.0.5")
+ with pytest.raises(sql_plane.SqlError) as e:
+ asyncio.run(sql_plane.run_query("postgres", "postgres://u:[email protected]/app", "SELECT 1"))
+ assert "private, local or metadata" in str(e.value) and ran == []
+
+
+def test_a_database_outside_runs_and_a_self_hosted_one_inside_runs_too(monkeypatch):
+ import sql_plane
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: False)
+ ran = _fake_pg(monkeypatch, "140.82.112.3")
+ asyncio.run(sql_plane.run_query("postgres", "postgres://u:[email protected]/app", "SELECT 1"))
+ assert len(ran) == 1
+ monkeypatch.setattr(gw, "_pool_is_local", lambda: True)
+ ran = _fake_pg(monkeypatch, "127.0.0.1")
+ asyncio.run(sql_plane.run_query("postgres", "postgres://u:p@localhost/app", "SELECT 1"))
+ assert len(ran) == 1
diff --git a/runner/environments.py b/runner/environments.py
index 36f1fe3b..7766cc51 100644
--- a/runner/environments.py
+++ b/runner/environments.py
@@ -35,6 +35,7 @@ import json
import mimetypes
import platform
import pwd
+import ipaddress
import re
import os
import pathlib
@@ -463,7 +464,34 @@ def _copy_without_links(src_root: str, dst: pathlib.Path) -> tuple[int, int]:
return written, skipped
-def import_git(env_id: str, url: str, ref: str = "", *, replace: bool = False) -> dict:
+def _git_pin_args(url: str, pin: str) -> list[str]:
+ """`git -c` settings that make the clone connect to the address the gateway classified.
+
+ `pin` is "<host>:<port>:<address>" from the gateway, on a deployment shared by several
+ organizations; empty otherwise. Without it git resolved the name again when it connected, so a
+ name that answered a public address to the gateway's check and an internal one a moment later
+ reached the internal one. Over http(s), curl is told the address for that host and port, and
+ redirects are not followed (one to another host would be resolved afresh); over ssh, ssh
+ connects to the address and still checks the host key under the name."""
+ if not pin:
+ return []
+ host, _, rest = pin.partition(":")
+ port, _, addr = rest.partition(":")
+ try:
+ ip = ipaddress.ip_address(addr)
+ except ValueError:
+ raise HTTPException(400, "git pin is not host:port:address")
+ if not (host and port.isdigit()):
+ raise HTTPException(400, "git pin is not host:port:address")
+ if url.startswith(("https://", "http://")):
+ shown = f"[{ip}]" if ip.version == 6 else str(ip)
+ return ["-c", f"http.curloptResolve={host}:{port}:{shown}", "-c", "http.followRedirects=false"]
+ if not re.fullmatch(r"[A-Za-z0-9.-]+", host):
+ raise HTTPException(400, "git pin names an invalid host")
+ return ["-c", f"core.sshCommand=ssh -o HostName={ip} -o HostKeyAlias={host}"]
+
+
+def import_git(env_id: str, url: str, ref: str = "", *, replace: bool = False, pin: str = "") -> dict:
"""Clone a repository's tree (shallow, one ref) into the source, without its .git. As with an
archive, only regular files and directories land: a link the repository commits is dropped and
counted, never followed. The copy runs as root, and following a committed link (say, to
@@ -473,7 +501,8 @@ def import_git(env_id: str, url: str, ref: str = "", *, replace: bool = False) -
if not str(url).startswith(("https://", "http://", "git@", "ssh://")):
raise HTTPException(400, "git url must be https://, http://, ssh:// or git@")
with tempfile.TemporaryDirectory(prefix="hr-env-git-") as tmp:
- cmd = ["git", "clone", "--depth", "1", "--quiet"] + (["--branch", ref] if ref else []) + [url, tmp + "/repo"]
+ cmd = (["git"] + _git_pin_args(url, pin) + ["clone", "--depth", "1", "--quiet"]
+ + (["--branch", ref] if ref else []) + [url, tmp + "/repo"])
try:
subprocess.run(cmd, check=True, capture_output=True, text=True, timeout=600,
env={**_tool_env(), "GIT_TERMINAL_PROMPT": "0"})
@@ -1105,7 +1134,9 @@ def r_delete(env_id: str, path: str) -> dict:
@router.post("/environments/{env_id}/import")
async def r_import(env_id: str, request: Request, replace: int = 0, git_url: str = "", git_ref: str = "") -> dict:
if git_url:
- return import_git(env_id, git_url, git_ref, replace=bool(replace))
+ # git_pin: host:port:address the gateway classified, on a shared deployment (_git_pin_args)
+ return import_git(env_id, git_url, git_ref, replace=bool(replace),
+ pin=str(request.query_params.get("git_pin") or ""))
fd, spool = tempfile.mkstemp(prefix="hr-env-import-")
try:
with os.fdopen(fd, "wb") as out:
diff --git a/runner/tests/test_environments.py b/runner/tests/test_environments.py
index f256884a..39afce6f 100644
--- a/runner/tests/test_environments.py
+++ b/runner/tests/test_environments.py
@@ -391,3 +391,36 @@ def test_taking_a_path_down_refuses_a_name_that_is_not_one_segment(store):
for slug in ("../outside", "a/b", "", ".."):
with pytest.raises(HTTPException):
E.drop_mount(slug, "henv_" + "a" * 32)
+
+
+def test_a_pinned_git_clone_connects_to_the_address_the_gateway_classified(store, monkeypatch):
+ """On a shared deployment the gateway hands over host:port:address; git must not resolve the
+ name again when it connects (a DNS rebind), nor follow a redirect to a name it would resolve."""
+ seen = []
+
+ def fake_run(cmd, **kw):
+ seen.append(cmd)
+ os.makedirs(cmd[-1], exist_ok=True)
+ pathlib.Path(cmd[-1], "README.md").write_text("hi\n")
+ return None
+ monkeypatch.setattr(E.subprocess, "run", fake_run)
+ E.import_git("henv_" + "c" * 32, "https://git.example/team/repo.git", pin="git.example:443:140.82.112.3")
+ E.import_git("henv_" + "c" * 32, "[email protected]:team/repo.git", pin="git.example:22:140.82.112.3")
+ E.import_git("henv_" + "c" * 32, "https://git.example/team/repo.git")
+ https, ssh, unpinned = seen
+ assert https[:5] == ["git", "-c", "http.curloptResolve=git.example:443:140.82.112.3",
+ "-c", "http.followRedirects=false"]
+ assert ssh[:3] == ["git", "-c", "core.sshCommand=ssh -o HostName=140.82.112.3 -o HostKeyAlias=git.example"]
+ assert unpinned[:2] == ["git", "clone"]
+ # through the route, as the gateway sends it
+ from fastapi import FastAPI
+ from fastapi.testclient import TestClient
+ app = FastAPI()
+ app.include_router(E.router)
+ r = TestClient(app).post(f"/environments/henv_{'c' * 32}/import",
+ params={"git_url": "https://git.example/team/repo.git", "git_pin": "git.example:443:140.82.112.3"})
+ assert r.status_code == 200 and seen[-1][:3] == ["git", "-c", "http.curloptResolve=git.example:443:140.82.112.3"]
+ for bad in ("git.example:443:not-an-address", "git.example::140.82.112.3", "a b:22:140.82.112.3"):
+ with pytest.raises(HTTPException):
+ E.import_git("henv_" + "c" * 32, "[email protected]:team/repo.git" if " " in bad else
+ "https://git.example/team/repo.git", pin=bad)