Files
ai-agent/sidecar/test_guest_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

92 lines
4.3 KiB
Python

"""A guest run's git (guest_git.py): the owner's Gitea helper never answers,
the guest's own token does when the run ships."""
import os
import subprocess
import tempfile
import guest_git
def _fill(env):
return subprocess.run(["git", "credential", "fill"], env={**os.environ, **env},
cwd=tempfile.gettempdir(), # not inside a (worktree's) repo
input="protocol=https\nhost=" + guest_git.GITEA_HOST + "\n\n",
capture_output=True, text=True, timeout=20)
def test_the_guests_token_answers():
p = _fill(guest_git.git_env("dom", "tok"))
assert p.returncode == 0 and "username=dom" in p.stdout and "password=tok" in p.stdout
def test_without_a_token_nothing_answers():
p = _fill(guest_git.git_env(None, None))
assert p.returncode != 0 and "password=" not in p.stdout
def test_chat_level_never_mints(monkeypatch):
monkeypatch.setattr(guest_git, "token_for", lambda *a: (_ for _ in ()).throw(AssertionError))
env = guest_git.run_env({"user": "dom", "push": []})
assert "AIA_GITEA_TOKEN" not in env and env["GIT_CONFIG_VALUE_0"] == ""
assert "GIT_CONFIG_GLOBAL" not in env # a guest's own computer keeps its git config
def _owner_home(tmp_path, monkeypatch):
"""A home whose ~/.gitconfig answers for every host, like the lab's
(`tea-cli credential` for Gitea under both names, `gh` for github.com)."""
home = tmp_path / "home"
home.mkdir()
(home / ".gitconfig").write_text(
"[user]\n\tname = The Owner\n\temail = owner@example.test\n"
+ "".join(f'[credential "https://{h}"]\n\thelper = "!f() {{ echo username=owner; '
f'echo password=OWNER-TOKEN; }}; f"\n'
for h in (guest_git.GITEA_HOST, "gitea.lab.gabvdl.xyz", "github.com")))
monkeypatch.setenv("HOME", str(home))
monkeypatch.delenv("GIT_CONFIG_GLOBAL", raising=False)
monkeypatch.setattr(guest_git, "STATE_DIR", tmp_path / "state")
monkeypatch.setattr(guest_git, "GLOBAL_CONFIG", tmp_path / "state" / "gitconfig")
def _fill_host(env, host):
return subprocess.run(["git", "credential", "fill"], env={**os.environ, **env},
cwd=tempfile.gettempdir(), input=f"protocol=https\nhost={host}\n\n",
capture_output=True, text=True, timeout=20)
def test_no_owner_helper_answers_a_lab_guest_run(tmp_path, monkeypatch):
"""Chat level "gets no git credential": the reset covered one URL, and the
owner's helpers for the Gitea host's other name and for github.com still
answered a plain `git push`."""
_owner_home(tmp_path, monkeypatch)
for host in (guest_git.GITEA_HOST, "gitea.lab.gabvdl.xyz", "github.com"):
assert "OWNER-TOKEN" in _fill_host({}, host).stdout # the owner's own shell
for env in (guest_git.git_env(None, None, isolate=True), # the lab
guest_git.git_env(None, None)): # helpers reset alone
p = _fill_host(env, host)
assert p.returncode != 0 and "OWNER-TOKEN" not in p.stdout, (host, p.stdout)
env = guest_git.git_env(None, None, isolate=True)
cfg = guest_git.GLOBAL_CONFIG
assert env["GIT_CONFIG_GLOBAL"] == str(cfg) and env["GIT_CONFIG_NOSYSTEM"] == "1"
assert oct(cfg.stat().st_mode & 0o777) == "0o600" and "helper = !" not in cfg.read_text()
# commits are still committed by the host's git user
who = subprocess.run(["git", "config", "--global", "user.name"], env={**os.environ, **env},
cwd=tempfile.gettempdir(), capture_output=True, text=True)
assert who.stdout.strip() == "The Owner"
def test_ship_level_still_pushes_as_the_guest_on_the_lab(tmp_path, monkeypatch):
_owner_home(tmp_path, monkeypatch)
env = guest_git.git_env("dom", "tok", isolate=True)
p = _fill_host(env, guest_git.GITEA_HOST)
assert p.returncode == 0 and "username=dom" in p.stdout and "password=tok" in p.stdout
assert "OWNER-TOKEN" not in p.stdout
# their token is for the Gitea host only
assert _fill_host(env, "github.com").returncode != 0
def test_remote_parsing():
m = guest_git._REMOTE_RE.match(f"https://{guest_git.GITEA_HOST}/gabrielvidal/sherlock-project.git")
assert m and m.groups() == ("gabrielvidal", "sherlock-project")
assert not guest_git._REMOTE_RE.match("https://github.com/x/y.git")