On 15 April, Codecov, a code coverage service used by tens of thousands of organisations, disclosed that its Bash Uploader script had been modified by an attacker. The altered script, which customers ran inside their CI pipelines, sent the contents of the CI environment, including environment variables holding credentials, tokens and keys, to a server controlled by the attacker. Codecov says the modification began on 31 January and was discovered in April.
Many affected companies have since been rotating every secret their pipelines could see. The incident is a stark reminder that CI/CD environments are treasure chests of credentials.
Why CI is full of secrets
Pipelines need to do privileged things: pull private dependencies, push images, run database migrations, deploy to cloud accounts, publish packages and notify chat channels. Each of those typically needs a credential, and those credentials are often:
- long-lived: created once, never rotated,
- over-privileged: "admin" because it was easier,
- widely available: exposed to every job and branch, and
- copied: the same key pasted into several projects.
How CI secrets leak
- Third-party scripts and actions that run in your pipeline, as with Codecov.
- Build logs that print environment variables or command output containing tokens.
- Artefacts and images that accidentally include
.envfiles or credentials. - Untrusted code: pull requests from forks or contributors running in jobs that can see secrets.
- Source code: secrets committed "temporarily" and never removed from history.
- Compromised CI infrastructure or plugins.
Step 1: reduce
The safest secret is one that does not exist, and the next safest is one that expires quickly.
- Remove secrets that are no longer used.
- Narrow permissions to exactly what each job needs: a deploy key for one service, not an account admin key.
- Shorten lifetimes. Where your platform supports it, issue short-lived credentials at job time instead of storing long-lived ones. HashiCorp Vault's dynamic secrets can generate cloud or database credentials that expire after the job:
# In a CI job authenticated to Vault: get AWS credentials valid for 15 minutes
export VAULT_ADDR=https://vault.internal.example
vault login -method=approle role_id="$VAULT_ROLE_ID" secret_id="$VAULT_SECRET_ID" > /dev/null
creds=$(vault read -format=json aws/creds/deploy-payments ttl=15m)
export AWS_ACCESS_KEY_ID=$(echo "$creds" | jq -r .data.access_key)
export AWS_SECRET_ACCESS_KEY=$(echo "$creds" | jq -r .data.secret_key)Step 2: protect
- Use the CI platform's secret store or a vault, never plain-text variables in pipeline files.
- Mask secrets in logs, and avoid commands that echo environment variables.
- Scope secrets to the jobs, environments and branches that need them. Production credentials should only be available to pipelines on protected branches, after review.
- Never expose secrets to untrusted code, such as forked pull requests.
- Pin third-party scripts and actions to specific versions or commit hashes, and verify checksums of downloaded scripts, rather than piping the latest version from the internet into a shell:
# Risky: runs whatever the URL serves today, with all your CI secrets in the environment
curl -s https://example.com/uploader.sh | bash
# Better: pinned version, verified checksum, minimal environment
curl -sSLo uploader.sh https://example.com/uploader-v1.2.3.sh
echo "3b1e...a9f uploader.sh" | sha256sum -c -
env -i PATH="$PATH" UPLOAD_TOKEN="$UPLOAD_TOKEN" bash uploader.shStep 3: detect
- Scan repositories for committed secrets, including history, with tools such as gitleaks or truffleHog, and add a pre-commit hook so new ones are caught locally.
- Scan build logs and artefacts for credential patterns.
- Monitor use of credentials: a CI deploy key used from an unfamiliar IP address is a strong signal.
# .pre-commit-config.yaml
repos:
- repo: https://github.com/zricethezav/gitleaks
rev: v7.4.0
hooks:
- id: gitleaksStep 4: rotate
- Rotate secrets on a schedule, ideally automatically.
- Maintain a runbook for emergency rotation: which secrets exist, where they are used and how to replace each one. The organisations that recovered fastest from Codecov were the ones that already had this list.
- After any suspected exposure, rotate first and investigate second.
Questions to answer this month
- Can we list every secret our pipelines use, and what each can do?
- Which third-party scripts, plugins and actions run in jobs that can see secrets?
- Could a pull request from a fork access any secret?
- How long would it take us to rotate every CI secret?
The bottom line
Codecov did not break into its customers' clouds directly. It collected keys that were sitting in CI environments, waiting to be read. Reduce the number and power of pipeline secrets, protect and scope the ones that remain, detect leaks quickly and be ready to rotate everything fast.
Our application security testing includes CI/CD secrets reviews, and our managed detection and response team can investigate whether exposed credentials have been used.