From f32c720f32a961c7116a517092bcf9cf65d5e21f Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 04:57:26 -0800 Subject: [PATCH 1/3] Write the guarded runner devrun (copy-in, isolation, network phases) bin/devrun (Python 3.9, standard library only) runs a repository command in a locked-down container without mounting any host path. - Source: --commit (git archive, pinned), --worktree (git ls-files --cached --others --exclude-standard, unpinned), or --history BASE..HEAD (a bundle of existing refs that contain the commits, fetched into a brand-new repository inside the container; HEAD and origin/main are pinned to the exact SHAs). No refs are created in the source repo. - The host checks and rewrites every tar member before Docker sees it: no .git, no absolute or traversing paths, no absolute or escaping symlinks, no hard links, devices, or FIFOs; setuid/setgid/sticky bits stripped; owner 10001; missing parent directories emitted with that owner. - Containers: fixed name -dev-run, owner and role labels, UID 10001, --cap-drop ALL, no-new-privileges, read-only root, size-limited /tmp, pids/memory/swap/cpu/nofile limits, bounded json-file logs, and only the project's named volumes. The cargo caches are writable only in the --fetch phase (network); the command runs with --network none and CARGO_NET_OFFLINE=true unless --network is given. - Lifecycle: create, copy in, start with a timeout, export, remove; the container is also removed on errors and SIGTERM/SIGINT. Each run claims host/heavy-work and host/docker/-dev/ in the workstation git-locks store (lease = timeout + 15 minutes). - Exports: only declared paths and an optional patch of the changes, as regular files, into a fresh folder outside every git repository. Tests: 24 unit tests (crafted hostile tarballs, export checks, argument handling) and tests/integration-devrun (real Docker: uid, read-only root, no network, no Docker socket, CapEff 0, NoNewPrivs, read-only cargo caches and CARGO_HOME, exports, patch, worktree and history modes, and the absolute-symlink refusal). Both pass locally (arm64). The Docker litter audit, receipts, and budget checks follow in #5. Closes #3 --- bin/devrun | 569 +++++++++++++++++++++++++++++++++++++++ docs/hygiene.md | 14 +- tests/integration-devrun | 75 ++++++ tests/test_devrun.py | 180 +++++++++++++ 4 files changed, 833 insertions(+), 5 deletions(-) create mode 100755 bin/devrun create mode 100755 tests/integration-devrun create mode 100644 tests/test_devrun.py diff --git a/bin/devrun b/bin/devrun new file mode 100755 index 0000000..fbac341 --- /dev/null +++ b/bin/devrun @@ -0,0 +1,569 @@ +#!/usr/bin/env python3 +"""devrun: run a repository command in a locked-down, copy-in container. + +Rules: docs/hygiene.md (ULTRAMAX PARANOIA). The repository is copied in, never +mounted. The tarball is checked and rewritten on the host before Docker sees it. +Each run claims git-locks keys, uses fixed names and owner labels, and removes +its container on every exit path. + +Usage: + devrun PROJECT REPO (--commit SHA | --history BASE..HEAD | --worktree) + [--rust VERSION | --image IMAGE] [--fetch] [--network] [--evidence] + [--export PATH]... [--export-patch] [--out DIR] [--timeout SECONDS] + [--cpus N] [--memory SIZE] -- COMMAND [ARG...] +""" +from __future__ import annotations + +import argparse +import hashlib +import io +import os +import posixpath +import re +import shlex +import signal +import stat +import subprocess +import sys +import tarfile +import tempfile +import uuid +from pathlib import Path +from typing import Iterable, List, Optional + +UID = 10001 +GID = 10001 +OWNER_LABEL = "org.flyingrobots.owner" +ROLE_LABEL = "org.flyingrobots.role" +PROJECT_RE = re.compile(r"[a-z0-9][a-z0-9-]{0,39}") +SHA_RE = re.compile(r"[0-9a-f]{7,40}") +LOCK_STORE = "/Users/Shared/git-locks/workstation.git" +LOCK_ENV = "DEVRUN_LOCK_JOB" +DEFAULT_RUST = "1.96.0" + + +class DevrunError(Exception): + """A refusal or failure with a message for the operator.""" + + +# --------------------------------------------------------------------------- # +# Host-side tarball checks (docs/hygiene.md section 1) +# --------------------------------------------------------------------------- # + + +def _safe_member_path(name: str) -> str: + if name.startswith("/") or name.startswith("\\"): + raise DevrunError(f"refused: absolute path in source: {name!r}") + norm = posixpath.normpath(name) + if norm == ".": + return "." + parts = norm.split("/") + if ".." in parts: + raise DevrunError(f"refused: path traversal in source: {name!r}") + if ".git" in parts: + raise DevrunError(f"refused: .git in source: {name!r}") + return norm + + +def _check_symlink(member_path: str, target: str) -> None: + if target.startswith("/"): + raise DevrunError(f"refused: absolute symlink {member_path!r} -> {target!r}") + resolved = posixpath.normpath(posixpath.join(posixpath.dirname(member_path), target)) + if resolved == ".." or resolved.startswith("../") or "/../" in f"/{resolved}/": + raise DevrunError(f"refused: symlink escapes the source {member_path!r} -> {target!r}") + if ".git" in resolved.split("/"): + raise DevrunError(f"refused: symlink into .git {member_path!r} -> {target!r}") + + +def sanitize_tar(src: io.BufferedIOBase, dst: io.BufferedIOBase) -> int: + """Copy tar members from src to dst after checking each one. Returns the member count. + + Refuses .git, absolute or traversing paths, escaping or absolute symlinks, + hard links, devices, FIFOs, and anything that is not a file, directory, or + symlink. Strips setuid, setgid, and sticky bits, and sets the owner to 10001. + """ + count = 0 + dirs_emitted = set() + + def emit_parents(path: str, mtime: int) -> None: + # Docker creates missing parent directories as root, which UID 10001 then cannot clean up. + # Emit every parent explicitly, owned by 10001. + parent = posixpath.dirname(path) + chain = [] + while parent and parent not in dirs_emitted: + chain.append(parent) + parent = posixpath.dirname(parent) + for d in reversed(chain): + info = tarfile.TarInfo(d) + info.type = tarfile.DIRTYPE + info.mode = 0o755 + info.uid, info.gid = UID, GID + info.uname = info.gname = "dev" + info.mtime = mtime + tout.addfile(info) + dirs_emitted.add(d) + + with tarfile.open(fileobj=src, mode="r|*") as tin, tarfile.open(fileobj=dst, mode="w|", format=tarfile.PAX_FORMAT) as tout: + for member in tin: + if member.type in (tarfile.XHDTYPE, tarfile.XGLTYPE): + continue + path = _safe_member_path(member.name) + if path == ".": + continue + emit_parents(path, int(member.mtime)) + if member.isdir(): + if path in dirs_emitted: + continue + dirs_emitted.add(path) + if member.issym(): + _check_symlink(path, member.linkname) + elif member.islnk(): + raise DevrunError(f"refused: hard link in source: {member.name!r}") + elif not (member.isfile() or member.isdir()): + raise DevrunError(f"refused: special file in source: {member.name!r}") + clean = tarfile.TarInfo(path) + clean.type = member.type + clean.mode = member.mode & 0o777 + clean.uid, clean.gid = UID, GID + clean.uname = clean.gname = "dev" + clean.mtime = member.mtime + if member.issym(): + clean.linkname = member.linkname + if member.isfile(): + clean.size = member.size + data = tin.extractfile(member) + tout.addfile(clean, data) + else: + tout.addfile(clean) + count += 1 + return count + + +def check_export_tar(src: io.BufferedIOBase, dest: Path) -> List[str]: + """Extract an export tarball from Docker into dest, allowing only regular files and directories.""" + written: List[str] = [] + with tarfile.open(fileobj=src, mode="r|*") as tin: + for member in tin: + path = _safe_member_path(member.name) + target = dest / path + if member.isdir(): + target.mkdir(parents=True, exist_ok=True) + continue + if not member.isfile(): + raise DevrunError(f"refused export: not a regular file: {member.name!r}") + target.parent.mkdir(parents=True, exist_ok=True) + data = tin.extractfile(member) + with open(target, "xb") as fh: + while True: + chunk = data.read(1 << 20) + if not chunk: + break + fh.write(chunk) + os.chmod(target, 0o644) + written.append(path) + return written + + +# --------------------------------------------------------------------------- # +# Source modes +# --------------------------------------------------------------------------- # + + +def run(cmd: List[str], **kw) -> subprocess.CompletedProcess: + kw.setdefault("check", False) + return subprocess.run(cmd, **kw) + + +def git(repo: Path, *args: str, capture: bool = True) -> str: + p = run(["git", "-C", str(repo), *args], stdout=subprocess.PIPE if capture else None, stderr=subprocess.PIPE) + if p.returncode != 0: + raise DevrunError(f"git {' '.join(args)} failed: {p.stderr.decode(errors='replace').strip()}") + return p.stdout.decode().strip() if capture else "" + + +def resolve_commit(repo: Path, rev: str) -> str: + return git(repo, "rev-parse", "--verify", f"{rev}^{{commit}}") + + +def build_source_tar(repo: Path, mode: str, rev: Optional[str], out: Path) -> dict: + """Write the sanitized source tar to out. Returns facts for the receipt.""" + raw = out.with_suffix(".raw.tar") + facts: dict = {"mode": mode} + if mode == "commit": + sha = resolve_commit(repo, rev or "HEAD") + facts["sha"] = sha + with open(raw, "wb") as fh: + p = run(["git", "-C", str(repo), "archive", "--format=tar", sha], stdout=fh, stderr=subprocess.PIPE) + if p.returncode != 0: + raise DevrunError(f"git archive failed: {p.stderr.decode(errors='replace').strip()}") + elif mode == "worktree": + listing = run(["git", "-C", str(repo), "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--stage"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if listing.returncode != 0: + raise DevrunError(f"git ls-files failed: {listing.stderr.decode(errors='replace').strip()}") + names = sorted(set(_worktree_paths(repo))) + facts["sha"] = None + facts["unpinned"] = True + with tarfile.open(raw, "w", format=tarfile.PAX_FORMAT) as tout: + for rel in names: + full = repo / rel + try: + st = os.lstat(full) + except FileNotFoundError: + continue # tracked but deleted in the worktree + if stat.S_ISDIR(st.st_mode): + raise DevrunError(f"refused: {rel!r} is a directory in the listing (submodule?); worktree mode does not support submodules") + tout.add(str(full), arcname=rel, recursive=False) + else: + raise DevrunError(f"unknown mode {mode}") + with open(raw, "rb") as fin, open(out, "wb") as fout: + facts["members"] = sanitize_tar(fin, fout) + raw.unlink() + facts["tar_sha256"] = sha256_file(out) + return facts + + +def _worktree_paths(repo: Path) -> Iterable[str]: + p = run(["git", "-C", str(repo), "ls-files", "-z", "--cached", "--others", "--exclude-standard"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if p.returncode != 0: + raise DevrunError(f"git ls-files failed: {p.stderr.decode(errors='replace').strip()}") + gitlinks = run(["git", "-C", str(repo), "ls-files", "-z", "--stage"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.split(b"\0") + if any(entry.startswith(b"160000 ") for entry in gitlinks): + raise DevrunError("refused: worktree mode does not support submodules") + for name in p.stdout.split(b"\0"): + if name: + yield name.decode() + + +def build_history(repo: Path, spec: str, out_dir: Path) -> dict: + if ".." not in spec: + raise DevrunError("--history needs BASE..HEAD") + base_rev, head_rev = spec.split("..", 1) + base, head = resolve_commit(repo, base_rev), resolve_commit(repo, head_rev) + # A bundle records refs, and it must carry the full ancestry to be cloneable. Use existing refs + # that contain the commits; never create refs in the source repository. + refs = _containing_refs(repo, head) + base_is_ancestor = run(["git", "-C", str(repo), "merge-base", "--is-ancestor", base, head]).returncode == 0 + if not base_is_ancestor: + refs += [r for r in _containing_refs(repo, base) if r not in refs][:1] + bundle = out_dir / "history.bundle" + p = run(["git", "-C", str(repo), "bundle", "create", str(bundle), *refs], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if p.returncode != 0: + raise DevrunError(f"git bundle failed: {p.stderr.decode(errors='replace').strip()}") + # The head tree must pass the same checks as an archive copy. + with open(out_dir / "head-check.raw.tar", "wb") as fh: + run(["git", "-C", str(repo), "archive", "--format=tar", head], stdout=fh, check=True) + with open(out_dir / "head-check.raw.tar", "rb") as fin: + sanitize_tar(fin, io.BytesIO()) + (out_dir / "head-check.raw.tar").unlink() + tar_path = out_dir / "history.tar" + with tarfile.open(tar_path, "w", format=tarfile.PAX_FORMAT) as tout: + info = tarfile.TarInfo("history.bundle") + info.size = bundle.stat().st_size + info.uid, info.gid, info.uname, info.gname, info.mode = UID, GID, "dev", "dev", 0o644 + with open(bundle, "rb") as fh: + tout.addfile(info, fh) + bundle.unlink() + return {"mode": "history", "sha": head, "base": base, "tar_sha256": sha256_file(tar_path)} + + +def _containing_refs(repo: Path, sha: str) -> List[str]: + refs = [r for r in git(repo, "for-each-ref", "--contains", sha, "--format=%(refname)").splitlines() if r] + if not refs: + raise DevrunError(f"refused: commit {sha} is not reachable from any ref") + preferred = [r for r in refs if r.startswith("refs/heads/")] + [r for r in refs if r.startswith("refs/remotes/")] + return (preferred or refs)[:1] + + +def sha256_file(path: Path) -> str: + h = hashlib.sha256() + with open(path, "rb") as fh: + for chunk in iter(lambda: fh.read(1 << 20), b""): + h.update(chunk) + return "sha256:" + h.hexdigest() + + +# --------------------------------------------------------------------------- # +# Containers (docs/hygiene.md sections 2, 3, 5, 7) +# --------------------------------------------------------------------------- # + +INNER_SCRIPT = r''' +set -euo pipefail +mode="$1"; shift +export_patch="$1"; shift +cd /scratch/work +git_q() { git -c user.name=devrun -c user.email=devrun@invalid -c commit.gpgsign=false -c init.defaultBranch=main "$@"; } +if [ "$mode" = history ]; then + git_q init -q . + git_q fetch -q /scratch/in/history.bundle '+refs/*:refs/devrun/*' + git_q update-ref refs/heads/main "$DEVRUN_HEAD" + git_q update-ref refs/remotes/origin/main "$DEVRUN_BASE" + git_q checkout -q main + git for-each-ref --format='%(refname)' refs/devrun/ | while read -r r; do git_q update-ref -d "$r"; done + test "$(git rev-parse HEAD)" = "$DEVRUN_HEAD" || { echo "devrun: clone HEAD does not match $DEVRUN_HEAD" >&2; exit 97; } +else + if find . -name .git -print -quit | grep -q .; then echo "devrun: .git found in the copy" >&2; exit 98; fi + git_q init -q . + git_q add -A + git_q commit -q --allow-empty -m "devrun baseline" +fi +baseline="$(git rev-parse HEAD)" +set +e +"$@" +status=$? +set -e +if [ "$export_patch" = 1 ]; then + git_q add -A + git_q diff --cached --binary "$baseline" > /scratch/out/changes.patch +fi +exit "$status" +''' + +PREP_SCRIPT = r''' +set -euo pipefail +rm -rf /scratch/work /scratch/in /scratch/out +mkdir -p /scratch/work /scratch/in /scratch/out +if [ "$1" = 1 ]; then find /target -mindepth 1 -delete; fi +''' + + +class Docker: + def __init__(self, project: str, image: str, cpus: str, memory: str, network_default: bool): + self.project = project + self.image = image + self.cpus = cpus + self.memory = memory + self.name = f"{project}-dev-run" + + def labels(self, role: str) -> List[str]: + return ["--label", f"{OWNER_LABEL}={self.project}", "--label", f"{ROLE_LABEL}={role}"] + + def ensure_volume(self, name: str, role: str) -> None: + p = run(["docker", "volume", "inspect", name, "--format", "{{json .Labels}}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if p.returncode == 0: + labels = json_loads(p.stdout.decode() or "null") or {} + if labels.get(OWNER_LABEL) != self.project: + raise DevrunError(f"refused: volume {name} exists but is not owned by {self.project}") + return + p = run(["docker", "volume", "create", *self.labels(role), name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if p.returncode != 0: + raise DevrunError(f"docker volume create {name} failed: {p.stderr.decode(errors='replace').strip()}") + + def create(self, cmd: List[str], network: bool, target_volume: str, env: Optional[dict] = None, + caches_writable: bool = False) -> None: + """Create the run container. The cargo caches are writable only in the fetch phase.""" + ro = "" if caches_writable else ",readonly" + args = ["docker", "create", "--name", self.name, *self.labels("dev-run"), + "--user", f"{UID}:{GID}", "--cap-drop", "ALL", "--security-opt", "no-new-privileges", + "--read-only", "--tmpfs", "/tmp:rw,nosuid,nodev,size=1g,mode=1777", + "--pids-limit", "4096", "--memory", self.memory, "--memory-swap", self.memory, "--cpus", self.cpus, + "--ulimit", "nofile=65536:65536", + "--log-driver", "json-file", "--log-opt", "max-size=10m", "--log-opt", "max-file=3", + "--network", "bridge" if network else "none", + "--mount", f"type=volume,src={self.project}-cargo-registry,dst=/usr/local/cargo/registry{ro}", + "--mount", f"type=volume,src={self.project}-cargo-git,dst=/usr/local/cargo/git{ro}", + "--mount", f"type=volume,src={target_volume},dst=/target", + "--mount", f"type=volume,src={self.project}-scratch,dst=/scratch", + "--workdir", "/scratch/work"] + for k, v in (env or {}).items(): + args += ["--env", f"{k}={v}"] + args += [self.image, *cmd] + p = run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if p.returncode != 0: + raise DevrunError(f"docker create failed: {p.stderr.decode(errors='replace').strip()}") + + def copy_in(self, tar_path: Path, dest: str) -> None: + with open(tar_path, "rb") as fh: + p = run(["docker", "cp", "-a", "-", f"{self.name}:{dest}"], stdin=fh, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if p.returncode != 0: + raise DevrunError(f"docker cp into {dest} failed: {p.stderr.decode(errors='replace').strip()}") + + def start(self, timeout: int) -> int: + try: + p = run(["docker", "start", "-a", self.name], timeout=timeout) + except subprocess.TimeoutExpired: + run(["docker", "kill", self.name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + raise DevrunError(f"timeout after {timeout}s; container killed") + return p.returncode + + def export(self, path: str, dest: Path) -> List[str]: + p = subprocess.Popen(["docker", "cp", f"{self.name}:/scratch/out/{path}", "-"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + try: + written = check_export_tar(p.stdout, dest) + finally: + p.stdout.close() + err = p.stderr.read().decode(errors="replace").strip() + if p.wait() != 0: + raise DevrunError(f"export of {path!r} failed: {err}") + return written + + def remove(self) -> None: + run(["docker", "rm", "-f", self.name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + def exists(self) -> bool: + p = run(["docker", "ps", "-a", "--filter", f"name=^{self.name}$", "--format", "{{.Names}}"], stdout=subprocess.PIPE) + return bool(p.stdout.strip()) + + +def json_loads(text: str): + import json + return json.loads(text) + + +# --------------------------------------------------------------------------- # +# Orchestration +# --------------------------------------------------------------------------- # + + +def parse_args(argv: List[str]) -> argparse.Namespace: + if "--" not in argv: + raise DevrunError("missing '--' before COMMAND") + i = argv.index("--") + head, command = argv[:i], argv[i + 1:] + if not command: + raise DevrunError("missing COMMAND after '--'") + ap = argparse.ArgumentParser(prog="devrun", description="Run a command in a locked-down copy-in container.") + ap.add_argument("project") + ap.add_argument("repo") + src = ap.add_mutually_exclusive_group(required=True) + src.add_argument("--commit", metavar="SHA") + src.add_argument("--history", metavar="BASE..HEAD") + src.add_argument("--worktree", action="store_true") + img = ap.add_mutually_exclusive_group() + img.add_argument("--rust", default=DEFAULT_RUST) + img.add_argument("--image") + ap.add_argument("--fetch", action="store_true", help="run cargo fetch --locked with network first") + ap.add_argument("--network", action="store_true", help="give the command itself network access") + ap.add_argument("--evidence", action="store_true", help="use an emptied evidence target volume") + ap.add_argument("--export", action="append", default=[], metavar="PATH") + ap.add_argument("--export-patch", action="store_true") + ap.add_argument("--out", metavar="DIR") + ap.add_argument("--timeout", type=int, default=1800) + ap.add_argument("--cpus", default="4") + ap.add_argument("--memory", default="8g") + ns = ap.parse_args(head) + if not PROJECT_RE.fullmatch(ns.project): + raise DevrunError(f"invalid project name {ns.project!r} (lowercase letters, digits, hyphens)") + if ns.timeout <= 0 or ns.timeout > 7200: + raise DevrunError("--timeout must be between 1 and 7200 seconds") + for e in ns.export: + _safe_member_path(e) + ns.command = command + return ns + + +def fresh_out_dir(arg: Optional[str]) -> Path: + if arg: + out = Path(arg).resolve() + if out.exists() and any(out.iterdir()): + raise DevrunError(f"refused: --out {out} is not empty") + else: + out = Path(tempfile.mkdtemp(prefix="devrun-out-")) + probe = run(["git", "-C", str(out if out.exists() else out.parent), "rev-parse", "--show-toplevel"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + if probe.returncode == 0: + raise DevrunError(f"refused: --out {out} is inside a git repository") + out.mkdir(parents=True, exist_ok=True) + return out + + +def main_inner(ns: argparse.Namespace) -> int: + repo = Path(ns.repo).resolve() + top = Path(git(repo, "rev-parse", "--show-toplevel")) + out = fresh_out_dir(ns.out) + work = Path(tempfile.mkdtemp(prefix="devrun-src-")) + image = ns.image or f"flyingrobots/rust-dev:{ns.rust}" + docker = Docker(ns.project, image, ns.cpus, ns.memory, ns.network) + target_volume = f"{ns.project}-target-evidence" if ns.evidence else f"{ns.project}-target" + stop = {"flag": False} + + def on_signal(signum, _frame): + docker.remove() + raise DevrunError(f"interrupted by signal {signum}; container removed") + + signal.signal(signal.SIGTERM, on_signal) + signal.signal(signal.SIGINT, on_signal) + try: + if ns.history: + facts = build_history(top, ns.history, work) + tar_path, mode = work / "history.tar", "history" + else: + mode = "commit" if ns.commit else "worktree" + tar_path = work / "source.tar" + facts = build_source_tar(top, mode, ns.commit, tar_path) + print(f"devrun: source {mode} {facts.get('sha') or '(unpinned worktree)'} {facts['tar_sha256']}", file=sys.stderr) + if docker.exists(): + raise DevrunError(f"refused: container {docker.name} already exists (another run, or litter)") + docker.ensure_volume(f"{ns.project}-cargo-registry", "cargo-registry") + docker.ensure_volume(f"{ns.project}-cargo-git", "cargo-git") + docker.ensure_volume(target_volume, "target-evidence" if ns.evidence else "target") + docker.ensure_volume(f"{ns.project}-scratch", "scratch") + # Phase 0: reset the scratch area (and the evidence target) offline. + docker.create(["bash", "-c", PREP_SCRIPT, "prep", "1" if ns.evidence else "0"], False, target_volume) + if docker.start(300) != 0: + raise DevrunError("preparing the scratch volume failed") + docker.remove() + env = {"DEVRUN_HEAD": facts.get("sha") or "", "DEVRUN_BASE": facts.get("base") or ""} + # Copy-in container: extract the source into the scratch volume. + dest = "/scratch/in" if mode == "history" else "/scratch/work" + docker.create(["true"], False, target_volume, env) + docker.copy_in(tar_path, dest) + if docker.start(300) != 0: + raise DevrunError("copy-in container failed") + docker.remove() + if ns.fetch: + # Phase 1: fetch dependencies with network, nothing else. + fetch = ["bash", "-c", INNER_SCRIPT, "fetch", mode, "0", "cargo", "fetch", "--locked"] + docker.create(fetch, True, target_volume, env, caches_writable=True) + if docker.start(ns.timeout) != 0: + raise DevrunError("cargo fetch --locked failed") + docker.remove() + if mode != "history": + # The fetch phase created the throwaway repository; start phase 2 from a clean copy again. + docker.create(["bash", "-c", PREP_SCRIPT, "prep", "0"], False, target_volume) + docker.start(300) + docker.remove() + docker.create(["true"], False, target_volume, env) + docker.copy_in(tar_path, dest) + docker.start(300) + docker.remove() + # Phase 2: the command, offline unless --network. + cmd = ["bash", "-c", INNER_SCRIPT, "run", mode, "1" if ns.export_patch else "0", *ns.command] + env["CARGO_NET_OFFLINE"] = "false" if ns.network else "true" + docker.create(cmd, ns.network, target_volume, env) + status = docker.start(ns.timeout) + exported: List[str] = [] + for path in ns.export: + exported += docker.export(path, out) + if ns.export_patch: + exported += docker.export("changes.patch", out) + docker.remove() + if docker.exists(): + raise DevrunError(f"container {docker.name} still exists after removal") + print(f"devrun: exit {status}; exports in {out}: {', '.join(exported) or 'none'}", file=sys.stderr) + return status + finally: + docker.remove() + for p in work.glob("*"): + p.unlink() + work.rmdir() + + +def main(argv: List[str]) -> int: + try: + ns = parse_args(argv) + if os.environ.get("CI") == "true" or os.environ.get(LOCK_ENV): + return main_inner(ns) + job = f"devrun-{ns.project}-{uuid.uuid4()}" + lock = ["git", "locks", "with", "--job", job, "--holder", f"devrun-{os.getpid()}", + "--ttl", str(ns.timeout + 900), "--wait", "600", "--note", f"devrun {ns.project}", + "host/heavy-work", f"host/docker/{ns.project}-dev/", "--", + sys.executable, os.path.abspath(__file__), *argv] + env = dict(os.environ, GIT_LOCKS_STORE=os.environ.get("GIT_LOCKS_STORE", LOCK_STORE), **{LOCK_ENV: job}) + return subprocess.call(lock, env=env) + except DevrunError as exc: + print(f"devrun: {exc}", file=sys.stderr) + return 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/docs/hygiene.md b/docs/hygiene.md index dd1d4a9..39b0be7 100644 --- a/docs/hygiene.md +++ b/docs/hygiene.md @@ -14,11 +14,15 @@ enforces every rule here; a rule that only lives in this file is not enforced. `git archive ` on the host, streamed into the container. The copy is exact and pinned. No `.git` is ever copied. - `--history ..` (for gates that read commit history, such as - Bunny's `repo-respect`): `git bundle create` on the host packs only the - commits from `` to ``. Inside the container, the runner clones a - brand-new repository from the bundle and sets `refs/remotes/origin/main` to - ``. The clone has the real commits and messages, and it is not the - original `.git`. The runner checks that the clone's `HEAD` equals ``. + Bunny's `repo-respect`): `git bundle create` on the host packs an existing + ref that contains `` (and one that contains `` when it is not an + ancestor). A bundle must carry the full ancestry to be cloneable, so it holds + more than `..`; it is still an object export, never the original + `.git`, and the runner never creates refs in the source repository. Inside the + container, the runner fetches the bundle into a brand-new repository, pins + `main` to `` and `refs/remotes/origin/main` to ``, and deletes the + temporary refs. It checks that `HEAD` equals ``. The `` tree must + also pass the archive checks on the host. - `--worktree` (iteration only, marked *unpinned* in the receipt): `git ls-files -z --cached --others --exclude-standard --recurse-submodules`, streamed with `tar`. This includes new files that are diff --git a/tests/integration-devrun b/tests/integration-devrun new file mode 100755 index 0000000..14cd9f2 --- /dev/null +++ b/tests/integration-devrun @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# Real-Docker integration test for bin/devrun. Needs flyingrobots/rust-dev:1.96.0 (scripts/build-image 1.96.0). +# Uses project name devrun-selftest and removes only that project's labeled volumes at the end. +set -euo pipefail +root="$(cd "$(dirname "$0")/.." && pwd)" +devrun="$root/bin/devrun" +project=devrun-selftest +work="$(mktemp -d -t devrun-selftest-XXXXXX)" +fail() { echo "integration-devrun: FAIL: $*" >&2; exit 1; } +cleanup() { + rm -rf "$work" + docker rm -f "$project-dev-run" >/dev/null 2>&1 || true + for v in cargo-registry cargo-git target target-evidence scratch; do + if [[ "$(docker volume inspect "$project-$v" --format '{{index .Labels "org.flyingrobots.owner"}}' 2>/dev/null || true)" == "$project" ]]; then + docker volume rm "$project-$v" >/dev/null + fi + done +} +trap cleanup EXIT + +repo="$work/repo"; mkdir -p "$repo/src" +git -C "$repo" init -q -b main +printf 'hello\n' > "$repo/README" +printf 'fn main() {}\n' > "$repo/src/main.rs" +ln -s README "$repo/README.link" +git -C "$repo" add -A +git -C "$repo" -c user.name=t -c user.email=t@invalid commit -q -m "base: first" +base="$(git -C "$repo" rev-parse HEAD)" +printf 'second\n' >> "$repo/README" +git -C "$repo" -c user.name=t -c user.email=t@invalid commit -qam "feat: second + +Repo-Respect-Receipt: none" +head="$(git -C "$repo" rev-parse HEAD)" + +echo "== commit mode: isolation checks" +probe='set -e +test "$(id -u)" = 10001 +test -f README && test -L README.link && test "$(cat README.link | head -1)" = hello +test -d .git # created inside the container by devrun, empty baseline only +test "$(git log --format=%s)" = "devrun baseline" +if touch /usr/local/devrun-probe 2>/dev/null; then echo "root fs is writable" >&2; exit 11; fi +if curl -sS -m 5 https://example.com >/dev/null 2>&1; then echo "network reachable" >&2; exit 12; fi +if test -e /var/run/docker.sock; then echo "docker socket visible" >&2; exit 13; fi +grep -q "^CapEff:\s*0000000000000000" /proc/self/status || { echo "capabilities not dropped" >&2; exit 14; } +grep -q "^NoNewPrivs:\s*1" /proc/self/status || { echo "no_new_privs not set" >&2; exit 15; } +if touch /usr/local/cargo/registry/devrun-probe 2>/dev/null; then echo "cargo registry writable in the run phase" >&2; exit 16; fi +if touch /usr/local/cargo/config.toml 2>/dev/null; then echo "CARGO_HOME writable" >&2; exit 17; fi +echo ok > /scratch/out/result.txt +printf "changed\n" >> README' +out1="$work/out1" +"$devrun" "$project" "$repo" --commit "$head" --timeout 300 --export result.txt --export-patch --out "$out1" -- bash -c "$probe" +[[ "$(cat "$out1/result.txt")" == "ok" ]] || fail "export result.txt" +grep -q '^+changed$' "$out1/changes.patch" || fail "patch does not contain the change" +docker ps -a --format '{{.Names}}' | grep -qx "$project-dev-run" && fail "container still exists" + +echo "== worktree mode: untracked file included, ignored file excluded" +printf 'new\n' > "$repo/untracked.txt" +printf 'secret\n' > "$repo/ignored.env"; printf 'ignored.env\n' > "$repo/.gitignore" +out2="$work/out2" +"$devrun" "$project" "$repo" --worktree --timeout 300 --export result.txt --out "$out2" -- \ + bash -c 'test -f untracked.txt && ! test -e ignored.env && echo ok > /scratch/out/result.txt' +[[ "$(cat "$out2/result.txt")" == "ok" ]] || fail "worktree mode" + +echo "== history mode: real commits, exact HEAD, origin/main at base" +out3="$work/out3" +"$devrun" "$project" "$repo" --history "$base..$head" --timeout 300 --export result.txt --out "$out3" -- \ + bash -c "test \"\$(git rev-parse HEAD)\" = $head && test \"\$(git rev-parse origin/main)\" = $base && git log -1 --format=%B | grep -q 'Repo-Respect-Receipt: none' && echo ok > /scratch/out/result.txt" +[[ "$(cat "$out3/result.txt")" == "ok" ]] || fail "history mode" + +echo "== refusals" +ln -s /etc/passwd "$repo/evil"; git -C "$repo" add evil; git -C "$repo" -c user.name=t -c user.email=t@invalid commit -qm "evil" +if "$devrun" "$project" "$repo" --commit HEAD --timeout 60 -- true 2>"$work/err"; then fail "absolute symlink accepted"; fi +grep -q 'absolute symlink' "$work/err" || fail "wrong refusal: $(cat "$work/err")" + +echo "integration-devrun: PASS" diff --git a/tests/test_devrun.py b/tests/test_devrun.py new file mode 100644 index 0000000..98692c6 --- /dev/null +++ b/tests/test_devrun.py @@ -0,0 +1,180 @@ +"""Unit tests for bin/devrun: the host-side tarball checks and argument handling. + +Run: python3 -m unittest discover -s tests +These tests need no Docker. +""" +import importlib.machinery +import importlib.util +import io +import pathlib +import tarfile +import tempfile +import unittest + +ROOT = pathlib.Path(__file__).resolve().parents[1] +loader = importlib.machinery.SourceFileLoader("devrun", str(ROOT / "bin" / "devrun")) +spec = importlib.util.spec_from_loader("devrun", loader) +devrun = importlib.util.module_from_spec(spec) +loader.exec_module(devrun) + + +def make_tar(entries): + """entries: list of (name, kind, extra). kind: file, dir, sym, hard, fifo, chr.""" + buf = io.BytesIO() + with tarfile.open(fileobj=buf, mode="w", format=tarfile.PAX_FORMAT) as t: + for name, kind, extra in entries: + info = tarfile.TarInfo(name) + info.uid = info.gid = 0 + info.mode = extra.get("mode", 0o644) if isinstance(extra, dict) else 0o644 + data = None + if kind == "file": + payload = extra.get("data", b"x") if isinstance(extra, dict) else b"x" + info.size = len(payload) + data = io.BytesIO(payload) + elif kind == "dir": + info.type = tarfile.DIRTYPE + info.mode = 0o755 + elif kind == "sym": + info.type = tarfile.SYMTYPE + info.linkname = extra + elif kind == "hard": + info.type = tarfile.LNKTYPE + info.linkname = extra + elif kind == "fifo": + info.type = tarfile.FIFOTYPE + elif kind == "chr": + info.type = tarfile.CHRTYPE + t.addfile(info, data) + buf.seek(0) + return buf + + +def sanitize(entries): + out = io.BytesIO() + n = devrun.sanitize_tar(make_tar(entries), out) + out.seek(0) + return n, out + + +class SanitizerRefuses(unittest.TestCase): + def assert_refused(self, entries, needle): + with self.assertRaises(devrun.DevrunError) as ctx: + sanitize(entries) + self.assertIn(needle, str(ctx.exception)) + + def test_git_directory(self): + self.assert_refused([(".git", "dir", None)], ".git") + + def test_nested_git_file(self): + self.assert_refused([("vendor/lib/.git", "file", {})], ".git") + + def test_absolute_path(self): + self.assert_refused([("/etc/passwd", "file", {})], "absolute path") + + def test_traversal(self): + self.assert_refused([("a/../../x", "file", {})], "path traversal") + + def test_absolute_symlink(self): + self.assert_refused([("link", "sym", "/etc/shadow")], "absolute symlink") + + def test_escaping_symlink(self): + self.assert_refused([("a/link", "sym", "../../outside")], "escapes") + + def test_symlink_into_git(self): + self.assert_refused([("link", "sym", ".git/config")], ".git") + + def test_hard_link(self): + self.assert_refused([("a", "file", {}), ("b", "hard", "a")], "hard link") + + def test_fifo(self): + self.assert_refused([("pipe", "fifo", None)], "special file") + + def test_device(self): + self.assert_refused([("dev", "chr", None)], "special file") + + +class SanitizerRewrites(unittest.TestCase): + def test_owner_and_mode_bits(self): + n, out = sanitize([("bin/tool", "file", {"mode": 0o4755, "data": b"#!/bin/sh\n"}), ("src", "dir", None)]) + self.assertEqual(n, 2) + with tarfile.open(fileobj=out) as t: + members = {m.name: m for m in t.getmembers()} + tool = members["bin/tool"] + self.assertEqual((tool.uid, tool.gid), (10001, 10001)) + self.assertEqual(tool.mode, 0o755) + self.assertEqual(members["src"].uid, 10001) + + def test_inside_symlink_is_kept(self): + n, _ = sanitize([("a/real", "file", {}), ("a/link", "sym", "real"), ("b/up", "sym", "../a/real")]) + self.assertEqual(n, 3) + + def test_missing_parent_directories_are_emitted_and_owned(self): + _, out = sanitize([("a/b/c.txt", "file", {}), ("a/d.txt", "file", {})]) + with tarfile.open(fileobj=out) as t: + members = t.getmembers() + names = [m.name for m in members] + self.assertEqual(names, ["a", "a/b", "a/b/c.txt", "a/d.txt"]) + for m in members: + self.assertEqual((m.uid, m.gid), (10001, 10001)) + self.assertEqual(members[0].type, tarfile.DIRTYPE) + + def test_directory_entries_are_not_duplicated(self): + _, out = sanitize([("a", "dir", None), ("a/x", "file", {}), ("a", "dir", None)]) + with tarfile.open(fileobj=out) as t: + self.assertEqual([m.name for m in t.getmembers()], ["a", "a/x"]) + + def test_file_content_is_preserved(self): + _, out = sanitize([("README", "file", {"data": b"hello"})]) + with tarfile.open(fileobj=out) as t: + self.assertEqual(t.extractfile("README").read(), b"hello") + + +class ExportChecks(unittest.TestCase): + def test_regular_files_only(self): + with tempfile.TemporaryDirectory() as d: + written = devrun.check_export_tar(make_tar([("out", "dir", None), ("out/hash.txt", "file", {"data": b"h"})]), pathlib.Path(d)) + self.assertEqual(written, ["out/hash.txt"]) + self.assertEqual((pathlib.Path(d) / "out/hash.txt").read_bytes(), b"h") + + def test_symlink_export_refused(self): + with tempfile.TemporaryDirectory() as d, self.assertRaises(devrun.DevrunError): + devrun.check_export_tar(make_tar([("evil", "sym", "/etc/passwd")]), pathlib.Path(d)) + + def test_existing_file_is_not_overwritten(self): + with tempfile.TemporaryDirectory() as d: + (pathlib.Path(d) / "x").write_text("keep") + with self.assertRaises(FileExistsError): + devrun.check_export_tar(make_tar([("x", "file", {"data": b"new"})]), pathlib.Path(d)) + self.assertEqual((pathlib.Path(d) / "x").read_text(), "keep") + + +class Arguments(unittest.TestCase): + def test_requires_double_dash(self): + with self.assertRaises(devrun.DevrunError): + devrun.parse_args(["geordi", ".", "--commit", "HEAD", "true"]) + + def test_requires_command(self): + with self.assertRaises(devrun.DevrunError): + devrun.parse_args(["geordi", ".", "--commit", "HEAD", "--"]) + + def test_project_name_is_strict(self): + for bad in ["Geordi", "../x", "a b", "", "-x"]: + with self.assertRaises((devrun.DevrunError, SystemExit)): + devrun.parse_args([bad, ".", "--commit", "HEAD", "--", "true"]) + + def test_one_source_mode(self): + with self.assertRaises(SystemExit): + devrun.parse_args(["geordi", ".", "--commit", "HEAD", "--worktree", "--", "true"]) + + def test_export_paths_are_checked(self): + with self.assertRaises(devrun.DevrunError): + devrun.parse_args(["geordi", ".", "--commit", "HEAD", "--export", "../../etc", "--", "true"]) + + def test_good_arguments(self): + ns = devrun.parse_args(["geordi", ".", "--commit", "HEAD", "--fetch", "--export", "hashes.txt", "--", "cargo", "test"]) + self.assertEqual(ns.command, ["cargo", "test"]) + self.assertTrue(ns.fetch) + + +if __name__ == "__main__": + unittest.main() From f06ae9629c196e0b28f061ce7a48aa27c479631b Mon Sep 17 00:00:00 2001 From: James Ross Date: Fri, 13 Nov 2026 14:04:51 -0800 Subject: [PATCH 2/3] Bring devrun up to the hygiene rules that landed with the base image The draft runner predated the rules merged in #6. Fixed, each with a test: - It removed any container named -dev-run in its cleanup, even one it had just refused to touch because another run (or litter) owned it. Docker.remove now acts only on a container this run created; the integration test plants such a container and checks it survives. - The copy lived at /scratch/work, under a writable /scratch that was only partly emptied, so a planted .cargo/config.toml in a parent directory could reach a later run. The copy is now the root of its own -work volume at /work, and both volumes are emptied completely, hidden entries included, before every copy-in. - The fetch phase ran on the full copy. It now uses a copy without .cargo/config* and rust-toolchain* files (at every depth; history mode uses the sanitized head tree, not a clone) and forces RUSTUP_TOOLCHAIN, RUSTC and empty RUSTC_WRAPPER/RUSTC_WORKSPACE_WRAPPER. Calibration: with a planted rustc-wrapper and path toolchain, the run passes; a control in phase 2 shows cargo fetch does call the wrapper when the config is present; with both protections removed the run fails on the planted toolchain. - setuid and setgid bits are refused, as issue #3 requires, instead of stripped. The sticky bit is still dropped. - The lock key is host/docker/-dev-run/, the container's name. - HOME is /tmp/home in every container and is created before use. README documents devrun; CI runs its unit tests in a container through devrun and the real-Docker integration test, and shellchecks it. Checks: 34 unit tests in the rust-dev:1.96.0 container via devrun; tests/integration-devrun PASS (commit, worktree, history, fetch, existing container, refusals); shellcheck clean. Audit, receipts and budgets stay in #5. Agent-Session: abe7b061-b12e-4785-a5bf-3fb4b6ce1a0c Agent-Tool: Claude Code --- .github/workflows/ci.yml | 6 +- README.md | 45 +++++++++++++- bin/devrun | 131 +++++++++++++++++++++++++++++---------- tests/integration-devrun | 36 ++++++++++- tests/test_devrun.py | 89 +++++++++++++++++++++++++- 5 files changed, 266 insertions(+), 41 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f13c9d8..324a5f4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - name: Shellcheck - run: shellcheck scripts/* tests/smoke-image + run: shellcheck scripts/* tests/smoke-image tests/integration-devrun - name: Build flyingrobots/rust-dev:1.96.0 run: scripts/build-image 1.96.0 - name: Smoke test @@ -28,3 +28,7 @@ jobs: second="$(docker image inspect -f '{{.Id}}' flyingrobots/rust-dev:1.96.0)" echo "first=$first second=$second" test "$first" = "$second" + - name: devrun unit tests, in a container + run: bin/devrun devcontainers . --commit HEAD -- python3 -m unittest discover -s tests + - name: devrun integration test (real Docker) + run: tests/integration-devrun diff --git a/README.md b/README.md index 2ec8b30..428cdd2 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,45 @@ version. The image runs as UID 10001. Mount points for the named volumes: `/work` (the copy), `/scratch` (inputs, logs, exports), `/target` (build output), `/usr/local/cargo/registry`, and `/usr/local/cargo/git` (cargo caches). -The guarded runner `bin/devrun` is not here yet (flyingrobots/devcontainers#3 -and #5). Until it lands, these two scripts are the only supported way to use -Docker from this repository. +## Run a repository's gates: `bin/devrun` + +`bin/devrun` (Python 3.9+, standard library only) copies a repository into +locked-down containers and runs one command there. Nothing is mounted from the +host; every rule it enforces is in [`docs/hygiene.md`](docs/hygiene.md). + +```sh +# A pinned commit, fetching dependencies first (network only for cargo fetch): +bin/devrun bunny ~/git/bunny --commit HEAD --fetch -- cargo test --locked --workspace + +# Gates that read commit history (Bunny's repo-respect): +bin/devrun bunny ~/git/bunny --history origin/main..HEAD -- cargo run --locked -p xtask -- repo-respect check + +# Iterate on uncommitted work (marked unpinned), exporting a file and a patch: +bin/devrun ninelives ~/git/ninelives --worktree --export report.txt --export-patch -- cargo update +``` + +- Source modes: `--commit SHA` (pinned; required for merge-gate evidence), + `--history BASE..HEAD`, `--worktree` (unpinned). The host builds and checks + the tarball before Docker sees it: no `.git`, no absolute or escaping + symlinks, no hard links, no setuid or setgid bits, no special files; every + owner becomes UID 10001. +- Phase 1 (`--fetch`) runs `cargo fetch --locked` with the network and writable + cargo caches, from a copy without `.cargo/config*` or `rust-toolchain*`, with + the compiler pinned and the wrappers cleared. Phase 2 runs the command from a + fresh copy at `/work`, with `--network none` unless `--network` is given. +- Outputs leave only through `--export PATH` (files written under + `/scratch/out/` in the container) and `--export-patch` (a patch from the + throwaway repository), into a fresh folder outside every repository + (`--out DIR`, or a new temporary folder). +- Each run holds the git-locks keys `host/heavy-work` and + `host/docker/-dev-run/`, and removes only the container it created. + `--timeout` (default 1800 s) is shorter than the lease. +- Not yet: the Docker litter audit, run receipts, free-space and volume budget + checks (flyingrobots/devcontainers#5). + +Test it: + +```sh +bin/devrun devcontainers . --worktree -- python3 -m unittest discover -s tests # unit tests, in a container +tests/integration-devrun # real Docker +``` diff --git a/bin/devrun b/bin/devrun index fbac341..98dfced 100755 --- a/bin/devrun +++ b/bin/devrun @@ -79,8 +79,8 @@ def sanitize_tar(src: io.BufferedIOBase, dst: io.BufferedIOBase) -> int: """Copy tar members from src to dst after checking each one. Returns the member count. Refuses .git, absolute or traversing paths, escaping or absolute symlinks, - hard links, devices, FIFOs, and anything that is not a file, directory, or - symlink. Strips setuid, setgid, and sticky bits, and sets the owner to 10001. + hard links, devices, FIFOs, setuid and setgid bits, and anything that is not + a file, directory, or symlink. Drops the sticky bit and sets the owner to 10001. """ count = 0 dirs_emitted = set() @@ -121,6 +121,8 @@ def sanitize_tar(src: io.BufferedIOBase, dst: io.BufferedIOBase) -> int: raise DevrunError(f"refused: hard link in source: {member.name!r}") elif not (member.isfile() or member.isdir()): raise DevrunError(f"refused: special file in source: {member.name!r}") + if member.mode & 0o6000: + raise DevrunError(f"refused: setuid or setgid bit in source: {member.name!r}") clean = tarfile.TarInfo(path) clean.type = member.type clean.mode = member.mode & 0o777 @@ -139,6 +141,36 @@ def sanitize_tar(src: io.BufferedIOBase, dst: io.BufferedIOBase) -> int: return count +FETCH_EXCLUDED_NAMES = {"rust-toolchain", "rust-toolchain.toml"} +CARGO_CONFIG_NAMES = {"config", "config.toml"} + + +def excluded_from_fetch(path: str) -> bool: + """True for files the fetch copy must not contain (docs/hygiene.md section 3). + + A repository's `.cargo/config(.toml)` can name a compiler or wrapper, and its + `rust-toolchain(.toml)` can select a `path` toolchain; `cargo fetch` would run either + while the caches are writable. They are dropped at every depth. + """ + parts = path.split("/") + name = parts[-1] + if name in FETCH_EXCLUDED_NAMES: + return True + return name in CARGO_CONFIG_NAMES and len(parts) >= 2 and parts[-2] == ".cargo" + + +def fetch_copy(src: io.BufferedIOBase, dst: io.BufferedIOBase) -> int: + """Copy a sanitized tar, leaving out the files that could choose code for `cargo fetch`.""" + count = 0 + with tarfile.open(fileobj=src, mode="r|*") as tin, tarfile.open(fileobj=dst, mode="w|", format=tarfile.PAX_FORMAT) as tout: + for member in tin: + if member.isfile() and excluded_from_fetch(member.name): + continue + tout.addfile(member, tin.extractfile(member) if member.isfile() else None) + count += 1 + return count + + def check_export_tar(src: io.BufferedIOBase, dest: Path) -> List[str]: """Extract an export tarball from Docker into dest, allowing only regular files and directories.""" written: List[str] = [] @@ -250,11 +282,12 @@ def build_history(repo: Path, spec: str, out_dir: Path) -> dict: p = run(["git", "-C", str(repo), "bundle", "create", str(bundle), *refs], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if p.returncode != 0: raise DevrunError(f"git bundle failed: {p.stderr.decode(errors='replace').strip()}") - # The head tree must pass the same checks as an archive copy. + # The head tree must pass the same checks as an archive copy. The sanitized tree is kept: the + # fetch phase uses it, because a fetch must not clone the bundle with its config files. with open(out_dir / "head-check.raw.tar", "wb") as fh: run(["git", "-C", str(repo), "archive", "--format=tar", head], stdout=fh, check=True) - with open(out_dir / "head-check.raw.tar", "rb") as fin: - sanitize_tar(fin, io.BytesIO()) + with open(out_dir / "head-check.raw.tar", "rb") as fin, open(out_dir / "head.tar", "wb") as fout: + sanitize_tar(fin, fout) (out_dir / "head-check.raw.tar").unlink() tar_path = out_dir / "history.tar" with tarfile.open(tar_path, "w", format=tarfile.PAX_FORMAT) as tout: @@ -291,8 +324,9 @@ INNER_SCRIPT = r''' set -euo pipefail mode="$1"; shift export_patch="$1"; shift -cd /scratch/work +cd /work git_q() { git -c user.name=devrun -c user.email=devrun@invalid -c commit.gpgsign=false -c init.defaultBranch=main "$@"; } +mkdir -p "$HOME" if [ "$mode" = history ]; then git_q init -q . git_q fetch -q /scratch/in/history.bundle '+refs/*:refs/devrun/*' @@ -319,13 +353,19 @@ fi exit "$status" ''' +# Empties the copy and scratch volumes completely, hidden entries included, so nothing one run +# plants (such as a .cargo/config.toml) reaches the next (docs/hygiene.md section 2). PREP_SCRIPT = r''' set -euo pipefail -rm -rf /scratch/work /scratch/in /scratch/out -mkdir -p /scratch/work /scratch/in /scratch/out +find /work /scratch -mindepth 1 -delete +mkdir -p /scratch/in /scratch/out if [ "$1" = 1 ]; then find /target -mindepth 1 -delete; fi ''' +# Phase 1. Runs no repository-chosen code: the copy has no cargo config or toolchain file, and the +# environment pins the compiler and clears the wrappers (docs/hygiene.md section 3). +FETCH_COMMAND = ["bash", "-c", 'mkdir -p "$HOME" && exec cargo fetch --locked'] + class Docker: def __init__(self, project: str, image: str, cpus: str, memory: str, network_default: bool): @@ -334,6 +374,9 @@ class Docker: self.cpus = cpus self.memory = memory self.name = f"{project}-dev-run" + # True only between a successful `docker create` by this run and its removal, so cleanup + # never touches a container that another run (or litter) owns under the same name. + self.created = False def labels(self, role: str) -> List[str]: return ["--label", f"{OWNER_LABEL}={self.project}", "--label", f"{ROLE_LABEL}={role}"] @@ -363,14 +406,17 @@ class Docker: "--mount", f"type=volume,src={self.project}-cargo-registry,dst=/usr/local/cargo/registry{ro}", "--mount", f"type=volume,src={self.project}-cargo-git,dst=/usr/local/cargo/git{ro}", "--mount", f"type=volume,src={target_volume},dst=/target", + "--mount", f"type=volume,src={self.project}-work,dst=/work", "--mount", f"type=volume,src={self.project}-scratch,dst=/scratch", - "--workdir", "/scratch/work"] + "--env", "HOME=/tmp/home", + "--workdir", "/work"] for k, v in (env or {}).items(): args += ["--env", f"{k}={v}"] args += [self.image, *cmd] p = run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if p.returncode != 0: raise DevrunError(f"docker create failed: {p.stderr.decode(errors='replace').strip()}") + self.created = True def copy_in(self, tar_path: Path, dest: str) -> None: with open(tar_path, "rb") as fh: @@ -398,7 +444,11 @@ class Docker: return written def remove(self) -> None: + """Remove the run container, only if this run created it.""" + if not self.created: + return run(["docker", "rm", "-f", self.name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + self.created = False def exists(self) -> bool: p = run(["docker", "ps", "-a", "--filter", f"name=^{self.name}$", "--format", "{{.Names}}"], stdout=subprocess.PIPE) @@ -466,6 +516,30 @@ def fresh_out_dir(arg: Optional[str]) -> Path: return out +def fetch_environment(env: dict, rust_version: str) -> dict: + """The fetch phase's environment: these variables override any cargo configuration file, so the + image's compiler runs with no wrapper even if a config file slipped through.""" + return dict(env, RUSTUP_TOOLCHAIN=rust_version, RUSTC="/usr/local/cargo/bin/rustc", + RUSTC_WRAPPER="", RUSTC_WORKSPACE_WRAPPER="") + + +def prepare(docker: "Docker", target_volume: str, empty_target: bool) -> None: + """Empty the copy and scratch volumes (and the evidence target when asked), offline.""" + docker.create(["bash", "-c", PREP_SCRIPT, "prep", "1" if empty_target else "0"], False, target_volume) + if docker.start(300) != 0: + raise DevrunError("preparing the work and scratch volumes failed") + docker.remove() + + +def copy_in(docker: "Docker", target_volume: str, tar_path: Path, dest: str, env: dict) -> None: + """Extract a sanitized tar into the volumes through a short-lived, offline container.""" + docker.create(["true"], False, target_volume, env) + docker.copy_in(tar_path, dest) + if docker.start(300) != 0: + raise DevrunError("copy-in container failed") + docker.remove() + + def main_inner(ns: argparse.Namespace) -> int: repo = Path(ns.repo).resolve() top = Path(git(repo, "rev-parse", "--show-toplevel")) @@ -496,36 +570,25 @@ def main_inner(ns: argparse.Namespace) -> int: docker.ensure_volume(f"{ns.project}-cargo-registry", "cargo-registry") docker.ensure_volume(f"{ns.project}-cargo-git", "cargo-git") docker.ensure_volume(target_volume, "target-evidence" if ns.evidence else "target") + docker.ensure_volume(f"{ns.project}-work", "work") docker.ensure_volume(f"{ns.project}-scratch", "scratch") - # Phase 0: reset the scratch area (and the evidence target) offline. - docker.create(["bash", "-c", PREP_SCRIPT, "prep", "1" if ns.evidence else "0"], False, target_volume) - if docker.start(300) != 0: - raise DevrunError("preparing the scratch volume failed") - docker.remove() env = {"DEVRUN_HEAD": facts.get("sha") or "", "DEVRUN_BASE": facts.get("base") or ""} - # Copy-in container: extract the source into the scratch volume. - dest = "/scratch/in" if mode == "history" else "/scratch/work" - docker.create(["true"], False, target_volume, env) - docker.copy_in(tar_path, dest) - if docker.start(300) != 0: - raise DevrunError("copy-in container failed") - docker.remove() + dest = "/scratch/in" if mode == "history" else "/work" if ns.fetch: - # Phase 1: fetch dependencies with network, nothing else. - fetch = ["bash", "-c", INNER_SCRIPT, "fetch", mode, "0", "cargo", "fetch", "--locked"] - docker.create(fetch, True, target_volume, env, caches_writable=True) + # Phase 1: fetch dependencies with network and writable caches, from a copy that has no + # cargo config or toolchain file, with the compiler pinned and the wrappers cleared. + fetch_tar = work / "fetch.tar" + with open(work / "head.tar" if mode == "history" else tar_path, "rb") as fin, open(fetch_tar, "wb") as fout: + fetch_copy(fin, fout) + prepare(docker, target_volume, ns.evidence) + copy_in(docker, target_volume, fetch_tar, "/work", env) + docker.create(FETCH_COMMAND, True, target_volume, fetch_environment(env, ns.rust), caches_writable=True) if docker.start(ns.timeout) != 0: raise DevrunError("cargo fetch --locked failed") docker.remove() - if mode != "history": - # The fetch phase created the throwaway repository; start phase 2 from a clean copy again. - docker.create(["bash", "-c", PREP_SCRIPT, "prep", "0"], False, target_volume) - docker.start(300) - docker.remove() - docker.create(["true"], False, target_volume, env) - docker.copy_in(tar_path, dest) - docker.start(300) - docker.remove() + # Phase 2 starts from empty volumes and a fresh copy, whatever phase 1 left behind. + prepare(docker, target_volume, ns.evidence and not ns.fetch) + copy_in(docker, target_volume, tar_path, dest, env) # Phase 2: the command, offline unless --network. cmd = ["bash", "-c", INNER_SCRIPT, "run", mode, "1" if ns.export_patch else "0", *ns.command] env["CARGO_NET_OFFLINE"] = "false" if ns.network else "true" @@ -556,7 +619,7 @@ def main(argv: List[str]) -> int: job = f"devrun-{ns.project}-{uuid.uuid4()}" lock = ["git", "locks", "with", "--job", job, "--holder", f"devrun-{os.getpid()}", "--ttl", str(ns.timeout + 900), "--wait", "600", "--note", f"devrun {ns.project}", - "host/heavy-work", f"host/docker/{ns.project}-dev/", "--", + "host/heavy-work", f"host/docker/{ns.project}-dev-run/", "--", sys.executable, os.path.abspath(__file__), *argv] env = dict(os.environ, GIT_LOCKS_STORE=os.environ.get("GIT_LOCKS_STORE", LOCK_STORE), **{LOCK_ENV: job}) return subprocess.call(lock, env=env) diff --git a/tests/integration-devrun b/tests/integration-devrun index 14cd9f2..00dffad 100755 --- a/tests/integration-devrun +++ b/tests/integration-devrun @@ -4,13 +4,13 @@ set -euo pipefail root="$(cd "$(dirname "$0")/.." && pwd)" devrun="$root/bin/devrun" -project=devrun-selftest +project="devrun-selftest" work="$(mktemp -d -t devrun-selftest-XXXXXX)" fail() { echo "integration-devrun: FAIL: $*" >&2; exit 1; } cleanup() { rm -rf "$work" docker rm -f "$project-dev-run" >/dev/null 2>&1 || true - for v in cargo-registry cargo-git target target-evidence scratch; do + for v in cargo-registry cargo-git target target-evidence work scratch; do if [[ "$(docker volume inspect "$project-$v" --format '{{index .Labels "org.flyingrobots.owner"}}' 2>/dev/null || true)" == "$project" ]]; then docker volume rm "$project-$v" >/dev/null fi @@ -33,8 +33,12 @@ Repo-Respect-Receipt: none" head="$(git -C "$repo" rev-parse HEAD)" echo "== commit mode: isolation checks" +# shellcheck disable=SC2016 # the probe's $(...) and $HOME must expand inside the container, not on the host. probe='set -e test "$(id -u)" = 10001 +test "$(pwd)" = /work +test -d "$HOME" && touch "$HOME/probe" +if touch /devrun-probe 2>/dev/null; then echo "the parent of /work is writable" >&2; exit 18; fi test -f README && test -L README.link && test "$(cat README.link | head -1)" = hello test -d .git # created inside the container by devrun, empty baseline only test "$(git log --format=%s)" = "devrun baseline" @@ -67,6 +71,34 @@ out3="$work/out3" bash -c "test \"\$(git rev-parse HEAD)\" = $head && test \"\$(git rev-parse origin/main)\" = $base && git log -1 --format=%B | grep -q 'Repo-Respect-Receipt: none' && echo ok > /scratch/out/result.txt" [[ "$(cat "$out3/result.txt")" == "ok" ]] || fail "history mode" +echo "== fetch phase: a planted cargo config runs no code while the caches are writable" +crate="$work/crate"; mkdir -p "$crate/src" "$crate/.cargo" +printf '[package]\nname = "selftest"\nversion = "0.1.0"\nedition = "2021"\n' > "$crate/Cargo.toml" +printf 'version = 4\n\n[[package]]\nname = "selftest"\nversion = "0.1.0"\n' > "$crate/Cargo.lock" +printf 'fn main() {}\n' > "$crate/src/main.rs" +printf '#!/bin/sh\ntouch /usr/local/cargo/registry/devrun-pwned 2>/dev/null\ntouch /tmp/devrun-wrapper-ran\nexec "$@"\n' > "$crate/evil-wrapper"; chmod 755 "$crate/evil-wrapper" +printf '[build]\nrustc-wrapper = "/work/evil-wrapper"\n' > "$crate/.cargo/config.toml" +printf '[toolchain]\npath = "/work/evil-toolchain"\n' > "$crate/rust-toolchain.toml" +git -C "$crate" init -q -b main && git -C "$crate" add -A && git -C "$crate" -c user.name=t -c user.email=t@invalid commit -qm fetch +out4="$work/out4" +# Two layers protect this phase: the fetch copy drops the config and toolchain files, and the +# environment overrides them. Disabling both makes cargo honour the planted toolchain and fail here. +if ! "$devrun" "$project" "$crate" --commit HEAD --fetch --timeout 300 --export result.txt --out "$out4" -- \ + bash -c 'test ! -e /usr/local/cargo/registry/devrun-pwned && test -f .cargo/config.toml && test -f rust-toolchain.toml && + # Control: with the config present, cargo fetch does call the wrapper, so the check above is not vacuous. + { RUSTUP_TOOLCHAIN=1.96.0 cargo fetch --locked >/dev/null 2>&1 || true; } && test -e /tmp/devrun-wrapper-ran && + echo ok > /scratch/out/result.txt'; then + fail "the fetch phase honoured the planted cargo config or toolchain" +fi +[[ "$(cat "$out4/result.txt")" == "ok" ]] || fail "fetch phase ran the planted wrapper, or phase 2 lost the config files" + +echo "== an existing container with the run's name is refused and left alone" +docker create --name "$project-dev-run" --label org.flyingrobots.owner=other flyingrobots/rust-dev:1.96.0 true >/dev/null +if "$devrun" "$project" "$repo" --commit "$base" --timeout 60 -- true 2>"$work/err-exists"; then fail "ran despite an existing container"; fi +grep -q 'already exists' "$work/err-exists" || fail "wrong refusal: $(cat "$work/err-exists")" +docker ps -a --format '{{.Names}}' | grep -qx "$project-dev-run" || fail "devrun removed a container it did not create" +docker rm "$project-dev-run" >/dev/null + echo "== refusals" ln -s /etc/passwd "$repo/evil"; git -C "$repo" add evil; git -C "$repo" -c user.name=t -c user.email=t@invalid commit -qm "evil" if "$devrun" "$project" "$repo" --commit HEAD --timeout 60 -- true 2>"$work/err"; then fail "absolute symlink accepted"; fi diff --git a/tests/test_devrun.py b/tests/test_devrun.py index 98692c6..c84be11 100644 --- a/tests/test_devrun.py +++ b/tests/test_devrun.py @@ -92,10 +92,16 @@ def test_fifo(self): def test_device(self): self.assert_refused([("dev", "chr", None)], "special file") + def test_setuid(self): + self.assert_refused([("bin/tool", "file", {"mode": 0o4755})], "setuid or setgid") + + def test_setgid(self): + self.assert_refused([("bin/tool", "file", {"mode": 0o2755})], "setuid or setgid") + class SanitizerRewrites(unittest.TestCase): def test_owner_and_mode_bits(self): - n, out = sanitize([("bin/tool", "file", {"mode": 0o4755, "data": b"#!/bin/sh\n"}), ("src", "dir", None)]) + n, out = sanitize([("bin/tool", "file", {"mode": 0o1755, "data": b"#!/bin/sh\n"}), ("src", "dir", None)]) self.assertEqual(n, 2) with tarfile.open(fileobj=out) as t: members = {m.name: m for m in t.getmembers()} @@ -148,6 +154,87 @@ def test_existing_file_is_not_overwritten(self): self.assertEqual((pathlib.Path(d) / "x").read_text(), "keep") +class FetchCopy(unittest.TestCase): + def test_cargo_config_and_toolchain_files_are_excluded_at_every_depth(self): + for path in [".cargo/config.toml", ".cargo/config", "crates/a/.cargo/config.toml", + "rust-toolchain.toml", "rust-toolchain", "crates/a/rust-toolchain.toml"]: + self.assertTrue(devrun.excluded_from_fetch(path), path) + + def test_lookalikes_are_kept(self): + for path in ["config.toml", "src/config.toml", ".cargo/credentials.toml", "cargo/config.toml", + "docs/rust-toolchain.md", "Cargo.toml", "Cargo.lock"]: + self.assertFalse(devrun.excluded_from_fetch(path), path) + + def test_fetch_copy_drops_only_excluded_files(self): + _, clean = sanitize([("Cargo.toml", "file", {}), (".cargo/config.toml", "file", {}), + ("rust-toolchain.toml", "file", {}), ("src/main.rs", "file", {})]) + out = io.BytesIO() + devrun.fetch_copy(clean, out) + out.seek(0) + with tarfile.open(fileobj=out) as t: + names = {m.name for m in t.getmembers() if m.isfile()} + self.assertEqual(names, {"Cargo.toml", "src/main.rs"}) + + + def test_fetch_environment_pins_the_compiler_and_clears_wrappers(self): + env = devrun.fetch_environment({"DEVRUN_HEAD": "abc"}, "1.96.0") + self.assertEqual(env["RUSTUP_TOOLCHAIN"], "1.96.0") + self.assertEqual(env["RUSTC"], "/usr/local/cargo/bin/rustc") + self.assertEqual((env["RUSTC_WRAPPER"], env["RUSTC_WORKSPACE_WRAPPER"]), ("", "")) + self.assertEqual(env["DEVRUN_HEAD"], "abc") + + +class ContainerOwnership(unittest.TestCase): + def setUp(self): + self.calls = [] + self.original_run = devrun.run + devrun.run = lambda cmd, **kw: self.calls.append(cmd) or devrun.subprocess.CompletedProcess(cmd, 0, b"", b"") + + def tearDown(self): + devrun.run = self.original_run + + def test_remove_skips_a_container_this_run_did_not_create(self): + docker = devrun.Docker("geordi", "img", "1", "1g", False) + docker.remove() + self.assertEqual(self.calls, []) + + def test_remove_runs_once_after_create(self): + docker = devrun.Docker("geordi", "img", "1", "1g", False) + docker.create(["true"], False, "geordi-target") + docker.remove() + docker.remove() + self.assertEqual([c for c in self.calls if c[:2] == ["docker", "rm"]], [["docker", "rm", "-f", "geordi-dev-run"]]) + + def test_run_container_mounts_the_copy_at_work_with_home_set(self): + docker = devrun.Docker("geordi", "img", "1", "1g", False) + docker.create(["true"], False, "geordi-target") + create = self.calls[-1] + self.assertIn("type=volume,src=geordi-work,dst=/work", create) + self.assertIn("HOME=/tmp/home", create) + self.assertEqual(create[create.index("--workdir") + 1], "/work") + + +class Locks(unittest.TestCase): + def test_lock_keys_follow_the_container_name(self): + captured = {} + original = devrun.subprocess.call + devrun.subprocess.call = lambda cmd, env=None: captured.setdefault("cmd", cmd) and 0 + try: + env = dict(devrun.os.environ) + env.pop("CI", None) + env.pop(devrun.LOCK_ENV, None) + saved = dict(devrun.os.environ) + devrun.os.environ.clear() + devrun.os.environ.update(env) + devrun.main(["geordi", ".", "--commit", "HEAD", "--", "true"]) + finally: + devrun.subprocess.call = original + devrun.os.environ.clear() + devrun.os.environ.update(saved) + self.assertIn("host/heavy-work", captured["cmd"]) + self.assertIn("host/docker/geordi-dev-run/", captured["cmd"]) + + class Arguments(unittest.TestCase): def test_requires_double_dash(self): with self.assertRaises(devrun.DevrunError): From 15eea72192b027ef406ea0ecb00d22d2a3ebf574 Mon Sep 17 00:00:00 2001 From: James Ross Date: Fri, 13 Nov 2026 14:20:22 -0800 Subject: [PATCH 3/3] Let the devrun integration test remove only the decoy it created CodeRabbit review on f06ae96: the EXIT trap ran docker rm -f on devrun-selftest-dev-run unconditionally, so a container with that name that existed before the test (another run, or someone else's) would be deleted. The test now refuses to start when that container exists, and the trap removes it only while the decoy this script planted is alive. Verified: normal run PASS; with a pre-existing container the test fails with "already exists" and the container is still there afterwards; shellcheck clean. Agent-Session: abe7b061-b12e-4785-a5bf-3fb4b6ce1a0c Agent-Tool: Claude Code --- tests/integration-devrun | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/integration-devrun b/tests/integration-devrun index 00dffad..aa1c0a5 100755 --- a/tests/integration-devrun +++ b/tests/integration-devrun @@ -5,11 +5,17 @@ set -euo pipefail root="$(cd "$(dirname "$0")/.." && pwd)" devrun="$root/bin/devrun" project="devrun-selftest" -work="$(mktemp -d -t devrun-selftest-XXXXXX)" fail() { echo "integration-devrun: FAIL: $*" >&2; exit 1; } +# A container with the run's name belongs to someone else: refuse to start rather than touch it. +if docker container inspect "$project-dev-run" >/dev/null 2>&1; then + fail "container $project-dev-run already exists; remove it or wait for its owner" +fi +work="$(mktemp -d -t devrun-selftest-XXXXXX)" +planted=0 # 1 while the decoy container created by this script exists cleanup() { rm -rf "$work" - docker rm -f "$project-dev-run" >/dev/null 2>&1 || true + # devrun removes its own containers; this script removes only the decoy it created. + if [[ "$planted" == 1 ]]; then docker rm -f "$project-dev-run" >/dev/null 2>&1 || true; fi for v in cargo-registry cargo-git target target-evidence work scratch; do if [[ "$(docker volume inspect "$project-$v" --format '{{index .Labels "org.flyingrobots.owner"}}' 2>/dev/null || true)" == "$project" ]]; then docker volume rm "$project-$v" >/dev/null @@ -94,10 +100,12 @@ fi echo "== an existing container with the run's name is refused and left alone" docker create --name "$project-dev-run" --label org.flyingrobots.owner=other flyingrobots/rust-dev:1.96.0 true >/dev/null +planted=1 if "$devrun" "$project" "$repo" --commit "$base" --timeout 60 -- true 2>"$work/err-exists"; then fail "ran despite an existing container"; fi grep -q 'already exists' "$work/err-exists" || fail "wrong refusal: $(cat "$work/err-exists")" docker ps -a --format '{{.Names}}' | grep -qx "$project-dev-run" || fail "devrun removed a container it did not create" docker rm "$project-dev-run" >/dev/null +planted=0 echo "== refusals" ln -s /etc/passwd "$repo/evil"; git -C "$repo" add evil; git -C "$repo" -c user.name=t -c user.email=t@invalid commit -qm "evil"