Sub topic 8
Restricting GITHUB_TOKEN permissions
This chapter assumes you can read a workflow file, know the difference between jobs and steps, and understand that pull_request_target runs in the base repository's context. The earlier chapters showed how an attacker can run code in your job. Token permissions decide how much damage that code can do.
What GITHUB_TOKEN is
Every workflow run gets a GITHUB_TOKEN. It is a short-lived credential for the repository's API, and GitHub creates it when the job starts and revokes it when the job ends. Any step in the job can read it, including steps running injected code.
The token's default scope depends on repository and organization settings. Older repositories often default to read and write access on most scopes. Newer ones default to read-only. Don't rely on the default. Declare what you need.
Setting permissions explicitly
Use the permissions key. At workflow level it applies to every job. At job level it overrides the workflow level for that job.
name: label-pr
on:
pull_request_target:
types: [opened]
permissions:
contents: read
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/github-script@v7
env:
PR_TITLE: ${{ github.event.pull_request.title }}
with:
script: |
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ["needs-review"]
})The workflow level grants only contents: read. The one job that must write labels adds pull-requests: write. If an attacker injects code into this job, the token still cannot push commits or change releases.
Rules that matter
- Once you list any permission, every scope you don't list becomes
none. This is useful: list only what you need. permissions: {}removes all scopes. Use it for jobs that need no API access.read-allandwrite-allexist, but they defeat the purpose. Avoid them.- Job-level
permissionsreplace the workflow-level block. They do not merge with it. - Forked
pull_requestruns get a read-only token no matter what you write.pull_request_targetruns get what you declare, so the block matters most there.
How permissions limit an attack
Common mistakes
- Leaving the permissions block out and trusting the repository default, which may have changed or may be broader than you think.
- Setting broad permissions at workflow level for one job's need. Put the write scope on that job only.
- Forgetting that the token is not the only secret. Least privilege on
GITHUB_TOKENdoes not protect repository secrets that a job can read.
Exercise
A workflow triggered by pull_request_target has no permissions block, and the repository default is read and write. A step contains an injectable run command. Name one change that reduces the damage without fixing the injection, and say why it helps.
Show answer
Add permissions: contents: read at workflow level, and grant extra scopes only to the job that needs them. Injected code still runs, but the token can no longer push commits, edit releases, or change other resources. The injection itself must still be fixed by passing the value through an environment variable.