accept allow with claude_cli, fail closed when tool calls are unseen

This commit is contained in:
hermes committed 2026-11-14 10:22:29 +00:00
1 parent 2751bcf27b
commit 80ea84f8f6
6 files changed
+56 -46

No files matched your search

+8 -8
View File
@@ -279,14 +279,14 @@ fakecl = tmp / "claude"
fakecl.write_text("#!/bin/sh\n[ \"$1\" = --version ] && echo fake\nexit 0\n")
fakecl.chmod(0o755)
cenv = {"HX_CLAUDE_BIN": str(fakecl)} # a claude binary exists => hx would pick claude_cli
s = check(run("refuse", "sec", env=cenv, body="CALL write a.txt"), "needs-human", "policy: `allow` cannot be enforced")
assert s["attempts"] == 0 and s["hx_exit"] is None, s # hx was never started
assert "claude_cli = false" in (tmp / "state/runs" / (s["id"] + ".log")).read_text()
# explicit opt-out in the profile's hx config => direct, allowed again
(work_home / ".hx").mkdir(exist_ok=True)
(work_home / ".hx/config.toml").write_text("claude_cli = false\n")
check(run("optout", "sec", env=cenv, body="CALL write a.txt"), "done")
(work_home / ".hx/config.toml").unlink()
# claude_cli + allow: every tool claude runs must reach the guard as a tool_call (hx#182); fewer = fail closed
s = check(run("cli-seen", "sec", env=cenv, body="CLAUDE\nCALL write a.txt\nCALL bash cargo test -p x"), "done")
s = check(run("cli-deny", "sec", env=cenv, body=f"CLAUDE\nCALL read {private}/auth.json"), "needs-human", "policy:")
assert "PRIVATE-TOKEN-123" not in git_show(s["branch"], "calls.txt")
s = check(run("cli-unseen", "sec", env=cenv, body="CLAUDE UNSEEN\nCALL write a.txt"), "needs-human", "policy: claude used 1 tool(s) but hx reported 0")
assert "johannes/hx#182" in s["reason"] or "hx#182" in s["reason"], s
# no claude lines => nothing to cross-check (direct provider): unaffected
check(run("direct-ok", "sec", env=senv, body="CALL write a.txt"), "done")
# without `allow` claude_cli runs as before (recorded as claude_cli)
profile("noallow", f'home = "{work_home}"\n')
check(run("cli-ok", "noallow", env=cenv, body="WRITE"), "done")
+3
View File
@@ -16,5 +16,8 @@ if target:
except OSError as e:
open("claude-read.txt", "w").write("ERR " + str(e))
out({"type": "system", "subtype": "init", "session_id": "S1"})
if os.environ.get("FAKE_CLAUDE_READ"):
out({"type": "assistant", "session_id": "S1", "message": {"content": [
{"type": "tool_use", "name": "Read", "input": {"file_path": os.environ["FAKE_CLAUDE_READ"]}}]}})
out({"type": "stream_event", "session_id": "S1", "event": {"type": "content_block_delta", "delta": {"type": "text_delta", "text": "done"}}})
out({"type": "result", "session_id": "S1", "is_error": False, "result": "done", "usage": {"input_tokens": 3, "output_tokens": 4}})
+4
View File
@@ -21,6 +21,10 @@ def guard(name, args):
for n, l in enumerate(x for x in body.splitlines() if x.startswith("CALL ")):
_, name, arg = l.split(" ", 2)
args = {"command": arg} if name == "bash" else {"path": arg}
if "CLAUDE" in body: # claude_cli: hx prints a text line per tool claude runs
e({"type": "stream", "event": {"type": "text_delta", "text": f"\n[claude {name}: {arg}]\n"}})
if "UNSEEN" in body: # hx without #182: the line is there, the tool_call event is not
continue
e({"type": "tool_call", "id": f"c{n}", "name": name, "args": args})
r = guard(name, args)
if r and r["action"] == "block":
+5 -18
View File
@@ -74,26 +74,13 @@ try:
assert seen == "PRIVATE-TOKEN-123", (seen, p.stdout, p.stderr, st)
last = [json.loads(l) for l in (tmp / "state/telemetry.jsonl").read_text().splitlines() if l.strip()][-1]
assert last["provider_mode"] == "claude_cli", last
# with `allow`: refused before hx starts, so the claude binary never runs and the secret stays unread
n_calls = len((tmp / "argv.log").read_text().splitlines())
# with `allow` on today's hx (no tool events for claude's tools, hx#182): fail closed, never "guarded"
st, seen, p = go("guarded", "gd")
assert len((tmp / "argv.log").read_text().splitlines()) == n_calls, "claude binary was started despite allow"
assert seen is None, seen
assert st["status"] == "needs-human" and "cannot be enforced" in st["reason"], (st, p.stdout, p.stderr)
# the escape hatch: the direct provider in the profile's hx config makes `allow` enforceable again
(home / ".hx/config.toml").write_text("claude_cli = false\n")
srv2 = subprocess.Popen([sys.executable, str(fake), str(port + 1), str(turns)])
time.sleep(0.7)
cenv_vars["HX_BASE_URL"] = f"http://127.0.0.1:{port + 1}"
try:
st, seen, p = go("guarded", "gd2")
finally:
srv2.terminate()
assert st["status"] == "needs-human" and st["reason"].startswith("policy: ") and "cannot be enforced" not in st["reason"], (st, p.stdout)
assert seen is None or "PRIVATE-TOKEN-123" not in seen
assert st["status"] == "needs-human" and "could not be enforced" in st["reason"], (st, p.stdout, p.stderr)
last = [json.loads(l) for l in (tmp / "state/telemetry.jsonl").read_text().splitlines() if l.strip()][-1]
assert last["provider_mode"] == "direct", last
print("claude_cli mode ok: allow refused with claude_cli, enforced with direct")
assert last["provider_mode"] == "claude_cli", last
# (the fake claude has already read the secret by then: this hx cannot stop it, which is what hx#182 is for)
print("claude_cli mode ok: unseen tool use fails closed (hx#182 missing)")
finally:
srv.terminate()
shutil.rmtree(tmp, ignore_errors=True)