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.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
An environment's git import never follows a link in the repository (#423)
github-actions[bot]6f0e58d5e43c
diff --git a/gateway/app.py b/gateway/app.pyindex 43a84be3..fe2b44e3 100644--- a/gateway/app.py+++ b/gateway/app.py@@ -16787,6 +16787,28 @@ 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)."""+ if _pool_is_local():+ return None+ from urllib.parse import urlparse+ u = str(url or "")+ if u.startswith("git@"):+ host, port = u[4:].split(":", 1)[0], 22+ else:+ try:+ p = urlparse(u)+ host, port = p.hostname or "", p.port or {"http": 80, "ssh": 22}.get(p.scheme, 443)+ except ValueError:+ return "invalid url"+ if not host:+ return "url has no host"+ return await _internal_target(host, port)++@app.post("/v1/environments/{env_id}/import")async def environment_import(env_id: str, request: Request, replace: int = 0) -> dict:"""A whole project at once, keeping its tree: a zip or tar archive as the body, or a JSON body@@ -16798,6 +16820,9 @@ 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")))+ 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"")diff --git a/gateway/tests/test_environments.py b/gateway/tests/test_environments.pyindex 23a3333f..2933b12e 100644--- a/gateway/tests/test_environments.py+++ b/gateway/tests/test_environments.py@@ -268,3 +268,25 @@ def test_a_request_that_does_not_validate_is_answered_without_its_body():j = r.json()assert j["error"]["code"] == "invalid_request" and j["error"]["param"] == "model_map" and "model_map" in j["error"]["message"]assert j["detail"][0]["loc"] == ["body", "model_map"] and set(j["detail"][0]) == {"loc", "msg", "type"}+++def test_a_git_address_on_a_private_network_is_refused_on_a_shared_deployment(api, monkeypatch):+ """The clone runs on this side, so the rule an MCP address follows applies: on a shared+ deployment a private, local or metadata address is refused before anything is cloned; a+ self-hosted box's own network is its operator's and is allowed."""+ eid = api.post("/v1/environments", json={"name": "git-env"}).json()["id"]+ forwarded = []++ async def recorder(method, path, env_id, **kw):+ forwarded.append(path)+ raise AssertionError("nothing should reach the runner")+ monkeypatch.setattr(app, "_env_runner", recorder)+ monkeypatch.setattr(app, "_pool_is_local", lambda: False)+ for url in ("https://10.0.0.5/team/repo.git", "http://169.254.169.254/latest/", "[email protected]:team/repo.git",+ "ssh://git@[::1]/team/repo.git", "https:///no-host"):+ 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+ 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 Nonediff --git a/runner/environments.py b/runner/environments.pyindex 6e133878..8d8aff34 100644--- a/runner/environments.py+++ b/runner/environments.py@@ -432,15 +432,51 @@ def import_archive(env_id: str, data_path: str, *, replace: bool = False) -> dicreturn {"written": written, "skipped": skipped, **source_stat(env_id)}+def _copy_without_links(src_root: str, dst: pathlib.Path) -> tuple[int, int]:+ """Copy a tree's regular files and directories into dst; a link, a device or a fifo is counted+ and left behind, never followed. Returns (written, skipped)."""+ written = skipped = 0+ for dirpath, dirnames, filenames in os.walk(src_root): # os.walk does not descend a linked directory+ keep = []+ for d in dirnames:+ if os.path.islink(os.path.join(dirpath, d)):+ skipped += 1+ else:+ keep.append(d)+ dirnames[:] = keep+ rel = os.path.relpath(dirpath, src_root)+ out = dst if rel == "." else dst / rel+ out.mkdir(parents=True, exist_ok=True)+ for f in filenames:+ p = os.path.join(dirpath, f)+ st = os.lstat(p)+ if not stat.S_ISREG(st.st_mode):+ skipped += 1+ continue+ target = out / f+ if target.is_symlink():+ target.unlink()+ shutil.copyfile(p, target, follow_symlinks=False)+ if st.st_mode & 0o111:+ os.chmod(target, 0o755)+ written += 1+ return written, skipped++def import_git(env_id: str, url: str, ref: str = "", *, replace: bool = False) -> dict:- """Clone a repository's tree (shallow, one ref) into the source, without its .git."""+ """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+ /proc/self/environ) wrote what it pointed at into the source, where the files route reads it+ back (reported privately, GHSA-mw9j-m5jf-5r56). The clone runs with the environment a build's+ commands get, without the runner's own secrets."""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"]try:subprocess.run(cmd, check=True, capture_output=True, text=True, timeout=600,- env={**os.environ, "GIT_TERMINAL_PROMPT": "0"})+ env={**_tool_env(), "GIT_TERMINAL_PROMPT": "0"})except subprocess.CalledProcessError as e:raise HTTPException(400, f"git clone failed: {(e.stderr or '')[-400:].strip()}")except subprocess.TimeoutExpired:@@ -449,9 +485,8 @@ def import_git(env_id: str, url: str, ref: str = "", *, replace: bool = False) -src = source_dir(env_id)if replace and src.exists():shutil.rmtree(src)- src.mkdir(parents=True, exist_ok=True)- shutil.copytree(tmp + "/repo", src, dirs_exist_ok=True, symlinks=False)- return {"imported": "git", "url": url, "ref": ref, **source_stat(env_id)}+ written, skipped = _copy_without_links(tmp + "/repo", _source(env_id))+ return {"imported": "git", "url": url, "ref": ref, "written": written, "skipped": skipped, **source_stat(env_id)}# ── runtimes ────────────────────────────────────────────────────────────────────────────────────diff --git a/runner/tests/test_environments.py b/runner/tests/test_environments.pyindex f64bca46..59b27fd8 100644--- a/runner/tests/test_environments.py+++ b/runner/tests/test_environments.py@@ -325,3 +325,41 @@ def test_a_package_check_asks_the_registry_and_refuses_what_is_not_there(monkeypapt = E.check_package("apt", "jq=1.7.1-3")assert apt["exists"] is True and apt["latest"] == "1.7.1-3" and apt["version"] == "1.7.1-3"assert E.check_package("apt", "nope")["exists"] is False+++def test_a_git_import_lands_files_and_directories_and_never_follows_a_link(store, tmp_path, monkeypatch):+ """A link a repository commits is dropped and counted, like an archive's; following it as root+ wrote what it pointed at into the source (reported privately). The clone gets the build's+ environment, without the runner's secrets."""+ outside = tmp_path / "outside"+ (outside / "dir").mkdir(parents=True)+ (outside / "private.txt").write_text("not for the environment")+ (outside / "dir" / "inner.txt").write_text("nor this")+ monkeypatch.setenv("HR_SECRET_KEY", "runner-secret-value")+ monkeypatch.setenv("HARNESS_INTERNAL_KEY", "runner-internal-value")+ seen = {}++ def fake_clone(cmd, **kw):+ seen["env"] = kw.get("env") or {}+ repo = pathlib.Path(cmd[-1])+ (repo / ".git").mkdir(parents=True)+ (repo / "src").mkdir()+ (repo / "README.md").write_text("hello\n")+ (repo / "src" / "run.sh").write_text("echo hi\n")+ os.chmod(repo / "src" / "run.sh", 0o755)+ os.symlink(outside / "private.txt", repo / "leak.txt")+ os.symlink(outside / "dir", repo / "src" / "linked-dir")+ os.symlink("README.md", repo / "readme-alias")+ return None+ monkeypatch.setattr(E.subprocess, "run", fake_clone)+ out = E.import_git("henv_g", "https://example.com/repo.git")+ assert out["written"] == 2 and out["skipped"] == 3, out+ files = {e["path"] for e in E.tree("henv_g") if not e["dir"]}+ assert files == {"README.md", "src/run.sh"}+ src = E.source_dir("henv_g")+ assert not any(p.is_symlink() for p in src.rglob("*"))+ assert all("not for the environment" not in p.read_text() and "nor this" not in p.read_text()+ for p in src.rglob("*") if p.is_file())+ assert os.stat(src / "src" / "run.sh").st_mode & 0o111+ assert "HR_SECRET_KEY" not in seen["env"] and "HARNESS_INTERNAL_KEY" not in seen["env"]+ assert seen["env"]["GIT_TERMINAL_PROMPT"] == "0"