#Why this matters
A CI/CD pipeline bridges source code and live production infrastructure. Build runners typically hold deployment roles, container registry credentials, and signing keys that let them modify cloud environments you worked hard to lock down with IAM and Terraform.
If an attacker triggers a workflow on a pull request or recovers an API token from an old Git commit, they can bypass network perimeters and act with the pipeline's privileges. Hardening workflow definitions, removing static secrets through OIDC and Vault, and scanning repositories continuously closes this critical gap.
#CI/CD attack vectors and workflow hardening
Three misconfigurations dominate CI/CD security reviews, especially in GitHub Actions:
- Script (expression) injection: Writing
${{ github.event.pull_request.title }}or${{ github.event.issue.title }}directly inside arun:shell block causes the runner to expand attacker-controlled text before the shell parses the script. A crafted title such asfix"; echo PWNED #breaks out of quotes and executes commands on the runner. - Poisoned pipeline execution (
pull_request_target): Unlikepull_request, thepull_request_targettrigger runs in the context of the base repository with access to repository secrets and a write-capableGITHUB_TOKEN. Checking out and executing untrusted fork code inside apull_request_targetjob hands those secrets to the fork author. - Excessive permissions and mutable tags: Leaving default token permissions overly broad lets any step write to code or packages, while
uses: vendor/action@v3trusts a mutable tag that an upstream compromise can silently repoint.
Compare this vulnerable workflow with a hardened version:
# Vulnerable: inline expression expansion, broad permissions, mutable tag
on: pull_request_target
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: echo "Testing PR: ${{ github.event.pull_request.title }}"To harden it, switch to pull_request when secrets are not needed, declare minimal permissions, pass untrusted metadata through env: instead of inline ${{ ... }} interpolation, and pin actions to an immutable 40-character commit SHA:
# Hardened: read-only token, env binding, SHA-pinned action
on: pull_request
permissions:
contents: read
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: printf 'Testing PR: %s\n' "$PR_TITLE"Because PR_TITLE is passed via the process environment and double-quoted in printf, shell metacharacters are treated as plain text rather than commands.
#Eliminating static keys with OIDC federation
Storing long-lived cloud access keys (AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY) inside CI secret settings creates operational risk: static keys do not expire automatically, are difficult to scope per branch, and stay valid if exfiltrated from a build log.
OpenID Connect (OIDC) federation replaces static keys with short-lived, verifiable identity tokens:
In your cloud IAM trust policy or Vault role, restrict claims strictly so only a specific repository and protected branch (such as repo:org/app:ref:refs/heads/main or a protected production environment) can assume the deployment role.
| Credential approach | Secret lifetime | Compromise blast radius | Rotation burden |
|---|---|---|---|
| Static key in CI secrets | Months or indefinite | Valid until manually revoked | Manual across all repos |
| OIDC federated role | Minutes (session TTL) | Short window, scoped to claims | Automatic per job |
| Vault dynamic secret | Minutes to hours (lease) | Auto-revoked on lease expiry | Automatic per lease |
#Centralized dynamic secrets with HashiCorp Vault
While OIDC handles cloud IAM authentication well, builds often still need database credentials, TLS certificates, or internal service tokens. HashiCorp Vault centralizes these secrets and generates them on demand instead of storing permanent shared passwords.
A hardened runner authenticates to Vault's JWT/OIDC auth method using the pipeline's OIDC token, receives a short-lived Vault token tied to a narrow policy, and reads dynamic credentials from a secrets engine (such as the database or AWS engine). Vault attaches a lease time-to-live (TTL) to every credential, writes every request to an audit log, and revokes the temporary user automatically when the job ends. Keep Vault policies tightly scoped by environment so a staging job can never read production paths.
#Detecting leaked secrets with Gitleaks
Hardening future runs does not clean up mistakes already sitting in Git history. Deleting a credential in a new commit with git rm leaves the original value intact in earlier commits. Gitleaks scans working trees, commit history, and pull requests using regex rules and entropy checks to spot exposed tokens before or after push.
$ gitleaks detect --source ./lab-repo --verbose
Finding: EXAMPLE_TOKEN=ghp_0123456789abcdef0123456789abcdef0123
Secret: ghp_0123456789abcdef0123456789abcdef0123
RuleID: github-pat
Commit: 9a4c21e8f0b12c34d567890abcdef12345678901
File: scripts/deploy.sh
Line: 4
WRN leaks found: 1When Gitleaks reports a valid secret in history, follow the right order of response:
- Revoke and rotate first at the issuing provider (assume the secret is already compromised).
- Audit provider logs for unauthorised usage during the exposure window.
- Rewrite Git history (for example with
git filter-repo) if required, and add a.gitleaks.tomlCI check plus a local pre-commit hook so the pattern cannot return.
#Defending and common mistakes
- Rotating in Git without revoking at the provider: Removing a line from a file or even rewriting history leaves an unrevoked key usable by anyone who cloned or scraped the repo earlier.
- Over-broad OIDC trust policies: Writing a wildcard subject claim like
repo:org/*lets any repository in the organisation--including test sandboxes--assume production roles. - Printing secrets during debugging: Running
envorset -xin a CI shell can leak Vault or OIDC-derived tokens into build logs even when secret masking is enabled. - Suppressing Gitleaks findings blindly: Adding a commit to
.gitleaksignorewithout confirming the string is a harmless test fixture leaves real credentials exposed.
#Summary
Securing CI/CD pipelines requires locking down both workflow execution and credential lifecycles. Avoid expression injection by passing untrusted event data through environment variables, restrict GITHUB_TOKEN permissions, pin third-party actions by commit SHA, and avoid unsafe pull_request_target checkouts. Replacing static secrets with OIDC federation and HashiCorp Vault dynamic leases, paired with continuous Gitleaks scanning, keeps pipelines from becoming an open backdoor into cloud infrastructure.