Files
claude-config/workflow-bench/review-git.py
T

74 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Run one git command for a bench review, confined to the clone: review-git.py <subcommand> <args>...
The bench's review runs no bare git. It calls this wrapper, its one allowed command (bench.py's
permission_flags). The wrapper is git with everything that could reach outside the clone removed:
it accepts only the review's subcommands, each with no option that reads a file, writes a file or
runs a program; it refuses an argument that names a path outside the clone; and it runs git under
the clone with a scrubbed config, so no global or system config, hook, pager or external diff fires.
Quoting, redirects and `$` are the shell's, never git's, so they cannot smuggle a path past this.
The wrapper lives outside the clone, so the clone is not on Python's import path.
"""
import os
import subprocess
import sys
SUBCOMMANDS = {"log", "diff", "show", "status", "ls-files", "add", "commit"}
# Options that read a file, write a file or run a program; "--no-index" lets diff read any two paths.
DENY_EXACT = {"--no-index", "-t"}
DENY_PREFIX = ("--output", "-O", "--pathspec-from-file", "-F", "--file", "--template",
"--exclude-from", "-X", "--git-dir", "--work-tree", "--exec-path", "--ext-diff", "--textconv")
def reject(reason):
sys.exit(f"review-git.py: refused: {reason}")
def bad_option(token):
name = token.split("=", 1)[0]
return name in DENY_EXACT or any(name.startswith(prefix) for prefix in DENY_PREFIX)
def bad_path(clone, arg):
# A revision such as HEAD~1 or a..b is not a path; a path that escapes the clone is refused.
if arg.startswith("/") or arg.startswith("~"):
return True
if arg == ".." or any(part == ".." for part in arg.split("/")):
return True
target = os.path.realpath(os.path.join(clone, arg))
return os.path.lexists(target) and target != clone and not target.startswith(clone + os.sep)
def main(argv):
if len(argv) < 1 or argv[0] not in SUBCOMMANDS:
reject(f"subcommand must be one of {' '.join(sorted(SUBCOMMANDS))}")
sub, args = argv[0], argv[1:]
clone = os.path.realpath(os.getcwd())
operands_only = False # Everything after "--" is a path operand.
skip_message = False # commit's -m value is free text, never a path.
for token in args:
if skip_message:
skip_message = False
continue
if not operands_only and token == "--":
operands_only = True
elif not operands_only and token.startswith("-"):
if bad_option(token):
reject(f"option not allowed: {token}")
short_m = not token.startswith("--") and token.endswith("m") # -m, -am
if sub == "commit" and "=" not in token and (token == "--message" or short_m):
skip_message = True # the next token is the commit message, free text, never a path.
elif bad_path(clone, token):
reject(f"argument names a path outside the clone: {token}")
env = {**os.environ, "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_SYSTEM": "/dev/null",
"GIT_CONFIG_NOSYSTEM": "1", "GIT_PAGER": "cat", "GIT_TERMINAL_PROMPT": "0"}
for name in ("GIT_EXTERNAL_DIFF", "GIT_SSH", "GIT_SSH_COMMAND", "GIT_PROXY_COMMAND", "GIT_CONFIG"):
env.pop(name, None)
git = ["git", "-c", "core.hooksPath=/dev/null", "-c", "core.fsmonitor=false",
"-c", "core.pager=cat", "-c", "protocol.ext.allow=never", sub, *args]
return subprocess.run(git, cwd=clone, env=env).returncode
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))