Files
codeberg-to-gitea-migrator/docs/devlog/20260811-issue1-gitrunner-sole-git-op-seam_log.md
AlexKucera 1613ec72b8
CI / test (push) Canceled after 0s
refactor(migrator): centralize git in GitRunner
Introduce git.GitRunner as the sole git-op seam: the only place git is
invoked. It owns subprocess execution, per-op timeout, error redaction, and
auth delivery. Forge tokens move off the remote URL entirely onto a
non-persisted per-op http.<host>.extraHeader, so they never reach the
command line, a remote URL, or .git/config.

Rewrite the mirror procedure to drive GitRunner and retire the URL-token
helpers (_authed/_bare/_redact/_scrub_remote_tokens) plus the try/finally
scrub -- their reason to exist vanishes. mirror_repo's signature and report
stay unchanged; cli/migrate import the re-exported GitError/timeout
unchanged. The procedure's ordering invariants are now locked by a
recording-fake test, and an AST guard fails the suite if any git op bypasses
the seam.

Closes the 2026-08-11 audit's HIGH (token in GitError) and MEDIUM (token
in .git/config) at one structural root cause.

Closes #1
2026-08-11 15:36:35 +02:00

6.8 KiB

GitRunner: sole git-op seam (closes audit HIGH + MEDIUM token findings)

Date: 2026-08-11 Type: issue Reference: Gitea issue #1 — tea issues view 1; audit audit/2026-08-11/security-report.md (A09 HIGH, A02 MEDIUM)

Goal

Introduce a single deep module — git.GitRunner — as the only way git is invoked, rewrite the mirror procedure to drive it, retire the URL-token machinery (its reason to exist vanishes), and lock the procedure's ordering invariants behind a recording-fake test. This closes the 2026-08-11 security audit's HIGH (live token in an uncaught GitError) and MEDIUM (token persisted in the mirror clone's .git/config) at one structural root cause.

What Was Done

  • New src/codeberg_to_gitea_migrator/git.py — the seam. GitRunner (sole git-op site), Forge (frozen url+token value type), GitError, DEFAULT_GIT_TIMEOUT, redact() (known-secret first, legacy oauth2: regex fallback), _auth_args() (non-persisted per-op git -c http.<scheme>://<host>.extraHeader=Authorization: token <T>), _real_exec (subprocess adapter that composes auth inside, so callers and test fakes never see a token), and the run / capture / ok verbs.
  • Rewrote mirror.py — mirror_repo drives a GitRunner; deleted _authed, _bare, _redact, _scrub_remote_tokens, _run, _run_capture, _ensure_remote, and the try/finally scrub block. mirror_repo's signature and report dict are unchanged; DEFAULT_GIT_TIMEOUT / GitError are re-exported so cli.py / migrate.py stay byte-identical.
  • Restored idempotent re-run for the LFS target remote (add-or-set-url via the seam's ok probe) — a regression the spec reviewer caught.
  • Tests — new test_git.py (15 seam tests + an AST sole-caller guard); rewrote test_mirror.py (ordering invariants, per-op auth wiring, LFS skip, idempotent-rerun regression). Deleted the old URL-token redaction tests.
  • Docs — src/.../AGENTS.md module map gains git.py and a rewritten mirror entry; tests/AGENTS.md gains test_git.py / test_mirror.py; CHANGELOG.md security + refactor entries. CONTEXT.md was already aligned to the target state — left untouched.

Decisions & Rationale

Decision Rationale
Auth via non-persisted git -c http.<host>.extraHeader (not URL token, not credential helper) Token leaves the remote URL, the persisted command line, and .git/config — closes both audit findings at the root. Credential-helper (process-list hardening) is a later swap behind the same seam.
Forge = frozen value type binding url + token Makes "push the target URL with the source token" unexpressable — same-forge is an invariant, not two free parameters (story #9).
Three verbs run / capture / ok (not flags) Removes the footgun where forgetting a "don't raise" flag turns "LFS not installed → skip" into a crash; ok never raises.
Re-export DEFAULT_GIT_TIMEOUT / GitError from mirror.py Keeps cli.py / migrate.py byte-identical (story #11) and mirror_repo's contract preserved (story #5).
redact() uses the known secret first, regex only as fallback Story #15 — a future auth pattern still gets masked, not just the oauth2: shape.
One recording fake of the exec callable, no formal Protocol/port Only one real adapter exists; the Protocol is deferred to the "second real backend" trigger (e.g. dry-run).
Refactor confined to the mirror layer cli / migrate / clients / refs / issues / releases untouched — confirmed by gitnexus detect_changes.

Gotchas & Fixes

Problem Root Cause Fix
test_pushes_authenticate_as_target and test_refetch_path… failed with impl-correct output pytest's tmp_path embeds the test name — …/test_pu**sh**es…/ and …/test_re**fetch**…/ — and my command matcher used substring-of-joined, so the path token masqueraded as the push/fetch subcommand Switched command identification to exact argv-token membership; substring-within-a-token only for the _pr/<n> refspec
ruff F401 flagged GitError unused in mirror.py Imported solely so cli.py can from .mirror import GitError Added __all__ = [...] so ruff treats it as an intentional re-export
Spec review: remote add target (add-only) broke idempotent re-runs Replacing _ensure_remote's add-or-set-url with a bare add meant the persisted target remote makes git remote add fail on the 2nd run Restored add-or-set-url through the seam's ok probe + set-url fallback; added test_lfs_target_remote_is_idempotent_on_rerun
Tautology test (assert fake is not None) Didn't actually verify the per-runner timeout reaches exec _Fake now records timeouts; test asserts fake.timeouts == [42]

Files Changed

File Change Summary
src/codeberg_to_gitea_migrator/git.py NEW — the git-op seam: GitRunner, Forge, redact, _auth_args, _real_exec, run/capture/ok
src/codeberg_to_gitea_migrator/mirror.py Rewritten to drive GitRunner; retired URL-token helpers; idempotent target-remote restore; re-exports
tests/test_git.py NEW — seam tests (auth delivery, redaction, error/timeout contract, ok never raises) + AST sole-caller guard
tests/test_mirror.py Rewritten — ordering invariants + per-op auth wiring + LFS skip + idempotent-rerun, via a recording exec fake
src/codeberg_to_gitea_migrator/AGENTS.md DOX: module map gains git.py; mirror entry rewritten; Work Guidance + Verification updated
tests/AGENTS.md DOX: test index gains test_git.py / test_mirror.py
CHANGELOG.md security + refactor entries under [Unreleased]
AGENTS.md Session Logs section added (this log)

Verification

  • pytest → 44 passed. ruff check . + ruff format --check . → clean.
  • gitnexus detect_changes → only mirror_repo touched in production (signature preserved); cli/migrate/clients/refs/issues/releases untouched.
  • Two-axis /code-review → Standards: 0 hard violations; Spec: 1 medium (idempotent re-run) found and fixed.

Open Items & Next Steps

  • Runtime verify git-lfs honours http.<host>.extraHeader against the real forges; if not, swap to an LFS-scoped credential helper behind the seam (open item carried from the issue; not hermetically testable).
  • Residual audit LOW (token briefly visible in the process list via the --source-token / --target-token flags) — later swap behind the seam.
  • Issues #2 (Migration context handle) and #3 (_pr/<n> contract in refs.py) remain open — explicitly out of scope here.
  • Close Gitea issue #1 (tea issues close 1) after merge — left to the operator.

Log written by write-log skill