Stop Putting Tokens in URLs: A Gitea Actions Checkout Antipattern
A two-line fix in an automation script's clone command turned out to be two separate security fixes wearing one diff: HTTPS stops a token leaking on the wire, and a header instead of a URL keeps it out of shell history and process listings.
Table of contents
I found a token-in-the-URL git clone in one of my own automation scripts last week. It had been there since the script was first written, nobody noticed, and the fix was two lines. It's a good example of a mistake that's everywhere in copy-pasted "quick clone" one-liners and almost never gets a second look, so it's worth writing down why it's wrong and why the fix is two separate fixes, not one.
Where the pattern comes from
Every git host offers the same convenience: embed a username and token straight in the clone URL and you can clone a private repo with a single command, no credential helper, no interactive prompt, no separate auth step.
git clone http://ci-bot:[email protected]:3000/org/repo.gitIt's tempting because it's self-contained. Paste one line into a script, a README, or a CI job, and it just works. I'd written exactly that into a prompt template for an automation that clones a repo to work an issue — the kind of thing you write once while getting a bot working end-to-end, then never revisit because it keeps working.
Two different leaks, one line of code
The line above has two independent problems, and it's worth separating them because they have two independent fixes.
The first is plaintext HTTP. If the clone URL starts with http:// instead of https://, the entire request — including the credential sitting right there in the URL — goes over the wire unencrypted. Anything positioned to see the traffic (a shared network segment, a misconfigured proxy, a curious router) reads the token for free. This one is just "use TLS," and most hosts will happily serve the same repo over https:// with zero other changes.
The second problem survives switching to HTTPS entirely: the credential is embedded in the URL string itself, not just the wire protocol. A URL isn't a secret-shaped thing — it's a string that ends up in places that don't expect to be holding secrets:
- Shell history — anyone with read access to .bash_history or equivalent gets the token in plaintext, indefinitely.
- Process listings — while the clone is running, ps aux (or any process-monitoring tool) shows the full command line, token included, to any other user or process on the box that can see it.
- Logs — CI systems routinely echo the command they're about to run before running it. A workflow log, a debug trace, a proxy access log keyed on full request URLs — all of them now have a durable copy of a live credential.
TLS fixes the first problem and does nothing for the second. The token is still argv[2], still sitting in the URL, still getting written down by anything that logs the command. You need a second, independent fix.
The fix: a header instead of a URL
Git lets you attach an arbitrary HTTP header to every request it makes for a given clone, via -c http.extraHeader. That's the right place for a bearer credential — headers aren't part of the URL, aren't part of argv in the way a URL-embedded credential is, and aren't something tools instinctively log the way they log the command and its visible arguments.
git -c http.extraHeader="Authorization: token ghp_xxxxxxxxxxxxxxxxxxxx" clone https://git.example.com/org/repo.gitSame clone, two changes, two different leaks closed. HTTPS stops the token from being readable on the wire. The header keeps it out of the URL, which is what keeps it out of shell history, process listings, and anything logging the command verbatim. Neither change alone is sufficient — it's genuinely two fixes wearing one diff.
One caveat worth knowing: the header is still visible to anything that can read the process's environment or inspect it directly on the box doing the cloning (a root user, a debugger, a sufficiently nosy sibling process). It isn't a secrets-management solution — it just stops the credential from being casually copied into places that have no business holding it. If the thing doing the cloning is itself compromised, no amount of header-vs-URL juggling saves you; that's a different, bigger problem.
Where else to look
Once I'd found one, I went looking for the pattern elsewhere — in notes, scripts, half-finished setup docs, anything with a clone command in it. A quick grep for the shape of the problem is enough to flag most occurrences:
grep -rnE "https?://[^/\s]+:[^/\s@]+@" .That pattern matches scheme://user:secret@host, which is exactly the shape a token-in-URL clone takes. It'll also catch the equivalent mistake in anything else that builds a URL out of a credential — a curl command, a database connection string pasted into a script, a webhook URL with an API key tacked onto the query string. Same root cause every time: treating a credential as if it's just another piece of text that's convenient to concatenate, instead of something that needs a channel of its own.
The fix script is cheap. Finding every place the old pattern is quietly still working — because leaked-but-unexploited credentials don't announce themselves — is the part that actually takes the effort.