Sub topic 6
Starting from no permissions
This chapter covers the permissions: {} pattern: removing every permission from the GITHUB_TOKEN at workflow level, then granting access back one job at a time. It assumes you can read workflow YAML (on, jobs, steps) and that you know the permissions key accepts read, write, and none for each scope. The previous chapters covered that key and the common scopes.
Why start from nothing
Setting permissions: contents: read at the top of a workflow is a good baseline. It is still a choice, though. Every job in the file receives read access to repository contents, including jobs that never touch the repository.
An empty block makes the opposite choice. No job gets anything unless it asks. A new job added six months from now starts with no access, and the author must decide what it needs. This is deny by default.
permissions: {}The braces are an empty mapping. This is different from leaving the permissions key out. If you omit the key, the token takes the repository's default workflow permissions, which may be permissive or restricted depending on settings. With permissions: {}, the workflow file itself decides, and the answer is "nothing".
What an empty block does
When a workflow or job contains a permissions key, every scope you do not list is set to none. The same rule applies to a block with one entry. For example, this block grants issues: write and removes everything else, including contents:
permissions:
issues: writeAn empty mapping is the extreme case of that rule: nothing is listed, so every scope is none.
| Configuration | Scopes not listed |
|---|---|
| No `permissions` key | Use the repository, organization, or enterprise default |
| `permissions: {}` | `none` for every scope |
| `permissions: contents: read` | `none` for every scope except `contents` |
| `permissions: read-all` | `read` for every scope |
| `permissions: write-all` | `write` for every scope |
Avoid read-all and write-all in files you maintain. They are the opposite of the approach in this chapter, because they hand out access without naming it.
A workflow that starts empty
The next workflow labels pull requests. Only one job exists, and it names exactly what it needs.
name: label
on: pull_request
permissions: {}
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/labeler@v5Two details matter here. First, a job-level permissions block replaces the workflow-level block for that job. It does not merge with it. The job's token has contents: read and pull-requests: write, and every other scope is none. Second, the workflow-level block still protects any job you add later. A second job with no permissions key inherits the empty workflow block and gets no access.
flowchart TDThe diagram below shows how a job's final token is chosen.
Jobs that need nothing
Many jobs need no token access at all. A job that runs unit tests on code it received some other way, or a job that only calls an external service with its own secret, can run with an empty token.
There is one catch. actions/checkout uses the GITHUB_TOKEN to fetch the repository. In a private repository, a job with no contents: read cannot check out the code. In a public repository, checkout of the repository itself can still work without a token, because the contents are public. Do not depend on that. If a job checks out code, give it contents: read explicitly, so the workflow behaves the same when a repository changes visibility.
name: ci
on: [push, pull_request]
permissions: {}
jobs:
lint:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- run: make lint
notify:
runs-on: ubuntu-latest
steps:
- run: echo "This job has no token access"The notify job has no permissions key, so it inherits the empty workflow block. It still receives a token, because GitHub creates one for every job. The token simply grants nothing.
Checking what a job actually has
You can confirm a token's effective permissions by making a request that needs a specific scope and reading the result. The following step lists open pull requests, which requires pull-requests: read:
jobs:
probe:
runs-on: ubuntu-latest
permissions: {}
steps:
- run: |
curl --silent --show-error --output /dev/null \
--write-out "%{http_code}\n" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/pulls"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}In a private repository, this prints 403 or 404 because the token has no access. In a public repository the endpoint is readable without authentication, so it can succeed anyway. For that reason, test scopes against a private repository, or pick an endpoint that requires write access.
Common mistakes
- Mixing up
{}and a missing key. A workflow with nopermissionskey can be much more permissive than you expect, depending on the repository setting. - Expecting a job block to add to the workflow block. It replaces it. If the workflow grants
contents: readand a job grants onlypackages: write, that job losescontents: readand checkout fails in a private repository. - Writing the block at the wrong indentation. A
permissionskey understepsis invalid. It is valid only at the top of the workflow and directly under a job. - Treating
noneas the same as omission inside a block. Both result in no access, but writingnoneexplicitly can help readers see that a scope was considered. Use it sparingly, since an empty block already covers every unlisted scope. - Forgetting that some scopes cannot be raised. Fork pull requests and some bot-triggered runs have a token capped below what the file asks for. Those cases are covered in a later chapter.
Where the deny-by-default rule stops
An empty block removes access for the GITHUB_TOKEN only. Secrets you pass to steps, personal access tokens, and cloud credentials are unaffected. A step that has a long-lived personal access token in an environment variable can still use it, whatever the permissions key says. The empty block limits one credential. It does not make a workflow safe by itself.
Exercise
A workflow has permissions: {} at the top. The build job has no permissions key and starts with actions/checkout@v4, in a private repository. The release job has permissions: packages: write and also checks out the code. Which jobs can fail at checkout, and how do you fix each?
Show answer
Both can fail. build inherits the empty block, so its token has no contents: read. Add permissions: contents: read to the build job. release has a job-level block that replaces the workflow block, and that block lists only packages: write, so contents is none. Change it to list both contents: read and packages: write.
Exercise
What is the difference between omitting the permissions key and writing permissions: {}?
Show answer
Omitting the key makes the token use the default workflow permissions from the repository, organization, or enterprise settings, which can include write access. Writing permissions: {} sets every scope to none regardless of those settings.