Files
ai-agent/sidecar/test_routes_git.py
gabrielvidal aaab115fa2 fix(sidecar): Monitor through the guard, fail-closed, scoped runs ignore project Claude settings; safe git, egress, ship from a copy
Monitor ran shell commands the guard never saw; the owner CLI, docker and ssh
rules fell to ordinary spellings (the CLI now refuses under AIA_GUEST); a guard
exception let the call through; chat-level runs still had the owner's gitea.lab
and gh credential helpers. Scoped lab runs: --setting-sources user,
--strict-mcp-config, hooks under python -I, the project CLAUDE.md appended as
text, scope.noBash. 3d-render uploads carry data: URIs only; a guest's video
URL resolves to public addresses through a vetting proxy; git on a guest repo
runs with fsmonitor, hooks, filters and textconv out of reach; the ship broker
deploys a checked copy; tokens leave argv.
Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-15 15:23:05 +01:00

99 lines
3.8 KiB
Python

"""/git/commit, /git/range: a worker's commits, read from its own repos."""
import subprocess
import pytest
from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
import routes_git
H = {"Authorization": "Bearer t"}
def _git(repo, *args):
return subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True,
text=True).stdout.strip()
@pytest.fixture()
def env(tmp_path, monkeypatch):
home = tmp_path / "home"
repo = home / "WORK" / "repo"
repo.mkdir(parents=True)
_git(repo, "init", "-q")
_git(repo, "config", "user.email", "t@t")
_git(repo, "config", "user.name", "t")
(repo / "a.txt").write_text("1\n")
_git(repo, "add", ".")
_git(repo, "commit", "-qm", "first")
(repo / "a.txt").write_text("2\n")
_git(repo, "commit", "-qam", "second")
monkeypatch.setenv("HOME", str(home))
def auth(h):
if h != "Bearer t":
raise HTTPException(401, "unauthorized")
monkeypatch.setattr(routes_git, "_auth", auth)
app = FastAPI()
app.include_router(routes_git.router)
return TestClient(app), repo, tmp_path
def test_commit_from_a_gone_worktree_resolves_through_the_main_checkout(env):
client, repo, _ = env
sha = _git(repo, "rev-parse", "HEAD")
r = client.post("/git/commit", headers=H,
json={"dirs": [str(repo / ".claude" / "worktrees" / "gone"), "/etc"],
"sha": sha[:10]})
assert r.status_code == 200
d = r.json()
assert d["subject"] == "second" and d["sha"] == sha
assert "-1\n+2" in d["patch"] and d["remotes"] == []
def test_range_is_the_net_diff(env):
client, repo, _ = env
first = _git(repo, "rev-list", "--max-parents=0", "HEAD")
head = _git(repo, "rev-parse", "HEAD")
d = client.post("/git/range", headers=H,
json={"dirs": [str(repo)], "base": first, "head": head}).json()
assert "+2" in d["patch"] and "-1" not in d["patch"] # from the empty tree
def test_outside_home_and_unknown_shas_are_refused(env, tmp_path):
client, repo, tmp = env
outside = tmp / "outside"
outside.mkdir()
_git(outside, "init", "-q")
sha = _git(repo, "rev-parse", "HEAD")
assert client.post("/git/commit", headers=H,
json={"dirs": [str(outside)], "sha": sha}).status_code == 404
assert client.post("/git/commit", headers=H,
json={"dirs": [str(repo)], "sha": "deadbeef00"}).status_code == 404
assert client.post("/git/commit", headers=H,
json={"dirs": [str(repo)], "sha": "HEAD;rm"}).status_code == 400
assert client.post("/git/commit", json={"dirs": [str(repo)], "sha": sha}).status_code == 401
def test_a_repos_diff_drivers_never_run(env):
"""A guest's repo can name an external diff, a textconv or a gpg program
in its config: reading its commits must run none of them (safegit.py)."""
client, repo, tmp = env
marker = tmp / "ran"
trap = tmp / "trap.sh"
trap.write_text(f"#!/bin/sh\necho x >> {marker}\ncat\n")
trap.chmod(0o755)
sha = _git(repo, "rev-parse", "HEAD")
base = _git(repo, "rev-parse", "HEAD^")
(repo / ".gitattributes").write_text("*.txt diff=x\n")
for key, value in (("diff.external", trap), ("diff.x.textconv", trap),
("diff.x.command", trap), ("core.fsmonitor", trap),
("log.showSignature", "true"), ("gpg.program", trap)):
_git(repo, "config", key, str(value))
r = client.post("/git/commit", json={"dirs": [str(repo)], "sha": sha}, headers=H)
assert r.status_code == 200 and "+2" in r.json()["patch"]
r = client.post("/git/range", json={"dirs": [str(repo)], "base": sha, "head": sha}, headers=H)
assert r.status_code == 200 and "+2" in r.json()["patch"]
assert base and not marker.exists()