Sub topic 4
The permissions key and access levels
What you need before this chapter
This chapter assumes you can read a workflow file (on, jobs, steps) and know that each job gets a GITHUB_TOKEN. It also assumes you know that the token's permissions decide what API calls a step can make. Here you learn the syntax for controlling those permissions and the rules for how GitHub combines them. Which scopes to pick for specific tasks comes in later chapters.
What the permissions key does
The permissions key tells GitHub which scopes the job's GITHUB_TOKEN gets and at what level. It is optional. If you leave it out, the token gets the repository's default workflow permissions, which are either permissive or restricted. An organization or enterprise can set that default and override the repository's choice. A workflow with no permissions key therefore behaves differently from one repository to the next. Writing the key explicitly makes the workflow's behavior independent of those settings.
You can put the key in two places:
- At the top level of the workflow, where it applies to every job.
- Inside a job, where it applies to that job only.
The three access levels
Each scope takes one of three values.
| Level | Meaning |
|---|---|
| `read` | The token can read that resource through the API. |
| `write` | The token can read and modify that resource. `write` includes `read`. |
| `none` | The token has no access to that resource. |
Two details matter here:
- You do not need to list
readseparately when you grantwrite. - The
id-tokenscope accepts onlywriteornone. Itswritelevel lets a job request an OIDC token. It does not let the job change anything in the repository.
A scope is a named area of the GitHub API. A few of them are contents, issues, pull-requests, packages, actions, checks, statuses, deployments, pages, and security-events. The next chapter looks at the ones you will use most. This chapter treats them as names.
Writing the key
The usual form is a mapping from scope to level:
name: ci
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testThis workflow can check out code and read repository contents. It cannot comment on issues, create releases, or push packages, because those scopes are no longer granted.
There are also three shorthand forms. Each is a replacement for the mapping, not an addition to it:
permissions: read-allpermissions: write-allpermissions: {}read-all gives read access to every scope. write-all gives write access to every scope. {} is an empty mapping and gives none to every scope. Only {} follows least privilege as a starting point. read-all still exposes private data such as security alerts to every step, and write-all is as broad as the token can be.
Unspecified scopes become none
If you write a permissions key and list even one scope, every scope you did not list is set to none. The key replaces the repository default instead of adjusting it.
Suppose the repository default is permissive, which grants write access to most scopes. This workflow:
permissions:
issues: writegives the token issues: write and nothing else. The token can no longer read repository contents either. A step that runs actions/checkout in a private repository fails, because contents is none. If the job needs both, list both:
permissions:
contents: read
issues: writeThis all-or-nothing behavior is what makes an explicit key safe. The permissions you see in the file are the permissions the token has, plus the exceptions in the callout below.
How the effective permissions are decided
Each job's token is built from one of three sources, checked in order:
- The job's own
permissionskey, if present. - Otherwise the workflow's top-level
permissionskey, if present. - Otherwise the repository's default workflow permissions.
Only one source applies. GitHub does not merge the job key with the workflow key.
Job-level keys replace, they do not merge
A job-level key overrides the workflow-level key completely for that job. Look at this workflow:
name: label
on: pull_request
permissions: {}
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/labeler@v5The workflow denies everything. The label job then states its own full set: read contents, write pull requests. Any job added later that has no key of its own gets nothing, so a new job cannot gain access by accident. That is the main reason to combine a restrictive workflow-level key with job-level grants.
The replacement rule can also trip you up. Say the workflow sets contents: read at the top, and a job sets only pull-requests: write. That job's token has pull-requests: write and contents: none. The top-level contents: read does not carry over. If the job checks out code, you must repeat contents: read in the job.
Validation and syntax errors
GitHub checks the key when it parses the workflow. These mistakes make the file invalid, and the run does not start:
- A scope name that does not exist, such as
content: readinstead ofcontents: read. - A level that is not
read,write, ornone, such aspermissions: contents: admin. - Writing
id-token: read, since that scope has noreadlevel. - Mixing a shorthand with a mapping, such as
permissions: read-allfollowed by indented scopes.
Scope names use hyphens, as in pull-requests and security-events. A typo in YAML indentation that moves permissions under on or under steps also breaks the file, because the key is valid only at workflow or job level, not at step level. A step cannot have its own token permissions. If one step in a job needs more access than the others, move it into a separate job that requests the extra scope.
Checking the repository default
The workflow key matters most when the default is wide. You can read the repository's current default with the GitHub CLI, and you can set it to read-only:
gh api repos/OWNER/REPO/actions/permissions/workflow
gh api --method PUT repos/OWNER/REPO/actions/permissions/workflow \
-f default_workflow_permissions=read \
-F can_approve_pull_request_reviews=falseWith read as the default, a workflow with no permissions key gets read access to contents and packages. Other scopes are none. Even so, keep the explicit key in each workflow. It documents intent and survives a change to the default.
Tracing a job by hand
To predict what a token can do, ask three questions in this order:
- Does the job have a
permissionskey? If yes, only that key counts. - If not, does the workflow have one? If yes, only that counts.
- If not, what is the repository default?
Then treat every scope not named in the winning key as none. When a call fails with a permissions error, this trace tells you which scope is missing and where to add it.
Exercise
A workflow has permissions: { contents: read } at the top level. Its release job declares permissions: { contents: write }. Its lint job has no permissions key. A third job, comment, declares permissions: { pull-requests: write } and runs actions/checkout. What does each job's token get, and what goes wrong in comment?
Show answer
release gets contents: write only. lint has no job key, so it uses the workflow key and gets contents: read only. comment gets pull-requests: write and nothing else, because its key replaces the workflow key. Its contents scope is none, so actions/checkout fails in a private repository. The fix is to add contents: read to the comment job's key.
Exercise
Why is permissions: {} at the top of a workflow safer than omitting the key, and why is permissions: read-all not a good substitute?
Show answer
Omitting the key makes the token depend on the repository or organization default, which may be permissive. {} sets every scope to none regardless of that setting, so each job must ask for what it needs. read-all grants read access to every scope, including ones such as security events that most jobs never use, so a compromised step could read more than it should.