Sub topic 8
Limiting GITHUB_TOKEN permissions
This chapter assumes you can read a workflow file, know what jobs and steps are, and have seen secrets and vars used in a workflow. You will learn how to restrict what the automatic token can do.
What the GITHUB_TOKEN is
Every workflow run gets a short-lived credential called GITHUB_TOKEN. GitHub creates it when a job starts and revokes it when the job ends. Steps use it to call the GitHub API, for example to check out code, comment on a pull request, or publish a package.
If the token can do too much, a compromised step or a malicious third-party action can abuse it. Limiting its permissions reduces the damage. This is the principle of least privilege: give each job only what it needs.
Where the defaults come from
A repository or organization setting decides the default permissions. Newer repositories usually default to read-only access. Older ones may default to read and write. Check this under Settings, then Actions, then General, then Workflow permissions. Do not rely on the default. Declare permissions in the workflow so the file states what it needs.
The permissions key
You can set permissions at two levels:
- At the top of the workflow, where it applies to every job.
- Inside a job, where it replaces the workflow-level setting for that job.
When you list any permission, every scope you don't list is set to none. This makes an explicit list safe by default.
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testThis workflow can read repository contents and nothing else. The checkout step works because it only needs contents: read.
Raising permissions for one job only
A deploy job often needs more than a build job. Grant extra scopes in that job instead of at the top of the file.
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "build"
deploy:
needs: build
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- run: echo "deploy"Only deploy can request an OIDC token, through id-token: write. A later chapter uses this for cloud authentication. The build job keeps the read-only baseline.
Common scopes
| Scope | What it allows | Typical use |
|---|---|---|
| `contents` | Read or write repository files, releases, and tags | Checkout, creating a release |
| `pull-requests` | Read or write pull requests | Posting a comment |
| `id-token` | Request an OIDC token (`write` only) | Cloud login without stored keys |
| `packages` | Read or write GitHub Packages | Publishing a container image |
| `deployments` | Create deployment records | Reporting deployment status |
Each scope accepts read, write, or none. A shortcut is permissions: {}, which sets every scope to none.
Common errors
Resource not accessible by integration. A step needed a scope the job lacks. Read the failing step to see which API it called, then add that scope to the job.- Checkout fails in a private repository. You set
permissionswithoutcontents: read. Listing any scope removes the unlisted ones, including contents. - OIDC login fails. The job is missing
id-token: write. Adding it at the workflow level also works but gives every job the right to request a token. - Pull requests from forks. The token is read-only for runs triggered by forks, whatever you declare.
Exercise
Your workflow sets permissions: contents: read at the top. The deploy job needs to request an OIDC token and still check out code. What should the deploy job's permissions block contain, and why not put the change at the top?
Show answer
The deploy job should contain id-token: write and contents: read. A job-level block replaces the workflow-level one, so contents: read must be repeated or checkout fails. Putting id-token: write at the top would let every job, including build and test jobs that run third-party actions, request an OIDC token.