Sub topic 3
Setting the default to read-only
This chapter assumes you can read and write basic workflow YAML (on, jobs, steps) and that you know what the GITHUB_TOKEN is from the earlier chapters. You should also be comfortable with the GitHub CLI (gh) or the repository settings pages. By the end, you will be able to set the default token permissions at enterprise, organization, and repository level, check the current value from the command line, and predict which workflows will break when you tighten it.
What the default controls
Every job gets a GITHUB_TOKEN. Its permissions come from one of two places:
- The
permissionskey in the workflow or job, if one is present. - The default workflow permissions setting, if no
permissionskey applies.
The default is a safety net for workflows whose authors never wrote a permissions key. Setting it to read-only means a forgotten or copy-pasted workflow starts with a small token instead of a powerful one. A permissions key in a workflow always wins over the default, so the setting never blocks a workflow that asks for more. It only changes what you get when you don't ask.
The two options
The settings page offers two choices.
| Option | Label in the UI | What the token gets |
|---|---|---|
| Restricted | Read repository contents and packages permissions | `contents: read`, `packages: read`, `metadata: read` |
| Permissive | Read and write permissions | Read and write on most scopes, including `contents`, `packages`, `issues`, `pull-requests`, `actions`, `checks`, `deployments`, `pages`, `statuses`, and `security-events` |
In both cases id-token stays unset unless a workflow requests it, and metadata is always read. The API names for the options are read (restricted) and write (permissive).
The permissive default is the dangerous one. A step that runs untrusted code can push commits, edit issues, approve pull requests, or publish packages without anyone having granted it that ability on purpose.
Repositories, organizations, and enterprises created since early 2023 start with the restricted default. Older ones keep whatever they had, so do not assume an old repository is safe. Check it.
Where the setting lives
The setting exists at three levels, and each level can only tighten what the level above allows.
- Enterprise. Owners set the default for all organizations under Policies > Actions > Workflow permissions.
- Organization. Owners set it under Settings > Actions > General > Workflow permissions. If the enterprise chose restricted, the permissive option is disabled here.
- Repository. Admins set it under Settings > Actions > General > Workflow permissions. If the organization chose restricted, the permissive option is disabled here too.
A policy set higher up can lock the choice for everything below it. If you cannot change the setting in your repository, look at the organization and enterprise settings. Setting the organization default to restricted does not rewrite repositories that already chose permissive in the past, so you still need to check them (see the audit section below).
The pull request approval switch
Next to the default sits a checkbox: Allow GitHub Actions to create and approve pull requests. When it is on, a workflow token can open pull requests and submit approving reviews. Combined with a write token, that lets a workflow approve its own changes and defeat required-review rules.
Turn it off unless you have a specific workflow that opens pull requests, such as a release or dependency-update bot. If you do need that workflow, enable the setting and give only that job pull-requests: write and contents: write.
Reading and changing the setting with `gh`
The settings pages are fine for one repository. For many repositories, use the REST API through the GitHub CLI. You need admin access to the repository. The first command shows the current values. The second sets the default to read-only and stops workflows from approving pull requests.
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=falseThe GET call returns JSON like this:
{
"default_workflow_permissions": "write",
"can_approve_pull_request_reviews": true
}Note the two flags. -f sends the value as a string, which is right for read. -F converts false to a real JSON boolean. If you used -f for the second field, the API would receive the string "false" and reject the request.
The same endpoint exists for organizations, with the same body fields:
gh api --method PUT orgs/ORG/actions/permissions/workflow \
-f default_workflow_permissions=read \
-F can_approve_pull_request_reviews=falseAuditing many repositories
Before tightening an organization, list which repositories are still permissive. This loop prints one line per repository. It needs a token that has admin access to each one, and repositories you cannot administer will print an error instead of a value.
gh repo list ORG --limit 500 --no-archived \
--json nameWithOwner --jq '.[].nameWithOwner' |
while read -r repo; do
value=$(gh api "repos/$repo/actions/permissions/workflow" \
--jq .default_workflow_permissions 2>/dev/null || echo "no-access")
echo "$repo $value"
doneRepositories that print write are your first targets. Repositories that print no-access need someone with admin rights to look at them. Fix the loud ones first: public repositories, repositories that publish packages, and those with deploy secrets.
What breaks when you switch to read-only
Moving from permissive to restricted does not change any workflow that already has a permissions key. It changes workflows that have none, and those that relied on write access will fail. The typical symptoms are:
- A job that labels or comments on a pull request fails with
Resource not accessible by integration. - A release job that pushes a tag or creates a GitHub Release fails with a 403.
- A job that pushes an image to GitHub Container Registry fails to authenticate or gets a permission denied error.
The fix is not to flip the default back. Add a permissions block to the failing job with only the scope it needs. A job that labels pull requests, for example, needs only this:
name: label
on: pull_request
permissions: {}
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/labeler@v5The workflow-level permissions: {} removes everything, and the job adds back contents: read (for the labeler to read its config) and pull-requests: write. Later chapters cover this pattern in detail. For now, the point is that the default and the permissions key work together. The default protects workflows nobody has reviewed yet, and the key documents what each reviewed workflow actually needs.
A safe rollout order
Changing a default across an organization can break builds on many teams at once. A staged approach reduces the risk.
- Run the audit loop and record which repositories are permissive.
- Search for workflows that write to GitHub without a
permissionskey. Searching their steps forgh pr,gh release,git push,docker push, or actions that comment and label is a good start. - Add explicit
permissionsblocks to those workflows first. This change is safe under either default. - Switch the repository default to read-only, and watch the next runs.
- Once the busy repositories are stable, set the organization default to restricted so new and forgotten repositories inherit it.
Adding the explicit blocks before changing the setting means the switch itself changes nothing for the workflows you reviewed.
Exercise
A repository has the default set to "Read and write permissions". A workflow has permissions: { contents: read } at the top level and no job-level permissions key. What can the job's token do with issues, and what changes if you switch the repository default to read-only?
Show answer
The job token gets only contents: read. Once a permissions key is present, every scope not listed is set to none, so the token cannot touch issues either way. Switching the default to read-only changes nothing for this workflow, because the permissions key overrides the default. The change only affects workflows with no permissions key.
Exercise
You run gh api --method PUT repos/OWNER/REPO/actions/permissions/workflow -f default_workflow_permissions=read -f can_approve_pull_request_reviews=false and the API rejects the request. What is the likely cause and the fix?
Show answer
The -f flag sends false as the string "false", but the API expects a JSON boolean. Use -F can_approve_pull_request_reviews=false so the CLI sends a real boolean.