Sub topic 12
Credentials left behind by actions/checkout
This chapter assumes you can read a workflow file, know that GITHUB_TOKEN is created per job, and have seen the permissions key from earlier chapters. Here you will learn where actions/checkout stores the token, who can read it, and how to stop it from lingering.
What checkout does with the token
By default, actions/checkout fetches your repository using the job's GITHUB_TOKEN. To make later git commands work, it also saves that token in the local git configuration of the checked-out repository. It writes an authorization header into .git/config under the http.https://github.com/.extraheader key.
This is the setting called persist-credentials, and it defaults to true. The convenience is real. A later step can run git push or git fetch without any extra setup. The cost is that the token now sits on the runner's disk, in a file that every later step in the job can read.
You can see this for yourself in a throwaway workflow.
name: inspect-checkout
on: workflow_dispatch
permissions:
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Show git config keys
run: git config --local --list | sed 's/=.*/=<value hidden>/'The output includes a line starting with http.https://github.com/.extraheader. The sed command hides the value so the log does not echo a credential. Even without sed, GitHub masks the exact token string in logs, but you should not rely on masking as a security control.
Why this matters
Earlier chapters covered the threat model: any step in a job can misbehave, because a third-party action, a build script, or a dependency install runs with the job's privileges. A persisted token widens that exposure in two ways.
- Any later step can read it. A step does not need access to the
secretscontext or theGITHUB_TOKENenvironment variable. It can simply read.git/configin the workspace. - It can travel with the workspace. If a later step uploads the workspace or the
.gitdirectory as an artifact, the credential goes with it. Artifacts can often be downloaded by anyone who can read the repository, and for public repositories that means anyone.
The token expires when the job ends, or after at most 24 hours. So the leaked value is time-limited. Still, an artifact upload happens while the job is running, and an attacker who downloads the artifact quickly may be able to use the token before the job finishes. A long-running job gives more time. The size of the damage then depends on the token's scopes, which is why this chapter belongs next to the permissions chapters.
Turning persistence off
Set persist-credentials: false on the checkout step. Checkout still uses the token for the initial fetch, but it removes the credential from the git config when it finishes.
name: build
on: [push, pull_request]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- run: make build
- uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/Two details are worth noting in this example. First, the artifact uploads only dist/, not the whole workspace. Narrow upload paths are a second layer of defense. Second, permissions is already contents: read, so even if something did leak, the token could read the repository but not change it.
Recent versions of actions/checkout store the credential in a separate file under the runner's temporary directory instead of directly in .git/config, and reference it from the git config. That change makes accidental inclusion in an uploaded workspace less likely. Do not treat it as a reason to skip persist-credentials: false, because later steps in the same job can still reach the credential through git.
When you actually need to push
Some jobs must write to the repository, for example a job that commits a generated changelog. In that case you want persistence, but only in the job that needs it, and with the narrowest scope.
name: update-docs
on:
push:
branches: [main]
permissions: {}
jobs:
docs:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- run: ./scripts/generate-docs.sh
- name: Commit and push
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add docs/
git diff --cached --quiet || git commit -m "Update generated docs"
git pushThis job keeps the default persistence because git push depends on it. It limits the risk in other ways. The workflow-level block is empty, contents: write is declared only on this job, and the job does nothing else, such as installing dependencies from untrusted sources.
An alternative is to turn persistence off and supply the token yourself only to the push step.
- uses: actions/checkout@v4
with:
persist-credentials: false
- run: ./scripts/generate-docs.sh
- name: Commit and push
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add docs/
git diff --cached --quiet || git commit -m "Update generated docs"
git push "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" HEADHere the token exists in the environment of one step only, so the doc generation script cannot read it from .git/config. This costs a little more setup. Use it when the earlier steps run code you trust less.
| Approach | Token on disk during earlier steps | Setup effort | Good fit |
|---|---|---|---|
| Default persistence | Yes | None | Small jobs where every step is trusted and a push is needed |
| `persist-credentials: false` | No | One line | Build, test, and lint jobs |
| `persist-credentials: false` plus token in a single step's `env` | No | A few lines | Jobs that run untrusted code before a push |
Edge cases
- Submodules and private dependencies. With
persist-credentials: false, latergit submodule updateorgit fetchcommands against private repositories will fail with authentication errors. Checkout can fetch submodules itself when you setsubmodules: true. Note thatGITHUB_TOKENonly covers the current repository, so private submodules in other repositories need a different credential, which a later chapter covers. - Later git commands. Any step that runs
git push,git fetch, orgit pullafter checkout will fail with "could not read Username" or "Authentication failed" when persistence is off. This is the usual symptom, and the fix is to pass the token to that step explicitly, as shown above. - Containers and Docker builds. If you run
docker buildwith the workspace as context, the.gitdirectory can be copied into the image when it is not excluded by.dockerignore. With persistence on, the credential could end up in an image layer. Turn persistence off, or add.gitto.dockerignore. - Self-hosted runners. On a runner that is reused between jobs, files left in the workspace can outlive the job. Persisted credentials are normally cleaned up at the end of the job by checkout's post step, but a cancelled or crashed job may skip cleanup. Ephemeral runners avoid this class of problem.
- Multiple checkouts. Each
actions/checkoutcall has its ownpersist-credentialssetting. If a job checks out a second repository into a subdirectory, set it on that step too.
Pairing with other controls
persist-credentials: false does not replace least-privilege permissions. It is one layer in a stack.
- Set the repository default to read-only.
- Declare
permissionsat the workflow level, and elevate per job. - Disable credential persistence wherever a job does not push.
- Upload only the paths you need as artifacts.
A job with contents: read and persist-credentials: false has little for an attacker to take. A job with contents: write and persisted credentials, running untrusted code, has a lot.
Exercise
A build job uses actions/checkout@v4 with default settings, runs npm ci, then runs actions/upload-artifact with path: . to keep the build output. The job has contents: read. Name the problem, and rewrite the checkout and upload steps to remove it.
Show answer
The default persist-credentials: true saves the token in the git config, and uploading . includes the .git directory, so anyone who can download the artifact can read the token. Even with contents: read, that exposes read access to the repository, and for a private repository this is a leak. Fix both causes: use actions/checkout@v4 with persist-credentials: false, and change the upload to the specific output directory, for example path: dist/.
Exercise
After you set persist-credentials: false on a release job, the final step git push origin HEAD fails with an authentication error. The job declares contents: write. Why does it fail, and what is a fix that keeps the token out of the earlier steps?
Show answer
Checkout removed the stored credential, so git has nothing to authenticate with, regardless of the job's contents: write permission. Pass the token to the push step only, for example by setting env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} on that step and pushing to https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git. Earlier steps never see the token in their environment or in .git/config.