Sub topic 6
Starting from no permissions
This chapter assumes you can read workflow YAML and know the difference between workflow-level and job-level keys. It also assumes you know that the GITHUB_TOKEN is created for each job and expires when the job ends.
Two ways to end up with no access
The permissions key has two behaviors that give you a clean starting point.
- An empty mapping,
permissions: {}, sets every scope tonone. - Any listed scope turns every unlisted scope to
none. If you write onlycontents: read, thenissues,pull-requests,packagesand the rest are allnone.
The second behavior means you rarely need to write none by hand. You name what you need, and everything else is denied.
An empty block at workflow level
Putting permissions: {} at the top of the workflow makes denial the default for every job. Each job then asks for 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@v5If you add a second job to this workflow and forget to give it a permissions block, it runs with no access. That is the intended failure mode. A new job cannot pick up access it did not ask for.
Jobs that need nothing
Many jobs never call the GitHub API. A linter that runs on code passed in some other way, or a job that only calls an external service with its own credentials, can run with an empty block.
name: notify
on: workflow_dispatch
permissions: {}
jobs:
ping:
runs-on: ubuntu-latest
steps:
- run: echo "No GitHub API access needed here"The token still exists for this job. It just carries no scopes, so a compromised step has nothing useful to steal.
Job-level blocks replace, not merge
A permissions block on a job replaces the workflow-level block. It does not add to it. In this example the job has packages: write and nothing else, even though the workflow grants contents: read.
permissions:
contents: read
jobs:
push:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- run: echo "contents is none here"If this job also ran actions/checkout on a private repository, checkout would fail. List contents: read again in the job block.
Common mistakes
- Writing
permissions:with nothing after it. YAML reads that asnull, not as an empty mapping. Writepermissions: {}. - Assuming
permissions: {}makes checkout fail everywhere. Public repositories can still be read without a token, but private repositories needcontents: read. - Relying on the workflow-level block while a job defines its own. The job's block wins entirely.
Exercise
A workflow sets permissions: {} at the top. One job has permissions: { pull-requests: write } and runs actions/checkout on a private repository, then comments on a pull request. What goes wrong, and how do you fix it?
Show answer
The job's block replaces the workflow's block, and it lists only pull-requests: write, so contents is none. Checkout fails because it cannot read the private repository. Add contents: read to the job's permissions block.