Sub topic 8
Pinning third-party actions and limiting workflow permissions
This chapter assumes you can read a GitHub Actions workflow file and know how repository secrets reach a job. Earlier chapters covered how secrets are stored and scoped. Here the concern is the code that runs beside your secrets.
Why third-party actions are a risk
A step that says uses: some-org/some-action@v1 downloads and runs someone else's code inside your job. That code can read any secret you pass to the step. It can also read the job's GITHUB_TOKEN and any files on the runner.
Tags such as v1 are mutable. The owner of the action, or an attacker who takes over the owner's account, can move the tag to a new commit. Your next run then executes the new code without any change in your repository.
Pin to a full commit SHA
A commit SHA is a 40-character hash that identifies one exact commit. It cannot be moved, so pinning to it fixes the code you run.
steps:
# actions/checkout v4.1.1
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11Follow these rules:
- Use the full 40-character SHA. Short SHAs are not accepted for actions.
- Add a comment with the version, so people know what the hash means.
- Take the SHA from the action's official repository, not from a fork.
- Use Dependabot or Renovate to propose SHA updates as pull requests that you review.
Pinning does not cover everything. An action may itself call other actions or download tools at run time. For high-risk actions, read the source, or copy the action into your own organization and pin your copy.
Limit what the GITHUB_TOKEN can do
Every job receives a GITHUB_TOKEN. Its default permissions depend on repository and organization settings, and they can be broad. A compromised step can use the token to push code, edit releases, or open pull requests.
Set the permissions explicitly. Start at the workflow level with the minimum, then widen only for jobs that need more.
name: build
on:
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- run: make test
comment:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- run: echo "Only this job can write to pull requests"When a permissions block lists any scope, every scope you leave out is set to none. The test job above can read the repository and nothing else.
You can also set the repository default to read-only under Settings, then Actions, then General. That way a new workflow without a permissions block starts safe.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Pinning to `@main` or `@v1` | Code changes without review | Use the full commit SHA |
| No `permissions` block | Token may be read-write | Declare `contents: read` at the top |
| Adding `id-token: write` to every job | Any step can request an OIDC token | Grant it only to the deploy job |
| Never updating pins | Missing security fixes | Enable Dependabot for `github-actions` |
Pinning and permissions work together. Pinning controls which code runs. Permissions control how much damage that code can do if it turns out to be hostile.
Exercise
A workflow uses uses: acme/deploy-action@v2 and has no permissions block. Name two changes that reduce the risk, and say what each one protects against.
Show answer
First, replace @v2 with the full 40-character commit SHA of the reviewed release, with a version comment. This protects against the tag being moved to malicious code. Second, add a top-level permissions: contents: read block and grant extra scopes only to the jobs that need them. This limits what a compromised action can do with the GITHUB_TOKEN.