Sub topic 8
Restricting GITHUB_TOKEN permissions
This chapter assumes you can read a workflow file (events, jobs, steps) and know how pull requests from forks work. It also assumes you have seen why ${{ }} injection and pull_request_target are dangerous. Here you learn how to limit the damage when something goes wrong. A smaller token does not fix an injection bug, but it decides how much an attacker can do with it.
What the token is
Every job in a workflow run receives a GITHUB_TOKEN. GitHub creates it when the job starts and revokes it when the job ends. It authenticates as the GitHub Actions app and is available to steps as ${{ secrets.GITHUB_TOKEN }} and, in many actions, as github.token.
The token is scoped to the repository that runs the workflow. Its power depends on a set of permission scopes such as contents, issues and pull-requests. Each scope can be read, write or none.
If an attacker gets code execution in a step, they can usually read this token from the environment or from the runner's git configuration. They can then call the GitHub API with whatever scopes the job has. A token with contents: write can push commits or tags. A token with pull-requests: write can approve or comment on pull requests, subject to repository settings.
Where default permissions come from
A job's permissions come from three layers, in this order:
- The default set in the organization or repository settings (Settings, Actions, General, Workflow permissions).
- Trigger-specific limits that GitHub applies regardless of that default.
- The
permissions:key in the workflow file, which can only narrow or choose within what the earlier layers allow, with the exceptions noted below.
The repository default has two modes:
| Mode | Effect |
|---|---|
| Permissive | Read and write access to most scopes |
| Restricted | Read-only access to `contents` and `packages` (plus `metadata`) |
New repositories in many organizations use the restricted mode, but older repositories and organizations often still use the permissive one. Do not rely on the setting alone. A workflow file that declares its own permissions behaves the same way in every repository.
Triggers apply their own limits:
| Trigger | Token | Secrets |
|---|---|---|
| `push`, `issues`, `schedule` | Repository default | Available |
| `pull_request` from the same repository | Repository default | Available |
| `pull_request` from a fork | Read-only | Not available (except `GITHUB_TOKEN`) |
| `pull_request_target` | Repository default, including write | Available |
| Runs triggered by Dependabot | Read-only by default | Dependabot secrets only |
The pull_request_target row is why this trigger is risky. It gives the workflow a token and secrets while it handles data from an outsider. In that case the permissions: key matters most.
The `permissions` key
You can set permissions: at the top of a workflow, which applies to every job, or inside a single job. The key has four behaviors you need to know.
- If you list any scope, every scope you do not list becomes
none. Listingcontents: readtherefore also removesissues: write. permissions: {}sets every scope tonone.read-allandwrite-allare shortcuts for all scopes. Avoidwrite-all.- A job-level
permissions:block replaces the workflow-level one for that job. The two are not merged.
Behavior 1 surprises people in both directions. Adding one line to restrict a scope can silently break a step that needed another scope. A step that fails with Resource not accessible by integration is usually missing a permission.
A deny-by-default pattern
The safest layout denies everything at the top and grants scopes per job:
name: triage
on:
pull_request_target:
types: [opened]
permissions: {}
jobs:
label:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/github-script@v7
env:
PR_TITLE: ${{ github.event.pull_request.title }}
with:
script: |
const title = process.env.PR_TITLE
if (title.toLowerCase().includes("docs")) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ["documentation"],
})
}
report:
runs-on: ubuntu-latest
steps:
- run: echo "This job has no token permissions at all."The label job needs only pull-requests: write, so its token cannot read repository contents through the API. Adding labels goes through the issues endpoint, which pull-requests: write covers for pull requests. The report job inherits {}. If someone later adds a new job and forgets to think about permissions, it starts with nothing.
The title reaches the script through process.env, so this example also uses the safe pattern from earlier chapters. The two defenses work together. The environment variable stops injection, and the permission block limits what a successful attack could do.
Common scopes and their risk
| Scope | What write access allows | Typical need |
|---|---|---|
| `contents` | Push commits, create releases and tags | Release jobs only |
| `pull-requests` | Comment on, label, or review pull requests | Triage bots |
| `issues` | Create, edit and label issues | Triage bots |
| `actions` | Cancel runs, manage caches and artifacts through the API | Rarely |
| `packages` | Publish packages | Publish jobs only |
| `id-token` | Request an OIDC token for cloud authentication | Deploy jobs only |
| `security-events` | Upload code scanning results | Code scanning |
id-token: write deserves care. It does not grant repository access, but it lets the job request an identity token that cloud providers may accept. If your cloud trust policy is loose, an attacker who controls the job can assume the cloud role.
Checkout leaves the token behind
actions/checkout by default saves the token in the local git configuration so that later git push commands work. Any later step in the job can read it, including steps that run attacker-influenced code such as install scripts. If a job does not need to push, turn this off:
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- run: npm ci && npm testHere the token has read access only and is not stored in .git/config. A malicious dependency script in npm ci would find no credentials to steal. It could still read the checked-out source, which is public anyway in this case.
Limits of this defense
Restricting the token does not protect secrets. A secret you pass into a job, whether through env: or an action input, is exposed to anything that runs in that job. Under pull_request_target, the combination "untrusted code plus a secret" is still fatal even if permissions: {} is set. Use permissions to reduce the token's reach, and keep untrusted code out of jobs that hold secrets.
Permissions also interact with other settings:
- A reusable workflow can keep or reduce the permissions of its caller. It cannot raise them.
- Organizations can disable the option that lets Actions approve pull requests. Turn it off unless you need it, because an attacker with
pull-requests: writecould otherwise approve their own change. - The "Send write tokens to workflows from pull requests" setting overrides the fork read-only rule. Leave it off.
How GitHub decides the final token
A practical checklist
- Set the repository and organization default to restricted.
- Put
permissions: {}orpermissions: contents: readat the top of every workflow. - Grant write scopes only in the job that needs them.
- Split jobs so that the job holding write scopes does not run untrusted code.
- Set
persist-credentials: falseon checkouts that do not push. - Review
id-token: writeandcontents: writein code review as security-relevant lines.
Exercise
A workflow triggered by pullrequesttarget declares permissions: contents: read at the top level. A job in it uses actions/github-script to add a label to the pull request and now fails with "Resource not accessible by integration". What caused the failure, and what is the smallest fix?
Show answer
Listing any scope sets every unlisted scope to none, so the token lost its write access to pull requests and issues. The fix is to add pull-requests: write to the job that adds the label, in a job-level permissions: block that also repeats contents: read if that job needs it, because a job-level block replaces the workflow-level one. Other jobs keep only contents: read.