From 0a1c744835e75a576caf05f54e23bccc8ebbe352 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:57:45 +0000 Subject: [PATCH 1/2] fork-ci: clean .git and fixed git config before checkout on self-hosted (#83) A self-hosted runner reuses its workspace, and a pull request job can leave .git/config keys (core.fsmonitor, core.hooksPath, attr.tree with a merge driver, includes) that the next job's checkout and git calls would run. - New shared action fork-ci/actions/git-clean: on a self-hosted runner it empties the workspace before actions/checkout (symlinks removed as links, never followed; untrusted workspace paths refused), then refuses a .git config with keys or values actions/checkout does not write. - docs-scope, job-changed, test-select, head-check and pr-queue tested-base run the same check first (tested-base writes no note and still passes). - Every fork-ci git helper runs with GIT_CONFIG_GLOBAL=/dev/null, GIT_CONFIG_NOSYSTEM=1 and hooks, fsmonitor, commit-graph and replace refs off; git_clean.test.py pins each module's copy to one source. Co-Authored-By: Claude Opus 5.5 --- .github/fork-ci-tests.txt | 1 + fork-ci/README.md | 1 + fork-ci/actions/docs-scope/action.yml | 4 + fork-ci/actions/docs-scope/docs-scope.test.py | 7 +- fork-ci/actions/docs-scope/docs_scope.py | 10 +- fork-ci/actions/git-clean/README.md | 66 ++ fork-ci/actions/git-clean/action.yml | 19 + fork-ci/actions/git-clean/git_clean.py | 249 ++++++++ fork-ci/actions/git-clean/git_clean.test.py | 564 ++++++++++++++++++ fork-ci/actions/head-check/action.yml | 6 + fork-ci/actions/head-check/head_check.py | 2 +- fork-ci/actions/head-check/head_check.test.py | 2 +- fork-ci/actions/head-check/head_gate.py | 2 +- fork-ci/actions/job-changed/action.yml | 4 + .../actions/job-changed/job-changed.test.py | 26 +- fork-ci/actions/job-changed/job_changed.py | 10 +- fork-ci/actions/pr-queue/pr_queue.py | 7 +- .../actions/pr-queue/tested-base/action.yml | 9 +- .../pr-queue/tested-base/tested_base.py | 11 +- .../pr-queue/tested-base/tested_base.test.py | 32 +- fork-ci/actions/test-select/action.yml | 4 + .../actions/test-select/test-select.test.py | 6 +- fork-ci/actions/test-select/test_select.py | 6 +- 23 files changed, 1013 insertions(+), 35 deletions(-) create mode 100644 fork-ci/actions/git-clean/README.md create mode 100644 fork-ci/actions/git-clean/action.yml create mode 100644 fork-ci/actions/git-clean/git_clean.py create mode 100644 fork-ci/actions/git-clean/git_clean.test.py diff --git a/.github/fork-ci-tests.txt b/.github/fork-ci-tests.txt index 503397fef9dcd..b18d941ab02e6 100644 --- a/.github/fork-ci-tests.txt +++ b/.github/fork-ci-tests.txt @@ -42,6 +42,7 @@ .github/scripts/fork-workflow-triggers.test.cjs fork-ci/actions/ci-demand/ci_demand.test.py ~13s fork-ci/actions/docs-scope/docs-scope.test.py ~16s +fork-ci/actions/git-clean/git_clean.test.py fork-ci/actions/fleet-route/fleet_route.test.py fork-ci/actions/fleet-watch/fleet_watch.test.py fork-ci/actions/gradle-cache/gradle_cache.test.py diff --git a/fork-ci/README.md b/fork-ci/README.md index 48fede5567cd8..3c847b8d036d8 100644 --- a/fork-ci/README.md +++ b/fork-ci/README.md @@ -64,6 +64,7 @@ Each action's README has a full example, every input and output, and what happen | [fleet-route](actions/fleet-route/README.md) | which runner each job of a run gets: the project's self-hosted pools or hosted | `config`, `vars`, `mode`, `gates` | `runners`, `mode`, `attempt`, `fallback`, `watch` | the fleet config (e.g. `.github/fleet.json`) | | [fleet-watch](actions/fleet-watch/README.md) | re-runs, once, a run that a dispatcher pool's runner cut off | `config`, `verb`, `run-id`, `attempt` | `rerun`, `pending` | the fleet config (its `watch` section) | | [head-check](actions/head-check/README.md) | fails a pull request head that is not the change its review read | `config`, `required-lenses`, `token` | `result` | the pr-queue config | +| [git-clean](actions/git-clean/README.md) | empties a reused self-hosted workspace before checkout; refuses an unknown .git config | `path` | none | none | | [pr-queue](actions/pr-queue/README.md) | a merge queue for an up-to-date-required repository (also a reusable workflow) | `config`, `state-file`, `cache`, `token` | none | the pr-queue config | Two habits every action expects: diff --git a/fork-ci/actions/docs-scope/action.yml b/fork-ci/actions/docs-scope/action.yml index 6a0e7dc90f0a1..bb577cbb75b26 100644 --- a/fork-ci/actions/docs-scope/action.yml +++ b/fork-ci/actions/docs-scope/action.yml @@ -31,6 +31,10 @@ outputs: runs: using: composite steps: + # Before any git call: a .git config that actions/checkout does not write (one an earlier job left) is refused. + - name: Refuse a git config actions/checkout does not write (fork-ci git-clean) + shell: bash + run: exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require # Only code from this action's folder (the caller's pinned SHA) runs; nothing from the checkout. # The script also reads the runner's GITHUB_SHA and GITHUB_EVENT_PATH (HEAD must be the run's commit). - id: scope diff --git a/fork-ci/actions/docs-scope/docs-scope.test.py b/fork-ci/actions/docs-scope/docs-scope.test.py index 42b2b3d79b12c..37bb445158eb4 100644 --- a/fork-ci/actions/docs-scope/docs-scope.test.py +++ b/fork-ci/actions/docs-scope/docs-scope.test.py @@ -889,14 +889,15 @@ class Action(CleanEnv): def test_runs_only_its_own_script(self): runs = re.findall(r'^\s*run: (.*)$', self.text, re.M) - self.assertEqual(runs, ['python3 -I "$GITHUB_ACTION_PATH/docs_scope.py" --config "$CONFIG" ' + self.assertEqual(runs, ['exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require', # the git-clean check of this pinned SHA + 'python3 -I "$GITHUB_ACTION_PATH/docs_scope.py" --config "$CONFIG" ' '--event "$EVENT" --base "$BASE"']) self.assertNotIn('uses:', self.text) self.assertIn('using: composite', self.text) def test_inputs_reach_the_script_by_env_only(self): # never ${{ }} inside run: (script injection) - run = re.search(r'^\s*run: (.*)$', self.text, re.M)[1] - self.assertNotIn('${{', run) + for run in re.findall(r'^\s*run: (.*)$', self.text, re.M): + self.assertNotIn('${{', run) def test_declares_every_output_the_script_writes(self): for name in ('docs_only', 'jobs', 'reason'): diff --git a/fork-ci/actions/docs-scope/docs_scope.py b/fork-ci/actions/docs-scope/docs_scope.py index b7a0d17f4916b..54de3c556533e 100644 --- a/fork-ci/actions/docs-scope/docs_scope.py +++ b/fork-ci/actions/docs-scope/docs_scope.py @@ -47,9 +47,17 @@ class Unclear(Exception): """Anything that makes the answer unknown: the run is full and this is the reason.""" +# Every git call: no global or system config (on a self-hosted runner an earlier job may have written ~/.gitconfig), and +# no hooks, fsmonitor command, commit-graph or replace refs from the repository. The same as +# ../git-clean/git_clean.py's (git_clean.test.py pins them); the action's first step refused any other .git config. +GIT_ENV = {'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1'} +GIT_SAFE = ('-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.commitGraph=false', + '--no-replace-objects') + + def git(*args): """stdout, or None when git fails. Bytes that are not UTF-8 survive as lone surrogates (never a crash).""" - r = subprocess.run(['git', *args], capture_output=True) + r = subprocess.run(['git', *GIT_SAFE, *args], capture_output=True, env={**os.environ, **GIT_ENV}) return r.stdout.decode('utf-8', 'surrogateescape') if r.returncode == 0 else None diff --git a/fork-ci/actions/git-clean/README.md b/fork-ci/actions/git-clean/README.md new file mode 100644 index 0000000000000..33011bd95073e --- /dev/null +++ b/fork-ci/actions/git-clean/README.md @@ -0,0 +1,66 @@ +# git-clean + +A composite action for the **first step of a job, before `actions/checkout`**. A self-hosted runner reuses its +workspace, and an earlier job may have run pull request code there. That code can write `.git/config` keys that +`actions/checkout` does not reset: `core.fsmonitor` (a command git runs during the checkout's own git calls), +`core.hooksPath`, `attr.tree` with a merge driver, an include of another file. The next job would run them before any +of its own steps runs pull request code. + +- **Self-hosted runner:** it empties the workspace (`GITHUB_WORKSPACE`), so the checkout starts from nothing, as on a + GitHub-hosted runner. Symlinks are removed as links, never followed: nothing outside the workspace is touched. +- **GitHub-hosted runner:** each job starts on a fresh disk, so nothing is removed. +- **Both:** it then refuses a `.git` at the checkout path whose config `actions/checkout` does not write (below). + +The fork-ci actions that read a checkout with git (docs-scope, job-changed, test-select, head-check, pr-queue's +tested-base) run the same check as their first step, and every git call of theirs runs with no global or system +config (`GIT_CONFIG_GLOBAL=/dev/null`, `GIT_CONFIG_NOSYSTEM=1`) and with hooks, `core.fsmonitor`, the commit-graph and +replace refs off. tested-base writes no note instead of failing. + +## Usage + +Pin a release commit by its full SHA, never a commit of main ([why](../../README.md#pinning)): + +```yaml +jobs: + build: + runs-on: [self-hosted, build] + steps: + - uses: scuttletoby/t3code/fork-ci/actions/git-clean@ # ci/vN + - uses: actions/checkout@ + with: { fetch-depth: 0 } +``` + +Put it in every job that runs on a self-hosted runner and checks out code, before the first checkout and before any +other step that writes the workspace. With several checkouts, list their paths (`path: | ...`): all of them are +checked. + +| Input | Default | Meaning | +| ------ | ------- | ----------------------------------------------------------------------- | +| `path` | `.` | the checkout path or paths (actions/checkout's `path`), one per line | + +No outputs. A refusal fails the step, so the checkout and every later step do not run. + +## What is refused + +- `GITHUB_WORKSPACE` that is not an absolute folder, is a symlink, is not inside `RUNNER_WORKSPACE`, or holds `HOME`, + `RUNNER_TEMP`, `RUNNER_TOOL_CACHE` or the action itself: nothing is removed. +- A delete that fails (a file of another user, for example from a container): the step fails with the path. What is + left is removed by the next run once the file can be deleted. +- A checkout path that is absolute or leads outside the workspace. +- `/.git` that is a file or a symlink (a worktree or submodule: git would read another repository), or that holds + `commondir`. +- A key in `.git/config` or `.git/config.worktree` that `actions/checkout` (v4 to v7) and `git init` do not write, or + one of those keys with another kind of value. Allowed: the core settings of `git init`, sparse checkout, the object + format, `gc.auto 0`, `remote.origin` url/fetch/partial clone, `branch.*.remote`/`merge`, the token header, the LFS + filter exactly as `git lfs install` writes it, submodule url/active, `user.name`/`user.email`. The include + checkout writes for its token (`includeIf.gitdir:...path`) only to a `git-credentials-.config` in + `RUNNER_TEMP` that holds nothing but the token header. Everything else is refused by name, among it + `core.fsmonitor`, `core.hooksPath`, `core.sshCommand` (checkout's `ssh-key` with `persist-credentials`: use + `persist-credentials: false`), `core.pager`, `attr.tree`, merge/diff/filter drivers, `include.path`, other + includes, `url.*.insteadOf` and `alias.*`. + +## Not covered + +The runner user's own files outside the workspace (`~/.gitconfig`, the system git config) are not removed: the fork-ci +git calls ignore them, `actions/checkout` does not. A runner that takes pull requests from people who are not trusted +also needs a job home that does not outlive the job. diff --git a/fork-ci/actions/git-clean/action.yml b/fork-ci/actions/git-clean/action.yml new file mode 100644 index 0000000000000..e113cffa2e857 --- /dev/null +++ b/fork-ci/actions/git-clean/action.yml @@ -0,0 +1,19 @@ +name: git-clean +description: >- + The first step of a job, before actions/checkout. On a self-hosted runner it empties the reused workspace, so a git + config, hook or file an earlier job (pull request code) left there cannot run during this job's checkout. On a + GitHub-hosted runner (a fresh disk) it removes nothing. Then it refuses a .git config at the checkout path that + actions/checkout does not write. Needs git and python3. +inputs: + path: + description: The checkout path or paths (actions/checkout's path), one per line, relative to the workspace. + required: false + default: "." +runs: + using: composite + steps: + - name: Empty a reused workspace; refuse a git config actions/checkout does not write + shell: bash + env: + CHECKOUT_PATH: ${{ inputs.path }} + run: exec python3 -I "$GITHUB_ACTION_PATH/git_clean.py" prepare --path "$CHECKOUT_PATH" diff --git a/fork-ci/actions/git-clean/git_clean.py b/fork-ci/actions/git-clean/git_clean.py new file mode 100644 index 0000000000000..1a37b3f87fc67 --- /dev/null +++ b/fork-ci/actions/git-clean/git_clean.py @@ -0,0 +1,249 @@ +#!/usr/bin/env python3 +"""A workspace a self-hosted runner reuses is emptied before actions/checkout, and a git config fork-ci did not expect +is refused before fork-ci's own git calls (README.md). + + git_clean.py prepare [--path DIR]... first step of a job, before actions/checkout + git_clean.py check --path DIR [--require] before git reads a checkout (the actions that read one run this) + +An earlier job on the same runner may have run pull request code in the workspace, and that code can write +`.git/config` keys that actions/checkout does not reset: `core.fsmonitor` (a command git runs during checkout's own git +calls), `core.hooksPath`, `attr.tree` with a merge driver, an include of another file. The next job would run them +before any of its own steps runs pull request code. + +prepare: on a self-hosted runner (RUNNER_ENVIRONMENT is not `github-hosted`) every entry of GITHUB_WORKSPACE is +removed, symlinks as links (never what they point to), so nothing outside the workspace is touched. A +GitHub-hosted runner starts each job on a fresh disk, so nothing is removed there. Then `check` runs on each path. + +check: `DIR/.git` must be a folder (not a .git file or symlink, which point git elsewhere), with no `commondir`, and +every key of its config (and config.worktree) must be one actions/checkout writes, with a value of the kind it +writes (ALLOWED). The include it writes for its token is allowed only to a git-credentials file in RUNNER_TEMP that +holds nothing but the token header. Anything else is refused by name: nothing is repaired in place. + +Exit 0: done; 2: refused (the step fails, so the job's checkout and git calls never run). +""" +import argparse, os, re, stat, subprocess, sys, tempfile + +# Every fork-ci git call that reads a checkout runs with this environment and these flags (the actions' modules carry +# copies: git_clean.test.py pins them to these). No global or system config (an earlier job may have written +# ~/.gitconfig on a self-hosted runner); no hooks, fsmonitor command, commit-graph or replace refs from the repository. +GIT_ENV = {'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1'} +GIT_SAFE = ('-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.commitGraph=false', + '--no-replace-objects') + +BOOL = r'(?i:true|false|yes|no|on|off|1|0)' +UUID = r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' +CREDENTIALS = 'credentials' # a value checked by credentials_problem, not by a pattern +HEADER = (r'http\.https://[^/\s]+/\.extraheader', r'AUTHORIZATION: basic [A-Za-z0-9+/=]+') +# (key, value): what actions/checkout (v4 to v7) and `git init` write, as `git config --list` prints a key (section and +# name lower case, a subsection as written). Everything else is refused, among it every key that runs a command or +# changes what git reads: core.fsmonitor, core.hooksPath, core.sshCommand, core.pager, core.attributesFile, attr.tree, +# merge/diff/filter drivers, include and other includeIf, url.*.insteadOf, alias.*, remote uploadpack. +ALLOWED = tuple((re.compile(k), v if v == CREDENTIALS else re.compile(v)) for k, v in ( + (r'core\.repositoryformatversion', r'[01]'), + (r'core\.(filemode|logallrefupdates|ignorecase|precomposeunicode|symlinks|sparsecheckout|sparsecheckoutcone)', + BOOL), + (r'core\.bare', r'(?i:false|no|off|0)'), + (r'extensions\.objectformat', r'sha1|sha256'), + (r'extensions\.worktreeconfig', BOOL), + (r'extensions\.partialclone', r'origin'), + (r'gc\.auto', r'0'), + (r'remote\.origin\.url', r'https://[^\s@]+|git@[A-Za-z0-9.-]+:[^\s]+'), + (r'remote\.origin\.fetch', r'\+?refs/[^\s:]+:refs/[^\s:]+'), + (r'remote\.origin\.promisor', BOOL), + (r'remote\.origin\.partialclonefilter', r'[A-Za-z0-9:=+-]+'), + (r'branch\.[^\s]+\.remote', r'origin'), + (r'branch\.[^\s]+\.merge', r'refs/[^\s]+'), + HEADER, + (r'includeif\.gitdir:.+\.path', CREDENTIALS), + (r'lfs\.repositoryformatversion', r'0'), + (r'filter\.lfs\.clean', r'git-lfs clean -- %f'), + (r'filter\.lfs\.smudge', r'git-lfs smudge -- %f'), + (r'filter\.lfs\.process', r'git-lfs filter-process'), + (r'filter\.lfs\.required', BOOL), + (r'submodule\.[^\s]+\.url', r'https://[^\s@]+'), + (r'submodule\.[^\s]+\.active', BOOL), + (r'user\.(name|email)', r'[^\x00-\x1f\x7f]*'), +)) +HEADER_ONLY = ((re.compile(HEADER[0]), re.compile(HEADER[1])),) +REFUSED = 2 + + +def say(text): + print(text, flush=True) + + +def entries(path, env): + """[(key, value)] of one config file read by git itself (no includes followed, no other config), or raises + ValueError with git's reason.""" + with tempfile.TemporaryDirectory(prefix='git-clean-') as empty: + clean = {'PATH': env.get('PATH') or os.defpath, 'LC_ALL': 'C', 'HOME': empty, 'GIT_TERMINAL_PROMPT': '0', + 'GIT_CEILING_DIRECTORIES': os.path.dirname(empty), **GIT_ENV} + r = subprocess.run(['git', 'config', '--file', path, '--no-includes', '--null', '--list'], cwd=empty, + env=clean, capture_output=True, stdin=subprocess.DEVNULL) + if r.returncode != 0: + raise ValueError(r.stderr.decode('utf-8', 'replace').strip()[:300] or f'git config exit {r.returncode}') + out = [] + for item in r.stdout.decode('utf-8', 'surrogateescape').split('\0'): + if item: + key, nl, value = item.partition('\n') + out.append((key, value if nl else 'true')) # `[core] bare` (no =) means true + return out + + +def credentials_problem(value, env): + """Why an includeIf value is not the token file actions/checkout writes, or None.""" + temp = env.get('RUNNER_TEMP') or '' + names = [r'/github/runner_temp'] # the same file as a container job sees it + if temp: + names.append(re.escape(os.path.realpath(temp))) + names.append(re.escape(temp)) + if not re.fullmatch(rf'(?:{"|".join(names)})/git-credentials-{UUID}\.config', value): + return 'which is not a git-credentials file of actions/checkout in RUNNER_TEMP' + return None + + +def file_problems(path, rules, env, seen=None): + """[why] for one config file: keys outside `rules`, values not of their kind, and the included token file.""" + try: + found = entries(path, env) + except ValueError as e: + return [f'{path} cannot be read as git config ({e})'] + problems = [] + for key, value in found: + rule = next((v for k, v in rules if k.fullmatch(key)), None) + if rule is None: + problems.append(f'{path} holds {key}, which actions/checkout never writes') + elif rule == CREDENTIALS: + why = credentials_problem(value, env) + if why: + problems.append(f'{path} holds {key} = {value!r}, {why}') + elif os.path.lexists(value) and value not in (seen or ()): + problems += file_problems(value, HEADER_ONLY, env, {*(seen or ()), value}) + elif not rule.fullmatch(value): + problems.append(f'{path} holds {key} = {value[:80]!r}, not a value actions/checkout writes') + return problems + + +def problems(repo, env, require=False): + """[why] git at `repo` would read a config fork-ci did not expect; [] when it would not.""" + dot = os.path.join(repo, '.git') + if not os.path.lexists(dot): + return [f'{repo} holds no .git: check it out first (actions/checkout)'] if require else [] + if os.path.islink(dot) or not os.path.isdir(dot): + return [f'{dot} is not a folder (a .git file or symlink points git to another repository)'] + if os.path.lexists(os.path.join(dot, 'commondir')): + return [f'{dot} holds commondir, which makes git read the config of another repository'] + found = [] + for name in ('config', 'config.worktree'): + path = os.path.join(dot, name) + if os.path.lexists(path): + found += file_problems(path, ALLOWED, env) + return found + + +def workspace(env): + """(the workspace's real path, None) or (None, why it is refused).""" + ws = env.get('GITHUB_WORKSPACE') or '' + if not ws or not os.path.isabs(ws): + return None, f'GITHUB_WORKSPACE {ws!r} is not an absolute path' + if os.path.islink(ws) or not os.path.isdir(ws): + return None, f'GITHUB_WORKSPACE {ws} is not a folder (or is a symlink)' + real = os.path.realpath(ws) + if real == '/': + return None, 'GITHUB_WORKSPACE is /' + runner = env.get('RUNNER_WORKSPACE') or '' + if runner: + top = os.path.realpath(runner) + if os.path.commonpath([real, top]) != top or real == top: + return None, f'GITHUB_WORKSPACE {real} is not inside RUNNER_WORKSPACE {top}' + for name in ('HOME', 'RUNNER_TEMP', 'RUNNER_TOOL_CACHE'): + other = env.get(name) or '' + if other and os.path.isabs(other): + o = os.path.realpath(other) + if os.path.commonpath([real, o]) == real: + return None, f'GITHUB_WORKSPACE {real} holds {name} ({o})' + here = os.path.realpath(os.path.dirname(os.path.abspath(__file__))) + if os.path.commonpath([real, here]) == real: + return None, f'GITHUB_WORKSPACE {real} holds this action ({here})' + return real, None + + +def remove(path): + """Remove one entry without following a symlink; a folder is made writable first (a read-only one too).""" + st = os.lstat(path) + if not stat.S_ISDIR(st.st_mode): + os.unlink(path) + return + if st.st_mode & 0o700 != 0o700: + os.chmod(path, stat.S_IMODE(st.st_mode) | 0o700) + with os.scandir(path) as it: + names = [e.path for e in it] + for p in names: + remove(p) + os.rmdir(path) + + +def wipe(env): + """Empty the workspace on a self-hosted runner. None when done or not needed, else why it stopped (refused, or a + delete failed: what is left is removed by the next run).""" + if (env.get('RUNNER_ENVIRONMENT') or '') == 'github-hosted': + say('git-clean: a GitHub-hosted runner starts on a fresh disk: nothing to remove') + return None + real, why = workspace(env) + if why: + return f'refused to empty the workspace: {why}' + with os.scandir(real) as it: + names = sorted(e.path for e in it) + for p in names: + try: + remove(p) + except OSError as e: + return f'could not remove {getattr(e, "filename", None) or p} ({e.strerror or e}): the workspace is half empty' + say(f'git-clean: emptied the workspace {real} ({len(names)} entries)') + return None + + +def inside(env, path): + """(the checkout folder, None) or (None, why): `path` relative to GITHUB_WORKSPACE, and inside it.""" + ws = env.get('GITHUB_WORKSPACE') or os.getcwd() + if os.path.isabs(path): + return None, f'path {path} must be relative, inside the workspace' + full = os.path.normpath(os.path.join(ws, path)) + base = os.path.realpath(ws) + if os.path.commonpath([os.path.realpath(full), base]) != base: + return None, f'path {path} must be inside the workspace {ws}' + return full, None + + +def check(env, paths, require=False): + bad = [] + for p in paths: + repo, why = inside(env, p) + bad += [why] if why else problems(repo, env, require) + for why in bad: + say(f'::error::git-clean: {why}. On a self-hosted runner run fork-ci/actions/git-clean before actions/checkout') + return REFUSED if bad else 0 + + +def main(argv=None, env=None): + env = os.environ if env is None else env + p = argparse.ArgumentParser(prog='git_clean.py', description=__doc__.split('\n\n', 1)[0]) + sub = p.add_subparsers(dest='cmd', required=True) + pre = sub.add_parser('prepare', help='empty a self-hosted workspace, then check') + pre.add_argument('--path', action='append', default=None, help='a checkout path to check (default .)') + chk = sub.add_parser('check', help='refuse a git config actions/checkout does not write') + chk.add_argument('--path', action='append', required=True) + chk.add_argument('--require', action='store_true', help='a missing .git is refused too') + a = p.parse_args(argv) + paths = [x for line in (a.path or ['.']) for x in line.splitlines() if x.strip()] or ['.'] + if a.cmd == 'prepare': + why = wipe(env) + if why: + say(f'::error::git-clean: {why}') + return REFUSED + return check(env, paths) + return check(env, paths, a.require) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/fork-ci/actions/git-clean/git_clean.test.py b/fork-ci/actions/git-clean/git_clean.test.py new file mode 100644 index 0000000000000..aa296cdcee8b4 --- /dev/null +++ b/fork-ci/actions/git-clean/git_clean.test.py @@ -0,0 +1,564 @@ +"""Tests of git_clean.py, its action and the git calls of every fork-ci action that reads a checkout +(run: python3 fork-ci/actions/git-clean/git_clean.test.py). + +A workspace an earlier job left behind is planted the way pull request code could: a .git/config with a command git +runs (core.fsmonitor, core.hooksPath, attr.tree with a merge driver, an include of another file, ...). On a self-hosted +runner `prepare` empties the workspace, so a checkout after it runs nothing planted; on a GitHub-hosted one (or after +checkout, `check`) a config that actions/checkout does not write is refused. Every marker file is one the planted +command would write: a test passes only when it is absent. Nothing here reaches the network. +""" +import io, importlib.util, json, os, shutil, stat, subprocess, sys, tempfile, unittest, unittest.mock + +sys.dont_write_bytecode = True +HERE = os.path.dirname(os.path.abspath(__file__)) +ACTIONS = os.path.dirname(HERE) +sys.path.insert(0, HERE) +import git_clean as G # noqa: E402 + +GIT_ENV = {**os.environ, 'GIT_AUTHOR_NAME': 't', 'GIT_AUTHOR_EMAIL': 't@t', 'GIT_COMMITTER_NAME': 't', + 'GIT_COMMITTER_EMAIL': 't@t', 'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1', + 'GIT_TERMINAL_PROMPT': '0'} +UUID = '0f1e2d3c-4b5a-6978-8a9b-0c1d2e3f4a5b' + + +def load(name, path): + spec = importlib.util.spec_from_file_location(name, path) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +def git(cwd, *a, env=None): + r = subprocess.run(['git', *a], cwd=cwd, text=True, capture_output=True, env=env or GIT_ENV) + if r.returncode != 0: + raise RuntimeError(f'git {a}: {r.stderr}') + return r.stdout.strip() + + +class Base(unittest.TestCase): + def setUp(self): + self.d = os.path.realpath(tempfile.mkdtemp(prefix='git-clean-')) + self.addCleanup(self.rm, self.d) + self.runner = os.path.join(self.d, '_work', 'proj') # RUNNER_WORKSPACE + self.ws = os.path.join(self.runner, 'proj') # GITHUB_WORKSPACE + self.temp = os.path.join(self.d, '_work', '_temp') # RUNNER_TEMP + self.outside = os.path.join(self.d, 'outside') # never touched + for p in (self.ws, self.temp, self.outside): + os.makedirs(p) + self.marker = os.path.join(self.outside, 'MARKER') + with open(os.path.join(self.outside, 'keep'), 'w') as f: + f.write('keep\n') + + @staticmethod + def rm(path): + for root, dirs, _ in os.walk(path): + for x in dirs: + p = os.path.join(root, x) + if not os.path.islink(p): + os.chmod(p, 0o700) + shutil.rmtree(path, True) + + def env(self, runner='self-hosted', **kw): + e = {'GITHUB_WORKSPACE': self.ws, 'RUNNER_WORKSPACE': self.runner, 'RUNNER_TEMP': self.temp, + 'HOME': self.d, 'PATH': os.environ['PATH']} + if runner is not None: + e['RUNNER_ENVIRONMENT'] = runner + e.update(kw) + return {k: v for k, v in e.items() if v is not None} + + def run_cli(self, *argv, env=None): + """(exit code, stdout+stderr) of git_clean.py as the action runs it.""" + r = subprocess.run([sys.executable, '-I', os.path.join(HERE, 'git_clean.py'), *argv], cwd=self.ws, + text=True, capture_output=True, env=env or self.env()) + return r.returncode, r.stdout + r.stderr + + def checkout(self, path='.'): + """A repository the way actions/checkout leaves it (one commit, origin, gc.auto 0, a credentials include).""" + repo = os.path.normpath(os.path.join(self.ws, path)) + os.makedirs(repo, exist_ok=True) + git(repo, 'init', '-q', '-b', 'main') + with open(os.path.join(repo, 'a.txt'), 'w') as f: + f.write('a\n') + git(repo, 'add', 'a.txt') + git(repo, 'commit', '-q', '-m', 'a') + git(repo, 'config', '--local', 'gc.auto', '0') + git(repo, 'remote', 'add', 'origin', 'https://github.com/owner/repo') + git(repo, 'config', 'branch.main.remote', 'origin') + git(repo, 'config', 'branch.main.merge', 'refs/heads/main') + creds = os.path.join(self.temp, f'git-credentials-{UUID}.config') + if os.path.exists(creds): + os.remove(creds) # a new job: the runner empties RUNNER_TEMP + git(repo, 'config', '--file', creds, 'http.https://github.com/.extraheader', 'AUTHORIZATION: basic eDp5') + gitdir = os.path.join(repo, '.git') + git(repo, 'config', f'includeIf.gitdir:{gitdir}.path', creds) + git(repo, 'config', f'includeIf.gitdir:{gitdir}/worktrees/*.path', creds) + git(repo, 'config', 'includeIf.gitdir:/github/workspace/.git.path', + f'/github/runner_temp/git-credentials-{UUID}.config') + return repo + + def plant(self, repo, *pairs): + for key, value in pairs: + git(repo, 'config', '--local', key, value) + + def touch_cmd(self): + return f"sh -c 'echo planted > {self.marker}'" + + +class Planted(Base): + """Each planted key: a real command first shown to run (so the plant is no dud), then refused or wiped.""" + + def assert_plant_runs(self, repo, *cmd): + git(repo, *cmd) + self.assertTrue(os.path.exists(self.marker), 'the planted command did not run: the test plants nothing') + os.remove(self.marker) + + def assert_refused_then_wiped(self, repo, key): + code, out = self.run_cli('check', '--path', os.path.relpath(repo, self.ws), env=self.env('github-hosted')) + self.assertEqual(code, 2, out) + self.assertIn(key, out) + self.assertFalse(os.path.exists(self.marker)) + code, out = self.run_cli('prepare', '--path', '.', env=self.env('github-hosted')) # hosted: no wipe, refused + self.assertEqual(code, 2, out) + self.assertTrue(os.path.isdir(os.path.join(repo, '.git'))) + code, out = self.run_cli('prepare', '--path', '.') # self-hosted: wiped + self.assertEqual(code, 0, out) + self.assertEqual(os.listdir(self.ws), []) + # what actions/checkout then runs in the emptied workspace: nothing planted is left to run + fresh = self.checkout() + git(fresh, 'status') + git(fresh, 'checkout', '-q', '-B', 'other') + self.assertFalse(os.path.exists(self.marker)) + self.assertEqual(self.run_cli('check', '--path', '.', '--require')[0], 0) + + def test_fsmonitor_command(self): + repo = self.checkout() + self.plant(repo, ('core.fsmonitor', self.touch_cmd())) + self.assert_plant_runs(repo, 'status') + self.assert_refused_then_wiped(repo, 'core.fsmonitor') + + def test_hooks_path(self): + repo = self.checkout() + hooks = os.path.join(self.ws, 'hooks') + os.makedirs(hooks) + with open(os.path.join(hooks, 'post-checkout'), 'w') as f: + f.write(f'#!/bin/sh\necho planted > {self.marker}\n') + os.chmod(os.path.join(hooks, 'post-checkout'), 0o755) + self.plant(repo, ('core.hooksPath', hooks)) + self.assert_plant_runs(repo, 'checkout', '-q', '-B', 'x') + self.assert_refused_then_wiped(repo, 'core.hookspath') + + def test_attr_tree_and_merge_driver(self): + repo = self.checkout() + with open(os.path.join(repo, '.gitattributes'), 'w') as f: + f.write('* merge=evil\n') + git(repo, 'add', '.gitattributes') + git(repo, 'commit', '-q', '-m', 'attrs') + git(repo, 'tag', 'attrs') + git(repo, 'rm', '-q', '.gitattributes') + git(repo, 'commit', '-q', '-m', 'attrs gone from the tree; attr.tree still reads them') + self.plant(repo, ('attr.tree', 'attrs'), ('merge.evil.driver', self.touch_cmd())) + base = git(repo, 'rev-parse', 'HEAD') + for b, text in (('one', '1\n'), ('two', '2\n')): + git(repo, 'checkout', '-q', '-b', b, base) + with open(os.path.join(repo, 'a.txt'), 'w') as f: + f.write(text) + git(repo, 'commit', '-q', '-am', b) + subprocess.run(['git', 'merge', '-q', 'one'], cwd=repo, env=GIT_ENV, capture_output=True) + self.assertTrue(os.path.exists(self.marker), 'the planted merge driver did not run') + os.remove(self.marker) + subprocess.run(['git', 'merge', '--abort'], cwd=repo, env=GIT_ENV, capture_output=True) + self.assert_refused_then_wiped(repo, 'attr.tree') + + def test_merge_driver_alone_is_refused(self): + repo = self.checkout() + self.plant(repo, ('merge.evil.driver', self.touch_cmd())) + self.assert_refused_then_wiped(repo, 'merge.evil.driver') + + def test_include_of_another_file(self): + repo = self.checkout() + other = os.path.join(self.ws, 'other.config') + git(self.ws, 'config', '--file', other, 'core.fsmonitor', self.touch_cmd()) + self.plant(repo, ('include.path', other)) + self.assert_plant_runs(repo, 'status') + self.assert_refused_then_wiped(repo, 'include.path') + + def test_an_included_file_is_never_read(self): # a FIFO no one writes would hang the step + repo = self.checkout() + fifo = os.path.join(self.ws, 'pipe.config') + os.mkfifo(fifo) + self.plant(repo, ('include.path', fifo)) + r = subprocess.run([sys.executable, '-I', os.path.join(HERE, 'git_clean.py'), 'check', '--path', '.'], + cwd=self.ws, text=True, capture_output=True, env=self.env('github-hosted'), timeout=30) + self.assertEqual(r.returncode, 2, r.stdout + r.stderr) + self.assertIn('include.path', r.stdout) + + def test_include_if_to_a_planted_file(self): + repo = self.checkout() + other = os.path.join(self.ws, 'other.config') + git(self.ws, 'config', '--file', other, 'core.fsmonitor', self.touch_cmd()) + self.plant(repo, (f'includeIf.gitdir:{repo}/.git.path', other)) + self.assert_plant_runs(repo, 'status') + self.assert_refused_then_wiped(repo, 'includeif.gitdir:') + + def test_credentials_include_with_more_than_the_header(self): + """The include checkout writes is allowed, but only to a file that holds nothing but the token header.""" + repo = self.checkout() + creds = os.path.join(self.temp, f'git-credentials-{UUID}.config') + git(self.ws, 'config', '--file', creds, 'core.fsmonitor', self.touch_cmd()) + self.assert_plant_runs(repo, 'status') + self.assert_refused_then_wiped(repo, 'core.fsmonitor') + + def test_ssh_command_filter_and_diff_drivers(self): + for key in ('core.sshCommand', 'filter.evil.smudge', 'diff.evil.textconv', 'diff.evil.command', + 'core.pager', 'remote.origin.uploadpack', 'core.attributesFile', 'alias.st'): + with self.subTest(key=key): + self.rm(self.ws) + os.makedirs(self.ws) + repo = self.checkout() + self.plant(repo, (key, self.touch_cmd())) + self.assert_refused_then_wiped(repo, key.lower()) + + def test_config_worktree_is_read_too(self): + repo = self.checkout() + self.plant(repo, ('extensions.worktreeConfig', 'true')) + git(repo, 'config', '--worktree', 'core.fsmonitor', self.touch_cmd()) + self.assert_plant_runs(repo, 'status') + self.assert_refused_then_wiped(repo, 'config.worktree') + + def test_commondir_points_git_elsewhere(self): + repo = self.checkout() + with open(os.path.join(repo, '.git', 'commondir'), 'w') as f: + f.write(self.outside + '\n') + self.assert_refused_then_wiped(repo, 'commondir') + + def test_dot_git_file_and_symlink(self): + """A worktree's or submodule's .git FILE points git elsewhere; a .git symlink too.""" + elsewhere = os.path.join(self.outside, 'repo') + git(self.outside, 'init', '-q', elsewhere) + for kind in ('file', 'symlink'): + with self.subTest(kind=kind): + self.rm(self.ws) + os.makedirs(self.ws) + dot = os.path.join(self.ws, '.git') + if kind == 'file': + with open(dot, 'w') as f: + f.write(f'gitdir: {elsewhere}/.git\n') + else: + os.symlink(os.path.join(elsewhere, '.git'), dot) + code, out = self.run_cli('check', '--path', '.', env=self.env('github-hosted')) + self.assertEqual(code, 2, out) + self.assertIn('not a folder', out) + self.assertEqual(self.run_cli('prepare', '--path', '.')[0], 0) + self.assertFalse(os.path.lexists(dot)) + self.assertTrue(os.path.isdir(os.path.join(elsewhere, '.git'))) # the target is outside: kept + + def test_unreadable_config_is_refused(self): + repo = self.checkout() + with open(os.path.join(repo, '.git', 'config'), 'a') as f: + f.write('[core\n\tbroken\n') + code, out = self.run_cli('check', '--path', '.', env=self.env('github-hosted')) + self.assertEqual(code, 2, out) + self.assertIn('cannot be read', out) + + +class Allowed(Base): + def test_what_checkout_writes_passes(self): + repo = self.checkout() + self.plant(repo, ('core.sparseCheckout', 'true'), ('core.sparseCheckoutCone', 'false'), + ('extensions.worktreeConfig', 'true'), ('remote.origin.promisor', 'true'), + ('remote.origin.partialclonefilter', 'blob:none'), ('lfs.repositoryformatversion', '0'), + ('filter.lfs.clean', 'git-lfs clean -- %f'), ('filter.lfs.smudge', 'git-lfs smudge -- %f'), + ('filter.lfs.process', 'git-lfs filter-process'), ('filter.lfs.required', 'true'), + ('http.https://github.com/.extraheader', 'AUTHORIZATION: basic eDp5'), + ('submodule.lib.url', 'https://github.com/owner/lib'), ('submodule.lib.active', 'true'), + ('user.name', 'ci'), ('user.email', 'ci@example.com')) + git(repo, 'config', '--worktree', 'core.sparseCheckout', 'true') + self.assertEqual(G.problems(repo, self.env()), []) + code, out = self.run_cli('check', '--path', '.', '--require', env=self.env('github-hosted')) + self.assertEqual(code, 0, out) + + def test_values_are_checked_not_only_keys(self): + for key, value in (('gc.auto', '1'), ('remote.origin.url', 'ext::sh -c touch% /tmp/x'), + ('filter.lfs.smudge', 'sh -c x'), ('branch.main.remote', '.'), + ('core.bare', 'true'), ('user.name', 'a\nb'), + (f'includeIf.gitdir:{self.ws}/.git.path', f'{self.temp}/git-credentials-x.config'), + (f'includeIf.gitdir:{self.ws}/.git.path', f'{self.outside}/git-credentials-{UUID}.config')): + with self.subTest(key=key, value=value): + self.rm(self.ws) + os.makedirs(self.ws) + repo = self.checkout() + self.plant(repo, (key, value)) + self.assertTrue(G.problems(repo, self.env()), (key, value)) + + def test_no_checkout_yet(self): + """GitHub-hosted, before actions/checkout: an empty workspace has nothing to check; --require wants a .git.""" + self.assertEqual(self.run_cli('prepare', '--path', '.', env=self.env('github-hosted'))[0], 0) + code, out = self.run_cli('check', '--path', '.', '--require', env=self.env('github-hosted')) + self.assertEqual(code, 2, out) + self.assertIn('no .git', out) + + +class Wipe(Base): + def fill(self): + repo = self.checkout() + os.makedirs(os.path.join(self.ws, 'sub', 'deep')) + with open(os.path.join(self.ws, 'sub', 'deep', 'f'), 'w') as f: + f.write('x') + os.symlink(self.outside, os.path.join(self.ws, 'link-out')) # a symlink to outside + os.symlink(os.path.join(self.outside, 'keep'), os.path.join(self.ws, 'sub', 'file-link')) + ro = os.path.join(repo, '.git', 'readonly') # a read-only file in .git + with open(ro, 'w') as f: + f.write('x') + os.chmod(ro, 0o400) + os.makedirs(os.path.join(repo, '.git', 'locked', 'in')) # a read-only folder in .git + with open(os.path.join(repo, '.git', 'locked', 'in', 'f'), 'w') as f: + f.write('x') + os.chmod(os.path.join(repo, '.git', 'locked', 'in'), 0o500) + os.chmod(os.path.join(repo, '.git', 'locked'), 0o000) + return repo + + def assert_outside_kept(self): + self.assertEqual(sorted(os.listdir(self.outside)), ['keep']) + with open(os.path.join(self.outside, 'keep')) as f: + self.assertEqual(f.read(), 'keep\n') + self.assertTrue(os.path.isdir(self.temp)) + self.assertTrue(os.path.isdir(self.ws)) + + def test_self_hosted_empties_the_workspace_only(self): + self.fill() + code, out = self.run_cli('prepare', '--path', '.') + self.assertEqual(code, 0, out) + self.assertEqual(os.listdir(self.ws), []) + self.assert_outside_kept() + + def test_again_and_on_an_empty_workspace(self): + self.fill() + for _ in range(3): # two jobs in a row on one runner, then an empty one + self.assertEqual(self.run_cli('prepare', '--path', '.')[0], 0) + self.assertEqual(os.listdir(self.ws), []) + self.assert_outside_kept() + + def test_unknown_runner_counts_as_self_hosted(self): + self.fill() + self.assertEqual(self.run_cli('prepare', '--path', '.', env=self.env(None))[0], 0) + self.assertEqual(os.listdir(self.ws), []) + + def test_github_hosted_keeps_files(self): + self.checkout() + self.assertEqual(self.run_cli('prepare', '--path', '.', env=self.env('github-hosted'))[0], 0) + self.assertIn('a.txt', os.listdir(self.ws)) + + def test_refuses_a_workspace_it_cannot_trust(self): + self.fill() + before = sorted(os.listdir(self.ws)) + for bad in ({'GITHUB_WORKSPACE': None}, {'GITHUB_WORKSPACE': 'proj'}, {'GITHUB_WORKSPACE': '/'}, + {'GITHUB_WORKSPACE': self.d}, # HOME + {'GITHUB_WORKSPACE': self.outside}, # not inside RUNNER_WORKSPACE + {'GITHUB_WORKSPACE': self.runner}, # RUNNER_WORKSPACE itself + {'GITHUB_WORKSPACE': os.path.join(self.ws, 'nope')}, # missing + {'GITHUB_WORKSPACE': os.path.join(self.d, '_work'), 'RUNNER_WORKSPACE': None}): # holds RUNNER_TEMP + with self.subTest(bad=bad): + code, out = self.run_cli('prepare', '--path', '.', env=self.env(**bad)) + self.assertEqual(code, 2, out) + self.assertIn('refused', out) + self.assertEqual(sorted(os.listdir(self.ws)), before) + self.assert_outside_kept() + + def test_symlinked_workspace_is_refused(self): + link = os.path.join(self.runner, 'linked') + os.symlink(self.outside, link) + code, out = self.run_cli('prepare', '--path', '.', env=self.env(GITHUB_WORKSPACE=link)) + self.assertEqual(code, 2, out) + self.assert_outside_kept() + + def test_symlink_to_a_folder_inside_the_runner_workspace_is_refused(self): # never followed, even inside + other = os.path.join(self.runner, 'other') + os.makedirs(other) + with open(os.path.join(other, 'f'), 'w') as f: + f.write('x') + link = os.path.join(self.runner, 'linked') + os.symlink(other, link) + code, out = self.run_cli('prepare', '--path', '.', env=self.env(GITHUB_WORKSPACE=link)) + self.assertEqual(code, 2, out) + self.assertIn('is a symlink', out) + self.assertEqual(os.listdir(other), ['f']) + + def test_workspace_that_holds_the_action_is_refused(self): # e.g. a local action path run from the workspace + self.fill() + own = os.path.join(self.ws, 'action') + os.makedirs(own) + shutil.copy(os.path.join(HERE, 'git_clean.py'), own) + before = sorted(os.listdir(self.ws)) + r = subprocess.run([sys.executable, '-I', os.path.join(own, 'git_clean.py'), 'prepare'], cwd=self.ws, + text=True, capture_output=True, env=self.env()) + self.assertEqual(r.returncode, 2, r.stdout + r.stderr) + self.assertIn('holds this action', r.stdout) + self.assertEqual(sorted(os.listdir(self.ws)), before) + + def test_the_root_is_refused_by_name(self): # checked without removing anything + self.assertEqual(G.workspace({'GITHUB_WORKSPACE': '/'}), (None, 'GITHUB_WORKSPACE is /')) + + def test_stops_half_way_then_runs_again(self): + """A delete that fails stops the step red (checkout never runs on a half-emptied workspace); the next run + finishes the job.""" + self.fill() + real = os.unlink + calls = [] + + def flaky(p, *a, **kw): + calls.append(p) + if len(calls) == 2: + raise PermissionError(13, 'denied', p) + return real(p, *a, **kw) + + with unittest.mock.patch.object(G.os, 'unlink', flaky): + why = G.wipe(self.env()) + self.assertIn('could not remove', why) + self.assertNotEqual(os.listdir(self.ws), []) + self.assertIsNone(G.wipe(self.env())) + self.assertEqual(os.listdir(self.ws), []) + self.assert_outside_kept() + + def test_path_outside_the_workspace_is_refused(self): + for p in ('/etc', '../x', 'a/../../x', self.ws, os.path.join(self.ws, 'sub')): # absolute, even inside + with self.subTest(path=p): + code, out = self.run_cli('check', '--path', p, env=self.env('github-hosted')) + self.assertEqual(code, 2, out) + self.assertIn('inside the workspace', out) + + +class FixedGitConfig(Base): + """Every fork-ci git call that reads a checkout runs with no global or system config and the safe flags: a broken + global/system config (any key in it would be read) breaks nothing, a planted fsmonitor runs nothing.""" + + def setUp(self): + super().setUp() + self.repo = self.checkout() + self.plant(self.repo, ('core.fsmonitor', self.touch_cmd())) + broken = os.path.join(self.outside, 'broken.config') + with open(broken, 'w') as f: + f.write('[core\n') + self.hostile = {'GIT_CONFIG_GLOBAL': broken, 'GIT_CONFIG_SYSTEM': broken, 'GIT_CONFIG_NOSYSTEM': '0', + 'HOME': self.outside} + + def helpers(self): + """(name, call(*git args) -> stdout or None) of every module's own git helper.""" + jc = load('job_changed', os.path.join(ACTIONS, 'job-changed', 'job_changed.py')) + ds = load('docs_scope', os.path.join(ACTIONS, 'docs-scope', 'docs_scope.py')) + ts = load('test_select', os.path.join(ACTIONS, 'test-select', 'test_select.py')) + tb = load('tested_base', os.path.join(ACTIONS, 'pr-queue', 'tested-base', 'tested_base.py')) + sys.path.insert(0, os.path.join(ACTIONS, 'pr-queue')) + hc = load('head_check', os.path.join(ACTIONS, 'head-check', 'head_check.py')) + repo = self.repo + + def in_repo(fn): + def call(*a): + old = os.getcwd() + os.chdir(repo) + try: + return fn(*a) + finally: + os.chdir(old) + return call + + def test_select(*a): + try: + return ts.git(*a, cwd=repo) + except ts.AllTests: + return None + + def head_check(*a): + r = hc.Checkout(repo).git(*a) + return r.stdout if r.returncode == 0 else None + + return (('job_changed', in_repo(jc.git)), ('docs_scope', in_repo(ds.git)), ('test_select', test_select), + ('tested_base', lambda *a: tb.git(repo, *a)), ('head_check', head_check)) + + def test_helpers_ignore_global_and_system_config(self): + with unittest.mock.patch.dict(os.environ, self.hostile): + for name, call in self.helpers(): + with self.subTest(module=name): + self.assertTrue(call('rev-parse', 'HEAD'), f'{name}: git read the global or system config') + + def test_helpers_run_no_planted_fsmonitor(self): + for name, call in self.helpers(): + if name == 'head_check': + continue # --git-dir with no work tree: status cannot run, so fsmonitor never could + with self.subTest(module=name): + call('status') + self.assertFalse(os.path.exists(self.marker), f'{name}: git ran the planted core.fsmonitor') + if os.path.exists(self.marker): + os.remove(self.marker) + + def test_queue_and_gate_git_ignore_global_and_system_config(self): + """pr-queue's cache git and init, and the head gate's init (bare repositories, not a checkout).""" + sys.path.insert(0, os.path.join(ACTIONS, 'pr-queue')) + sys.path.insert(0, os.path.join(ACTIONS, 'head-check')) + import pr_queue + import head_gate + q = object.__new__(pr_queue.Queue) + q.cache, q.read_token = os.path.join(self.d, 'cache', 'repo.git'), None + with unittest.mock.patch.dict(os.environ, self.hostile), \ + unittest.mock.patch('sys.stdout', new_callable=io.StringIO) as out: + self.assertTrue(q.own_cache(), out.getvalue()) # git init --bare + self.assertEqual(q.git('rev-parse', '--is-bare-repository').stdout.strip(), 'true') # Cache.git + path, why = head_gate.bare_repository(self.d, 'gate') + self.assertIsNone(why) + self.assertTrue(os.path.isdir(path)) + + def test_one_source(self): + """The constants each self-contained module carries are git_clean's (docs_scope must stay one file for + actions/pinned, so it cannot import them).""" + sys.path.insert(0, os.path.join(ACTIONS, 'pr-queue')) + import pr_queue + self.assertEqual(pr_queue.GIT_ENV, G.GIT_ENV) + self.assertEqual(pr_queue.GIT_SAFE, G.GIT_SAFE) + for folder, name in (('job-changed', 'job_changed'), ('docs-scope', 'docs_scope'), + ('test-select', 'test_select'), ('pr-queue/tested-base', 'tested_base')): + with self.subTest(module=name): + mod = load(name, os.path.join(ACTIONS, folder, f'{name}.py')) + self.assertEqual(mod.GIT_ENV, G.GIT_ENV) + self.assertEqual(mod.GIT_SAFE, G.GIT_SAFE) + + +class Actions(unittest.TestCase): + """Every action whose code runs git in a checkout runs `git_clean.py check` first; git-clean's own action runs + `prepare`. Found by reading the folders, so a new action that calls git is caught too.""" + + # Actions whose git calls never read a checkout: pr-queue runs git only in its own bare cache (whole config + # compared with CACHE_CONFIG), head-check's gate in a bare repository it makes. + OWN_REPOSITORY = {'pr-queue'} + + def action_files(self): + for root, _, files in os.walk(ACTIONS): + if 'action.yml' in files and os.path.basename(root) != 'git-clean': + yield root, os.path.join(root, 'action.yml') + + def test_actions_that_run_git_check_first(self): + seen = [] + for root, path in self.action_files(): + code = '' + for f in os.listdir(root): + if f.endswith('.py') and not f.endswith('.test.py'): + with open(os.path.join(root, f), encoding='utf-8') as fh: + code += fh.read() + if "['git'" not in code or os.path.relpath(root, ACTIONS) in self.OWN_REPOSITORY: + continue + seen.append(os.path.relpath(root, ACTIONS)) + with open(path, encoding='utf-8') as fh: + text = fh.read() + steps = text.split('\n steps:\n', 1)[1] + first = ('\n' + steps).split('\n - ')[1] + self.assertIn('git-clean/git_clean.py" check --path', first, f'{path}: the first step must be the check') + self.assertIn('--require', first, path) + self.assertEqual(sorted(seen), ['docs-scope', 'head-check', 'job-changed', 'pr-queue/tested-base', + 'test-select']) + + def test_git_clean_action_runs_prepare(self): + with open(os.path.join(HERE, 'action.yml'), encoding='utf-8') as fh: + text = fh.read() + self.assertIn('git_clean.py" prepare --path "$CHECKOUT_PATH"', text) + + +if __name__ == '__main__': + unittest.main() diff --git a/fork-ci/actions/head-check/action.yml b/fork-ci/actions/head-check/action.yml index c651e35ca7beb..18d021cd7b808 100644 --- a/fork-ci/actions/head-check/action.yml +++ b/fork-ci/actions/head-check/action.yml @@ -38,6 +38,12 @@ outputs: runs: using: composite steps: + # Before any git call: a .git config that actions/checkout does not write (one an earlier job left) is refused. + - name: Refuse a git config actions/checkout does not write (fork-ci git-clean) + shell: bash + env: + CHECKOUT_PATH: ${{ inputs.path }} + run: exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path "$CHECKOUT_PATH" --require - id: check name: The head is the change the review read shell: bash diff --git a/fork-ci/actions/head-check/head_check.py b/fork-ci/actions/head-check/head_check.py index e9d4cc159f07a..2e9f6465651be 100644 --- a/fork-ci/actions/head-check/head_check.py +++ b/fork-ci/actions/head-check/head_check.py @@ -76,7 +76,7 @@ def git(self, *a): with tempfile.TemporaryDirectory(prefix='head-check-') as empty: return subprocess.run(['git', *GIT_SAFE, *self.auth, '--git-dir', self.git_dir, *a], cwd=empty, text=True, capture_output=True, env={**os.environ, 'GIT_TERMINAL_PROMPT': '0', - 'GIT_ATTR_NOSYSTEM': '1'}) + 'GIT_ATTR_NOSYSTEM': '1', **pr_queue.GIT_ENV}) def problem(self): """Why the repository cannot be read as git made it, or None: not a git folder, shallow (the base's history is diff --git a/fork-ci/actions/head-check/head_check.test.py b/fork-ci/actions/head-check/head_check.test.py index 24c29c0a82875..45329110dc56c 100644 --- a/fork-ci/actions/head-check/head_check.test.py +++ b/fork-ci/actions/head-check/head_check.test.py @@ -1061,7 +1061,7 @@ def setUp(self): os.chmod(os.path.join(self.bin, 'python3'), 0o755) def run_step(self, **env): - step = self.action['runs']['steps'][0] + step = next(s for s in self.action['runs']['steps'] if s.get('id') == 'check') base = {'REPO': REPO, 'PR': '7', 'CONFIG': CONFIG_PATH, 'CHECKOUT_PATH': '.', 'REQUIRED_LENSES': '', 'PASS_DRAFTS': 'false', 'GITHUB_ACTION_PATH': HERE} return subprocess.run(['bash', '--noprofile', '--norc', '-eo', 'pipefail', '-c', step['run']], diff --git a/fork-ci/actions/head-check/head_gate.py b/fork-ci/actions/head-check/head_gate.py index c48d81c9ba5aa..a11cab5821f38 100644 --- a/fork-ci/actions/head-check/head_gate.py +++ b/fork-ci/actions/head-check/head_gate.py @@ -577,7 +577,7 @@ def bare_repository(work, name): except OSError as e: return None, f'could not make a folder in {work}: {e}' r = subprocess.run(['git', *pr_queue.GIT_SAFE, 'init', '-q', '--bare', '--template=', path], text=True, - capture_output=True) + capture_output=True, env={**os.environ, **pr_queue.GIT_ENV}) if r.returncode != 0: return None, f'git init failed: {r.stderr.strip()[:300]}' with open(os.path.join(path, 'config'), 'w', encoding='utf-8') as f: diff --git a/fork-ci/actions/job-changed/action.yml b/fork-ci/actions/job-changed/action.yml index f35535c3cba8c..2ba9bd3bbb26b 100644 --- a/fork-ci/actions/job-changed/action.yml +++ b/fork-ci/actions/job-changed/action.yml @@ -41,6 +41,10 @@ outputs: runs: using: composite steps: + # Before any git call: a .git config that actions/checkout does not write (one an earlier job left) is refused. + - name: Refuse a git config actions/checkout does not write (fork-ci git-clean) + shell: bash + run: exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require # Only code from this action's folder (the caller's pinned SHA) runs, and the workflow is read # from git objects (git show :), never from the working tree. - id: changed diff --git a/fork-ci/actions/job-changed/job-changed.test.py b/fork-ci/actions/job-changed/job-changed.test.py index 8254acf68e98e..cc6b8951c9a6b 100644 --- a/fork-ci/actions/job-changed/job-changed.test.py +++ b/fork-ci/actions/job-changed/job-changed.test.py @@ -58,12 +58,12 @@ NONE, BOTH = {'build': 'false', 'plugins': 'false'}, {'build': 'true', 'plugins': 'true'} -def step(): - """(env, run) of the action's one step.""" +def steps(): + """[(env, run)] of the action's steps: the git-clean check, then job_changed.py.""" with open(ACTION) as f: - steps = yaml.safe_load(f)['runs']['steps'] - assert len(steps) == 1 - return steps[0]['env'], steps[0]['run'] + found = yaml.safe_load(f)['runs']['steps'] + assert len(found) == 2 and 'git-clean/git_clean.py" check' in found[0]['run'], found + return [(s.get('env', {}), s['run']) for s in found] class Repo(unittest.TestCase): @@ -100,14 +100,14 @@ def outputs(self, job, base=None, **inputs): """{name: value} the action writes to $GITHUB_OUTPUT, its run text run with bash -e as the runner runs it.""" out = os.path.join(self.d, '.output') open(out, 'w').close() - env, run = step() given = {'base': self.base if base is None else base, 'head': 'HEAD', 'workflow': CI, 'job': job, 'paths': '', **inputs} - values = {k: given[re.fullmatch(r'\$\{\{ inputs\.(\w+) \}\}', v)[1]] for k, v in env.items()} - r = subprocess.run(['bash', '--noprofile', '--norc', '-e', '-c', run], cwd=self.d, text=True, - capture_output=True, env={**os.environ, **ENV, **values, 'GITHUB_OUTPUT': out, - 'GITHUB_ACTION_PATH': HERE}) - self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + for env, run in steps(): + values = {k: given[re.fullmatch(r'\$\{\{ inputs\.(\w+) \}\}', v)[1]] for k, v in env.items()} + r = subprocess.run(['bash', '--noprofile', '--norc', '-e', '-c', run], cwd=self.d, text=True, + capture_output=True, env={**os.environ, **ENV, **values, 'GITHUB_OUTPUT': out, + 'GITHUB_ACTION_PATH': HERE, 'GITHUB_WORKSPACE': self.d}) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) self.log = r.stdout with open(out) as f: return dict(line.split('=', 1) for line in f.read().splitlines() if '=' in line) @@ -312,6 +312,7 @@ def test_a_shallow_checkout_without_the_base_builds(self): clone = os.path.join(self.d, 'shallow') subprocess.run(['git', 'clone', '-q', '--depth', '1', f'file://{self.d}', clone], check=True, env={**os.environ, **ENV}) self.d = clone + self.git('remote', 'set-url', 'origin', 'https://github.com/owner/repo') # as actions/checkout writes it self.assertEqual(self.runs('plugins', first), 'true') def test_with_an_unrelated_ci_change_nothing_builds(self): @@ -445,7 +446,8 @@ class Action(unittest.TestCase): text = open(ACTION).read() def test_runs_only_its_own_script_with_inputs_by_env(self): # never ${{ }} inside run: (script injection) - env, run = step() + (_, check), (env, run) = steps() + self.assertEqual(check, 'exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require') self.assertTrue(run.startswith('python3 -I "$GITHUB_ACTION_PATH/job_changed.py" '), run) self.assertNotIn('${{', run) self.assertNotIn('uses:', self.text) diff --git a/fork-ci/actions/job-changed/job_changed.py b/fork-ci/actions/job-changed/job_changed.py index 1fdb502fb1df2..5a4dbc80d2376 100644 --- a/fork-ci/actions/job-changed/job_changed.py +++ b/fork-ci/actions/job-changed/job_changed.py @@ -30,9 +30,17 @@ NO_YAML = 'PyYAML is not installed on this runner (install python3-yaml), the change cannot be read' +# Every git call: no global or system config (on a self-hosted runner an earlier job may have written ~/.gitconfig), and +# no hooks, fsmonitor command, commit-graph or replace refs from the repository. The same as +# ../git-clean/git_clean.py's (git_clean.test.py pins them); the action's first step refused any other .git config. +GIT_ENV = {'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1'} +GIT_SAFE = ('-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.commitGraph=false', + '--no-replace-objects') + + def git(*args): """stdout, or None when git fails.""" - r = subprocess.run(['git', *args], capture_output=True) + r = subprocess.run(['git', *GIT_SAFE, *args], capture_output=True, env={**os.environ, **GIT_ENV}) return r.stdout.decode('utf-8', 'surrogateescape') if r.returncode == 0 else None diff --git a/fork-ci/actions/pr-queue/pr_queue.py b/fork-ci/actions/pr-queue/pr_queue.py index 3b41f0aebc530..a1a5fe4b198e1 100755 --- a/fork-ci/actions/pr-queue/pr_queue.py +++ b/fork-ci/actions/pr-queue/pr_queue.py @@ -74,6 +74,9 @@ # commit-graph (a planted objects/info/commit-graph(s) could change the parents git reports; git rebuilds nothing from it). GIT_SAFE = ('-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.commitGraph=false', '--no-replace-objects') +# ... and no global or system config: on a self-hosted runner an earlier job may have written ~/.gitconfig. The same +# two as ../git-clean/git_clean.py's (git_clean.test.py pins GIT_SAFE and GIT_ENV to its). +GIT_ENV = {'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1'} # The cache's whole config, as the queue writes it: a cache with any other config (a planted `url.*.insteadOf`, helper # or hook path) is rebuilt, never run. CACHE_CONFIG = '[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = true\n' @@ -372,7 +375,7 @@ def git(self, *a): token = self.read_token env = self.gh_env(token) if token else {k: v for k, v in os.environ.items() if k not in TOKEN_VARS} return subprocess.run(['git', *GIT_SAFE, *(GIT_AUTH if token else GIT_AUTH[:2]), '--git-dir', self.cache, *a], - text=True, capture_output=True, env={**env, 'GIT_TERMINAL_PROMPT': '0'}) + text=True, capture_output=True, env={**env, 'GIT_TERMINAL_PROMPT': '0', **GIT_ENV}) # --- step 1 and 4: the queue's own git repository ------------------------------------------------------------- def cache_problem(self): @@ -428,7 +431,7 @@ def own_cache(self): print(f'could not make {new}: {e}') return False r = subprocess.run(['git', *GIT_SAFE, 'init', '-q', '--bare', '--template=', new], text=True, - capture_output=True, env=no_token_env()) + capture_output=True, env={**no_token_env(), **GIT_ENV}) if r.returncode != 0: print(f'could not make {self.cache}: {r.stderr.strip()[:300]}') return False diff --git a/fork-ci/actions/pr-queue/tested-base/action.yml b/fork-ci/actions/pr-queue/tested-base/action.yml index e0c97127e104c..08d026b87d6d9 100644 --- a/fork-ci/actions/pr-queue/tested-base/action.yml +++ b/fork-ci/actions/pr-queue/tested-base/action.yml @@ -12,8 +12,15 @@ inputs: runs: using: composite steps: + # Before any git call, the checkout's .git config must be one actions/checkout writes (fork-ci git-clean): when it + # is not (one an earlier job left), no git runs and no note is written. Like a missing note, that only costs an + # update, so the step still passes. - name: Note the base this run tests shell: bash env: CHECKOUT_PATH: ${{ inputs.path }} - run: python3 -I "$GITHUB_ACTION_PATH/tested_base.py" --path "$CHECKOUT_PATH" + run: | + if python3 -I "$GITHUB_ACTION_PATH/../../git-clean/git_clean.py" check --path "$CHECKOUT_PATH" --require; then + exec python3 -I "$GITHUB_ACTION_PATH/tested_base.py" --path "$CHECKOUT_PATH" + fi + echo "::warning::pr_queue tested base: no note, git-clean refused the checkout's git config (above)" diff --git a/fork-ci/actions/pr-queue/tested-base/tested_base.py b/fork-ci/actions/pr-queue/tested-base/tested_base.py index 101c510962d3a..46fc97d6067ee 100644 --- a/fork-ci/actions/pr-queue/tested-base/tested_base.py +++ b/fork-ci/actions/pr-queue/tested-base/tested_base.py @@ -18,8 +18,17 @@ SHA = re.compile(r'[0-9a-f]{40}') +# Every git call: no global or system config (on a self-hosted runner an earlier job may have written ~/.gitconfig), and +# no hooks, fsmonitor command, commit-graph or replace refs from the repository. The same as +# ../../git-clean/git_clean.py's (git_clean.test.py pins them); the action's first step refused any other .git config. +GIT_ENV = {'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1'} +GIT_SAFE = ('-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.commitGraph=false', + '--no-replace-objects') + + def git(path, *a): - r = subprocess.run(['git', '-C', path, *a], text=True, capture_output=True) + r = subprocess.run(['git', *GIT_SAFE, '-C', path, *a], text=True, capture_output=True, + env={**os.environ, **GIT_ENV}) return r.stdout.strip() if r.returncode == 0 else None diff --git a/fork-ci/actions/pr-queue/tested-base/tested_base.test.py b/fork-ci/actions/pr-queue/tested-base/tested_base.test.py index 4e67ec314a8b4..7ff4437548dc1 100644 --- a/fork-ci/actions/pr-queue/tested-base/tested_base.test.py +++ b/fork-ci/actions/pr-queue/tested-base/tested_base.test.py @@ -124,17 +124,33 @@ def annotations(out): # what GitHub keeps of each `::level title=...::message` with open(os.devnull, 'w') as null, unittest.mock.patch('sys.stdout', null): self.assertEqual(q.tested_base(job), want) - def test_the_action_runs_the_script_on_its_path_input(self): + def action_run(self, path='repo', **env): + """The action's one step, run as the runner runs it (bash -e, in the workspace).""" + import yaml with open(os.path.join(HERE, 'action.yml')) as f: - text = f.read() - self.assertIn('CHECKOUT_PATH: ${{ inputs.path }}', text) - run = 'python3 -I "$GITHUB_ACTION_PATH/tested_base.py" --path "$CHECKOUT_PATH"' - self.assertIn(f'run: {run}\n', text) - r = subprocess.run(['bash', '-c', run], text=True, capture_output=True, - env={**GIT_ENV, 'GITHUB_ACTION_PATH': HERE, 'CHECKOUT_PATH': self.repo, - 'GITHUB_EVENT_NAME': 'pull_request', 'GITHUB_EVENT_PATH': self.event, 'GITHUB_SHA': self.M}) + steps = yaml.safe_load(f)['runs']['steps'] + self.assertEqual(len(steps), 1) # one step, so the action still always passes + self.assertEqual(steps[0]['env'], {'CHECKOUT_PATH': '${{ inputs.path }}'}) + return subprocess.run(['bash', '--noprofile', '--norc', '-e', '-c', steps[0]['run']], text=True, + capture_output=True, cwd=self.d, + env={**GIT_ENV, 'GITHUB_ACTION_PATH': HERE, 'CHECKOUT_PATH': path, + 'GITHUB_WORKSPACE': self.d, 'GITHUB_EVENT_NAME': 'pull_request', + 'GITHUB_EVENT_PATH': self.event, 'GITHUB_SHA': self.M, **env}) + + def test_the_action_runs_the_script_on_its_path_input(self): + r = self.action_run() self.assertEqual((r.returncode, r.stdout), (0, f'::notice title={T.TITLE}::{self.B1}\n'), r.stderr) + def test_the_action_writes_no_note_and_passes_when_git_clean_refuses(self): # fork-ci git-clean + marker = os.path.join(self.d, 'ran') + git(self.repo, 'config', 'core.fsmonitor', f'touch {marker}; false') + r = self.action_run() + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn('::error::git-clean: ', r.stdout) + self.assertIn('::warning::pr_queue tested base: no note', r.stdout) + self.assertNotIn('::notice', r.stdout) + self.assertFalse(os.path.exists(marker)) + if __name__ == '__main__': unittest.main() diff --git a/fork-ci/actions/test-select/action.yml b/fork-ci/actions/test-select/action.yml index 3a041c2e873c9..871efb5fbca50 100644 --- a/fork-ci/actions/test-select/action.yml +++ b/fork-ci/actions/test-select/action.yml @@ -29,6 +29,10 @@ outputs: runs: using: composite steps: + # Before any git call: a .git config that actions/checkout does not write (one an earlier job left) is refused. + - name: Refuse a git config actions/checkout does not write (fork-ci git-clean) + shell: bash + run: exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require # Only code from this action's folder (the caller's pinned SHA) runs; the checkout is parsed, never run. # The script also reads the runner's GITHUB_SHA and GITHUB_EVENT_PATH (HEAD must be GitHub's merge commit). - id: select diff --git a/fork-ci/actions/test-select/test-select.test.py b/fork-ci/actions/test-select/test-select.test.py index cbacaf1595cb4..b3363c4edc112 100644 --- a/fork-ci/actions/test-select/test-select.test.py +++ b/fork-ci/actions/test-select/test-select.test.py @@ -899,12 +899,14 @@ class Action(CleanEnv): def test_runs_only_its_own_script(self): runs = re.findall(r'^\s*run: (.*)$', self.text, re.M) - self.assertEqual(runs, ['python3 -I "$GITHUB_ACTION_PATH/test_select.py" --config "$CONFIG" --event "$EVENT"']) + self.assertEqual(runs, ['exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require', # the git-clean check of this pinned SHA + 'python3 -I "$GITHUB_ACTION_PATH/test_select.py" --config "$CONFIG" --event "$EVENT"']) self.assertNotIn('uses:', self.text) self.assertIn('using: composite', self.text) def test_inputs_reach_the_script_by_env_only(self): # never ${{ }} inside run: (script injection) - self.assertNotIn('${{', re.search(r'^\s*run: (.*)$', self.text, re.M)[1]) + for run in re.findall(r'^\s*run: (.*)$', self.text, re.M): + self.assertNotIn('${{', run) def test_declares_every_output_the_script_writes(self): for name in ('all', 'count', 'total', 'list', 'reason'): diff --git a/fork-ci/actions/test-select/test_select.py b/fork-ci/actions/test-select/test_select.py index a5da7296800ea..378502c0bec0d 100644 --- a/fork-ci/actions/test-select/test_select.py +++ b/fork-ci/actions/test-select/test_select.py @@ -120,8 +120,12 @@ def __hash__(self): CAPPED = Partial('\0cap') # a value the caps cut off +# Every git call runs as docs-scope's does (no global or system config, no hooks or fsmonitor of the repository). +GIT_ENV, GIT_SAFE = docs_scope.GIT_ENV, docs_scope.GIT_SAFE + + def git(*args, cwd=None): - r = subprocess.run(['git', *args], capture_output=True, cwd=cwd) + r = subprocess.run(['git', *GIT_SAFE, *args], capture_output=True, cwd=cwd, env={**os.environ, **GIT_ENV}) if r.returncode != 0: raise AllTests(f'git {args[0]} failed: {r.stderr.decode(errors="replace").strip()[:200]}') return r.stdout.decode('utf-8', 'surrogateescape') From 479c1438bae6e5050780cf32cdaaa06459f55a2f Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:17:30 +0000 Subject: [PATCH 2/2] fork-ci git-clean: format the README input table Co-Authored-By: Claude Opus 5.5 --- fork-ci/actions/git-clean/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fork-ci/actions/git-clean/README.md b/fork-ci/actions/git-clean/README.md index 33011bd95073e..4884489876602 100644 --- a/fork-ci/actions/git-clean/README.md +++ b/fork-ci/actions/git-clean/README.md @@ -34,8 +34,8 @@ Put it in every job that runs on a self-hosted runner and checks out code, befor other step that writes the workspace. With several checkouts, list their paths (`path: | ...`): all of them are checked. -| Input | Default | Meaning | -| ------ | ------- | ----------------------------------------------------------------------- | +| Input | Default | Meaning | +| ------ | ------- | -------------------------------------------------------------------- | | `path` | `.` | the checkout path or paths (actions/checkout's `path`), one per line | No outputs. A refusal fails the step, so the checkout and every later step do not run.