Sub topic 3
Setting the default to read-only
This chapter assumes you can read a workflow file (on, jobs, steps) and know that every job gets a GITHUB_TOKEN. Here you change the default permissions that token receives when a workflow does not set its own.
Why the default matters
A workflow without a permissions key gets whatever the repository default allows. That default is the baseline for every workflow in the repository, including old ones nobody reviews. Setting it to read-only means a forgotten workflow cannot push code, publish packages, or edit issues.
GitHub offers two defaults:
| Setting | What the token can do by default |
|---|---|
| Permissive | Read and write on most scopes, including `contents` and `packages` |
| Restricted (read-only) | Read access to `contents` and `packages` only |
Repositories and organizations created after early 2023 start with the restricted default. Older ones may still use the permissive one, so check them.
Where the setting lives
You can set the default at three levels:
- Enterprise: Policies, then Actions.
- Organization: Settings, then Actions, then General.
- Repository: Settings, then Actions, then General, under "Workflow permissions".
A higher level limits the levels below it. If an organization chooses read-only, its repositories cannot pick the permissive option. A repository can still be stricter than its organization.
The same panel has a checkbox, "Allow GitHub Actions to create and approve pull requests". Leave it off unless a workflow must approve pull requests. If it is on, a compromised workflow can approve its own malicious change.
Changing it from the command line
The web panel works for one repository. For many repositories, use the GitHub CLI against the REST API. The first command reads the current setting. The second sets the default to read-only and turns off pull request approval.
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=falseUse -f for the string value and -F for the boolean, so the API receives false as a boolean instead of the text "false". The GET response shows default_workflow_permissions as read or write. Run it again after the PUT to confirm the change. You need admin rights on the repository.
What breaks after the change
Workflows that quietly relied on write access now fail. Typical cases are a job that pushes a commit, comments on a pull request, or publishes a package. The log shows Resource not accessible by integration. The fix is not to revert the default. Add a permissions block to that job with only the scope it needs. Later chapters show how to find that scope.
Exercise
A repository is set to read-only default permissions. A workflow has no permissions key and one job runs git push to a branch. What happens, and what is the correct fix?
Show answer
The push fails with a permission error, because the token only has read access to contents. The fix is to add permissions: contents: write to that job only, not to change the repository default back to read and write.