Cloning every repository without a token in the URL
GitHub Auditor clones every repository in an account, and the access token never touches a URL, a repository’s git config or the environment.
Private repositories need a token, and the quick way to hand git one is to put it in the URL, as in https://<token>@github.com/owner/repo.git. Git then saves that URL in .git/config in plain text, so anything that prints the remote (git remote -v for one) prints the token too.
I kept the token out of all of that in GitHub Auditor, whose reports start from a local clone of every repository in an account. auditer checkout makes those clones and keeps them up to date, for as many accounts as the config lists, and the token never goes in the URL, the repository config or the environment. What the reports do with the clones is in Counting developer activity from git.
Asking git for the password
If the GIT_ASKPASS environment variable names a program, git runs it whenever it needs a credential and takes whatever it prints as the answer. _run_git() in src/auditer/git_ops.py writes a two-line shell script that prints the token, points GIT_ASKPASS at it for a single git command, and deletes it when the command ends:
def _make_askpass_script(token: str) -> str:
path = tempfile.mktemp(suffix=".sh", prefix="git_askpass_")
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o700)
try:
os.write(fd, f"#!/bin/sh\necho {token}\n".encode())
finally:
os.close(fd)
return path
The file is created with mode 0o700. From the moment it exists, only its owner can read or run it. Creating it with the usual permissions and tightening them afterwards would leave a gap. tempfile.mktemp() only picks a name, which on its own would be open to a race. O_EXCL closes that: the open fails if anything already exists at that path, and a file planted there in the meantime is never used.
The environment carries the script’s path, never the token. GIT_TERMINAL_PROMPT=0 is set alongside it: git never falls back to asking on the terminal, and a run from cron can’t hang waiting for someone to type. The script is removed in a finally block, which runs even when the command fails or times out.
Its tests check that the file’s mode is exactly 0o700, and that a failed removal logs a warning and lets the run carry on.
The URL still has a user name
The clone URL gets a user name, but no password: https://[email protected]/owner/repo.git. With the user name already in the URL, git only asks for the password, and the script answers with the token.
Bitbucket Cloud works the same way with a different user name. For git over HTTPS with an API token, it wants the fixed name x-bitbucket-api-token-auth, not the account’s email address. Its REST API takes the same token with the email address as the user name. The tool uses one convention for the API and the other for git.
Adding the user name has two traps, and _build_auth_url() handles both. Bitbucket’s clone URLs already contain a user name, which has to be replaced. And a user name with an @ in it would end the user part of the URL early, which is why it’s percent-encoded:
parts = urlsplit(clone_url)
netloc = f"{quote(auth_username, safe='')}@{parts.hostname}"
if parts.port:
netloc += f":{parts.port}"
return urlunsplit((parts.scheme, netloc, parts.path, parts.query, parts.fragment))
After a clone, the remote URL is set back to the plain clone URL. For an update, the URL with the user name is set just before the fetch and put back in a finally block. .git/config holds the plain URL except while a clone or fetch is running, and even then it holds a user name and no token.
Keeping it out of the output
When a clone or fetch fails, git’s error output is shown. Before it is, sanitise_git_output() in src/auditer/utils/security.py masks anything that matches one of GitHub’s token formats (ghp_, github_pat_, gho_, ghs_, ghu_ and ghr_), and any user name and password in a github.com URL. The same function runs over every log line, through a logging formatter in src/auditer/utils/logging.py:
class SanitisedFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
original = super().format(record)
return sanitise_git_output(original)
auditer config --show prints each token as four asterisks and its last four characters. The token itself lives in config.yaml, which git ignores.
A clone is a mirror
An update runs git fetch --all --prune, then git reset --hard to the remote’s default branch. These clones are read-only copies of what’s on GitHub, and the reset makes sure each one matches. But a reset throws away uncommitted work. Before updating, checkout runs git status --porcelain, skips any clone with changes in it, and lists the skipped ones at the end. --no-skip-dirty turns that off. A repository with no commits has no default branch on the remote yet, so the reset is skipped for it.
With --tags, the fetch adds --tags --force, and a tag moved on the remote replaces the local one, where git would otherwise refuse the whole fetch. With --branches, every local branch is moved to match its remote branch. --full turns on both.
The activity report fetches the clones too, so that it covers work pushed since the last checkout. That uses a separate function, fetch_repo(), which never touches the working tree or local branches, because someone might be working in that clone. It has a five-minute timeout for each repository.
Reporting what changed
For every branch a fetch updates, git prints a line with the old and new commits. Two dots between them mean the branch moved forward, three a forced update, and _parse_fetch_output() checks which it got:
match = _HASH_RANGE_RE.search(stripped)
if match:
old_hash = match.group(1)
new_hash = match.group(2)
forced = "..." in match.group(0)
For each updated branch, git log old..new lists the commits that were added, with their authors. For a force push, git rev-list --count new..old counts the commits that disappeared. The summary shows the branch as force-pushed, with the commits added and removed and a link to GitHub’s compare view. New branches and tags are listed with links too.
Repositories deleted from GitHub show up as well. After the run, checkout compares the local folders with the full list from the API, taken before any filters for visibility, forks, exclusions or projects. A repository that’s only filtered out isn’t reported as deleted. Any folder left over is listed as an orphan and left on disk.
Repository names come from the API and become folder names. Before a clone, each name is checked against a pattern of safe characters and refused if it contains ...
A nightly cron job
auditer checkout --account my-org --dry-run
auditer checkout --all --full
The first shows what would be cloned or updated without doing it. For the nightly run, scripts/daily-checkout.sh in the project runs the second from the repository root and appends to a log file:
0 4 * * * /path/to/github-auditor/scripts/daily-checkout.sh
More on GitHub Auditor
Featured projects
-
Apple News API
Publish articles and live coverage to Apple News from PHP, with the whole Apple News Format built from objects.
-
PrintMD
Markdown in, and a designed PDF, Word or PowerPoint file out. Its PDF service runs on your own computer.
-
Where Should I Live
Which city would leave you best off? Cities ranked by take-home pay, living costs, visas and the way you live.
-
Solar Calendar
Thirteen months of exactly four weeks, with the year starting on the first Monday on or after the spring equinox.
-
TurboPush
Resumable uploads of very large files from the browser, put back together by one PHP class on the server.
More writing
-
Many sessions at once, and what still needs me
Fifteen coding-agent sessions across two Linux boxes, one session that watches the rest, what it may answer for me, what it never may, and why a yes it passes on gets checked against what I typed.
-
61 public repos of agent tooling, none installed, six ideas kept
One author, 61 public repositories of coding-agent tooling: what I checked before any of it got near my Linux boxes, why none of it was installed, and the six ideas I built my own way.
-
Rules a coding agent cannot talk its way round
The hook that reads every command a coding agent wants to run on my Linux boxes: what it refuses, the 27 proofs that its tests fail when a rule is taken out, and the four lines every report opens with.
-
Dozens of repos, one person
46 repositories and two Linux boxes with nobody else on them: the diary, the decision table, the rebuild guide and the job that keeps both sets of clones in step.