Sub topic 2
How secrets leak: code, logs, artifacts, caches, and debug output
This chapter assumes you know basic Git, can read a GitHub Actions workflow, and understand environment variables in a shell. It covers where secrets escape from a pipeline. Later chapters cover the fixes in detail.
The leak paths
A secret leaks when it ends up somewhere readable by someone who should not see it. In CI/CD, five places account for most incidents.
| Path | How it happens | Who can read it |
|---|---|---|
| Source code | Token committed in a file, config, or `.env` | Anyone with repo access, forks, clones, history |
| Logs | Command echoes or prints a secret | Anyone who can view workflow runs |
| Artifacts | Build output contains a credential file | Anyone who can download the artifact |
| Caches | Cached directory holds a token or config | Other jobs and branches that restore the cache |
| Debug output | Verbose or trace mode prints environment values | Anyone who can view logs |
Code and history
Committing a secret is the most common leak. Deleting the file in a later commit does not help. Git keeps the old commit, and anyone who cloned or forked the repository already has a copy. Treat any committed secret as exposed, even if you remove it within minutes.
Logs
Logs are read by more people than the secret's intended audience. Leaks happen through:
echo "$TOKEN"left in a script while debugging.- Error messages that include a connection string or a full URL with credentials.
- Tools that print their own configuration on failure.
Platforms mask known secret values in logs, but masking only matches exact strings. A base64-encoded or split value slips through. Chapter 5 covers this limit.
Artifacts
Build artifacts are uploaded files that other people and jobs can download. A common mistake is uploading a whole directory that contains a credential file, such as .npmrc, .docker/config.json, or .env.
steps:
- uses: actions/checkout@v4
- run: npm ci
- uses: actions/upload-artifact@v4
with:
name: build
path: .This uploads everything in the workspace, including any file the job wrote there earlier. Upload only the build output, for example path: dist/. Also check that actions/checkout is not leaving credentials in .git/config. Setting persist-credentials: false prevents that.
Caches
A cache stores files so later runs can reuse them. If you cache a home directory, a package manager config, or a Docker config, you may store an auth token with it. Other jobs that can restore that cache then receive the token. Cache only dependency directories, such as ~/.npm, and never the files that hold credentials.
Debug output
Debug modes print more than normal runs. Examples are set -x in shell, which prints every command with its expanded variables, curl -v, which prints request headers including Authorization, and the platform's step debug logging.
set -x
curl -v -H "Authorization: Bearer $API_TOKEN" https://api.example.com/healthHere both lines can print the token. Masking may hide the exact value, but verbose output often reformats or encodes data, so do not rely on it.
A quick audit
Review each workflow for these questions:
- Does any file in the repository contain a credential, including old commits?
- Does any step print environment variables, headers, or configuration?
- Does each
upload-artifactpath name only the files you intend to share? - Does each cache path avoid token and config files?
- Is verbose or trace mode disabled where secrets are present?
Exercise
A job runs npm ci with a token written to .npmrc in the workspace, then uploads path: . as an artifact. Which leak path is this, and what two changes fix it?
Show answer
It is an artifact leak, because the credential file is inside the uploaded directory. Upload only the build output (for example dist/), and avoid leaving the token in the workspace by deleting .npmrc after install or by passing the token through an environment variable scoped to that step.
Exercise
Why does removing a committed secret in a new commit not make the repository safe?
Show answer
The secret remains in Git history and in every clone and fork made before the removal, so it must be treated as exposed and revoked.