Sub topic 8
Restricting the GITHUB_TOKEN with permissions
This chapter assumes you have a working lint and format workflow like the one from the earlier chapters. It also assumes you know basic YAML and how a pull request triggers a workflow. Here you will limit what the workflow's token is allowed to do.
What the GITHUB_TOKEN is
Every workflow run gets a short-lived credential called GITHUB_TOKEN. GitHub creates it when the run starts and revokes it when the run ends. Actions such as actions/checkout use it to read your repository. Any step you write can also use it to call the GitHub API.
The token's default access depends on your repository or organization settings. Older repositories often default to read and write access for many scopes. A lint job needs to read code and nothing else. If a third-party action in the job were compromised, a broad token would let it push commits, edit releases, or comment on issues.
The fix is least privilege. You declare only the access the job needs, and GitHub denies everything else.
Setting permissions
Add a top-level permissions key to the workflow file. It applies to every job in the file.
name: Lint
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
concurrency:
group: lint-${{ github.ref }}
cancel-in-progress: true
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run format:checkcontents: read lets the job check out the code. This is the minimum for almost every workflow. The concurrency block is covered in the next chapter. It is included here so you can see where both keys sit in the file.
Once you set any permissions key, every scope you do not list becomes none. You do not need to write issues: none or pull-requests: none.
Scopes you might add
Some tools need more than read access. Add a scope only when a step fails without it.
| Scope | Level | When a lint workflow needs it |
|---|---|---|
| `contents` | `read` | Always, to check out the code |
| `pull-requests` | `write` | A tool that posts review comments on the pull request |
| `checks` | `write` | A tool that creates its own check runs with annotations |
| `contents` | `write` | Only if the job pushes auto-fix commits |
Inline annotations from ::error workflow commands do not need extra scopes. The runner handles those itself.
Job-level permissions
You can also put permissions inside a single job. The job-level block replaces the top-level one for that job, and it does not merge with it.
permissions:
contents: read
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lint
comment:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- run: echo "This job may comment on pull requests"A good pattern is a strict top-level default of contents: read. Then each job that needs more access asks for it by name.
Common mistakes
- Forgetting that unlisted scopes become
none. If you addpermissions: contents: readand a tool then fails with "Resource not accessible by integration", it needs another scope. Read the error, then add that one scope. - Expecting a merge of job and workflow blocks. A job-level block replaces the top-level block. Repeat
contents: readin the job if it still needs checkout. - Using
permissions: write-allto make an error go away. This restores the broad access you were trying to remove. - Expecting forked pull requests to get write access. For pull requests from forks, the token is read-only no matter what you declare.
Exercise
A workflow has a top-level permissions: contents: read. One job adds its own block containing only pull-requests: write. That job then fails at actions/checkout. Why, and how do you fix it?
Show answer
A job-level block replaces the top-level block instead of merging with it. The job's block lists only pull-requests: write, so contents became none and checkout cannot read the repository. Add contents: read to the job's block.