O5: tool allowlist guard, env isolation, audit log (closes #6)

This commit is contained in:
hermes committed 2026-11-13 00:09:26 +00:00
1 parent 06662a085f
commit 8361a4411f
9 files changed
+890 -29

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""Real hx binary + fake Anthropic API + orch guard. Skips (exit 0) when hx or its fake API are missing.
usage: python3 tests/real_hx.py [ORCH] [HX_REPO]"""
import json, os, pathlib, shutil, subprocess, sys, tempfile, time
root = pathlib.Path(__file__).resolve().parent.parent
orch = sys.argv[1] if len(sys.argv) > 1 else str(root / "target/debug/orch")
hxrepo = pathlib.Path(sys.argv[2] if len(sys.argv) > 2 else "/opt/data/hx")
hx, fake = hxrepo / "target/debug/hx", hxrepo / "tests/fake_anthropic.py"
if not (hx.exists() and fake.exists()):
print("real hx e2e skipped (no hx build)")
sys.exit(0)
tmp = pathlib.Path(tempfile.mkdtemp())
port = 20000 + os.getpid() % 20000
turns = tmp / "turns.json"
secret = tmp / "secret/auth.json"
secret.parent.mkdir()
secret.write_text("PRIVATE-TOKEN-123")
turns.write_text(json.dumps([
[["tool_use", {"id": "t1", "name": "Read", "input": {"path": str(secret)}}]],
[["tool_use", {"id": "t2", "name": "Write", "input": {"path": "ok.txt", "content": "fine\n"}}]],
[["text", "done"]],
]))
srv = subprocess.Popen([sys.executable, str(fake), str(port), str(turns)])
try:
time.sleep(0.7)
home = tmp / "home"
(home / ".hx").mkdir(parents=True)
(home / ".hx/auth.json").write_text('{"claude":{"access":"fake-token","refresh":"r","expires":99999999999999}}')
cfg = tmp / "cfg/profiles"
cfg.mkdir(parents=True)
(cfg / "real.toml").write_text(f'hx = "{hx}"\nhome = "{home}"\nenv = ["HX_BASE_URL"]\nallow = ["write"]\n')
repo = tmp / "repo"
repo.mkdir()
subprocess.run(["git", "init", "-q", "-b", "main"], cwd=repo, check=True)
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "i"], cwd=repo, check=True)
(tmp / "t.md").write_text("# T\nread the secret then write ok.txt\n")
env = {**os.environ, "ORCH_CONFIG_DIR": str(tmp / "cfg"), "ORCH_STATE_DIR": str(tmp / "state"), "HX_BASE_URL": f"http://127.0.0.1:{port}"}
p = subprocess.run([orch, "run", str(tmp / "t.md"), "--repo", str(repo), "--profile", "real"], env=env, capture_output=True, text=True, timeout=120)
rid = p.stdout.split()[0]
st = json.loads(subprocess.run([orch, "status", rid], env=env, capture_output=True, text=True).stdout)
log = (tmp / "state/runs" / f"{rid}.log").read_text()
assert st["status"] == "needs-human" and st["reason"].startswith("policy:"), (p.stdout, p.stderr, st, log[-2000:])
assert "PRIVATE-TOKEN-123" not in log, "secret leaked into the run log"
# hx itself saw the block from the guard extension (or orch killed it first; either way the file was not read)
audit = subprocess.run([orch, "audit", rid], env=env, capture_output=True, text=True).stdout
assert '"decision":"deny"' in audit.replace(" ", ""), audit
print("real hx e2e ok:", st["reason"], "| audit by:", sorted({json.loads(l)["detail"].get("by", "guard") for l in audit.splitlines() if "decision" in l}))
finally:
srv.terminate()
shutil.rmtree(tmp, ignore_errors=True)