Sub topic 12
Credentials left behind by actions/checkout
This chapter assumes you know how the permissions key limits the GITHUB_TOKEN, and that you can read a basic workflow. It covers a second place the token ends up: the checked-out repository on the runner's disk.
What checkout stores
By default, actions/checkout sets persist-credentials: true. The action takes the job's GITHUB_TOKEN and writes it into the repository's local git configuration. This lets later steps run git fetch or git push without extra setup.
The token is then stored in .git/config as an HTTP header setting. You can confirm that the setting exists without printing the secret:
name: inspect
on: push
permissions:
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: git config --local --name-only --get-regexp 'extraheader'The output lists a key ending in extraheader. Its value is the encoded token.
Why it matters
Every later step in the job can read that file. This includes third-party actions, build scripts, and test code from a pull request. Even a read-only token (contents: read) lets an attacker read private repository contents. If the job has contents: write, the same file lets them push commits.
The token expires when the job ends, so a stolen copy is only useful during that window. Files can outlive the job, though. If a later step uploads the whole workspace as an artifact, the .git directory goes with it. The artifact then contains a credential that was valid while the job ran. Treat the file as a leak path, not just a convenience.
flowchart TD
A[actions/checkout runs] --> B[Token written to .git/config]
B --> C[Build and test steps]
B --> D[Third-party actions]
B --> E[Artifact upload of workspace]
C --> F[Any of them can read the token]
D --> F
E --> FTurning it off
Most jobs only read code, so disable persistence:
steps:
- uses: actions/checkout@v4
with:
persist-credentials: falseCheckout still uses the token to download the code. It just removes the token from the git config afterward. Your build, tests, and linters work as before.
When a job must push
Some jobs need to commit or tag, such as a release job. Keep the default there, but isolate that job so the extra access does not spread:
name: release
on:
push:
tags: ['v*']
permissions: {}
jobs:
tag-notes:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -f latest
git push --force origin latestOnly this job has contents: write, and it runs no untrusted code. Anything that builds or tests should live in a separate job with persist-credentials: false and read-only permissions.
Checklist
- Set
persist-credentials: falseon every checkout that does not push. - Do not upload the workspace root as an artifact. Upload specific build output paths instead.
- Keep jobs that push separate from jobs that run third-party or pull request code.
Exercise
A job runs actions/checkout@v4, runs npm test, and then uploads the repository folder as an artifact. The job only needs to read code. Which two changes reduce the risk of a leaked token?
Show answer
Set persist-credentials: false on the checkout step so the token is not written to .git/config. Change the artifact upload to include only the needed output paths, such as the test report, instead of the whole repository folder, which contains .git.