Sub topic 5
Setting fine-grained permissions
This chapter covers how fine-grained personal access tokens express what they may do. It assumes you can already create a fine-grained token, pick a resource owner, and select repositories, as covered in the previous chapter. It also assumes you can send an authenticated request to the GitHub REST API with curl and an environment variable.
Permissions are separate from repository selection
A fine-grained token answers two questions independently:
- Which resources? The resource owner and the selected repositories decide where the token can act.
- Which actions? The permissions decide what the token can do on those resources.
A request succeeds only when both answers allow it. A token with Contents: Read and write on octo-org/docs cannot push to octo-org/api, because that repository is outside the selection. It also cannot open an issue in octo-org/docs, because the token has no Issues permission.
This differs from a classic token. A classic repo scope grants full control of every repository the user can reach. A fine-grained token starts with nothing and gains only what you add.
The three permission groups
The token creation page splits permissions into groups. Each group applies at a different level.
| Group | Applies to | Examples |
|---|---|---|
| Repository permissions | Only the selected repositories | Contents, Issues, Pull requests, Actions, Metadata |
| Organization permissions | The resource owner organization | Members, Administration, Self-hosted runners |
| Account permissions | Your own user account | Email addresses, Profile, SSH signing keys |
Which groups appear depends on the resource owner. If you choose an organization as the resource owner, you see repository and organization permissions. If you choose your personal account, you see repository and account permissions. Account permissions are not available on a token owned by an organization, because they describe your own user account.
Access levels
Each permission has one of three levels:
- No access. The default. The token cannot use the endpoints that need this permission.
- Read-only. The token can fetch data but cannot change it.
- Read and write. The token can fetch and change data. Write includes read.
Some permissions offer only read-only or only read and write, because the underlying feature has no meaningful alternative. A few have different semantics. For example, Metadata is always read-only.
The levels are cumulative within one permission, not across permissions. Contents: Read and write lets you create a commit. It does not let you comment on a pull request that contains that commit. Commenting needs Pull requests: Read and write, or Issues: Read and write for the issue-style comment endpoint.
Metadata is added automatically
When you select any repository permission, GitHub adds Metadata: Read-only to the token. You cannot remove it. It allows basic repository information such as the name, collaborators list visibility, and topics. Many endpoints that look unrelated to metadata depend on it, so a token with no other permissions can still call GET /repos/OWNER/REPO on a selected repository.
Mapping an API call to a permission
Each REST endpoint documents the fine-grained permission it needs, in a section on the endpoint's reference page named "Fine-grained access tokens for ...". The page lists the required permissions, for example "Issues" repository permissions (write). Some endpoints list several alternatives, and any one of them is enough. Reading the reference before you build a token saves a trial-and-error loop.
A common mapping:
| Task | Endpoint | Permission needed |
|---|---|---|
| Read repository files | `GET /repos/OWNER/REPO/contents/PATH` | Contents: Read |
| Create or update a file | `PUT /repos/OWNER/REPO/contents/PATH` | Contents: Write |
| Create an issue | `POST /repos/OWNER/REPO/issues` | Issues: Write |
| Comment on a pull request | `POST /repos/OWNER/REPO/issues/NUMBER/comments` | Issues: Write or Pull requests: Write |
| Merge a pull request | `PUT /repos/OWNER/REPO/pulls/NUMBER/merge` | Contents: Write and Pull requests: Write |
| Re-run a workflow | `POST /repos/OWNER/REPO/actions/runs/ID/rerun` | Actions: Write |
Treat this table as a guide. Confirm the exact requirement on the endpoint's page, since permissions change as GitHub adds features.
Diagnosing a permission error
Calling an endpoint without the right permission usually returns 403 Resource not accessible by personal access token. A 404 Not Found can also mean the token lacks access. GitHub returns 404 for resources a token cannot see, so that it does not reveal that a private repository exists.
GitHub also reports the permissions an endpoint needs in response headers. The X-Accepted-GitHub-Permissions header lists the permissions that would allow the request. Print the headers to see it:
curl -s -i -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d '{"title":"Permission test"}' \
https://api.github.com/repos/OWNER/REPO/issuesIf the token has only Issues: Read-only, the response is a 403 and the headers include X-Accepted-GitHub-Permissions: issues=write. Add the permission in the token settings, then repeat the call.
Choosing the least permission set
A reliable way to pick permissions is to work backwards from the calls your script makes.
- List every endpoint the script calls.
- Look up the required permission for each on its reference page.
- Grant the lowest level that covers them. Prefer read-only when the script never writes.
- Run the script against a test repository and add only what a 403 reports as missing.
For example, a script that opens an issue when a nightly check fails needs only Issues: Read and write on one repository. It does not need Contents, Pull requests, or Actions.
Edge cases and limits
- Not every endpoint supports fine-grained tokens. Some endpoints still require a classic token or a different credential. The endpoint's reference page says whether fine-grained tokens are supported. Check it before you design around a token.
- Public repositories need no permissions for reads. A fine-grained token with only public repository access can read public data without any added permissions. Writes still need permissions.
- A permission cannot exceed your own access. A token acts as you. If you only have read access to a repository, giving the token
Contents: Writedoes not let it push. - Organization permissions depend on your role. To use
Administrationat the organization level, you must be an owner. The token is limited by both your role and the permission. - Single-owner scope. One token has one resource owner. If your automation must touch repositories in two organizations, you need two tokens, or a GitHub App installed on both.
Worked example: a token that only comments on issues
Suppose a reporting script posts a comment on issue 7 of octo-org/tools. Create a token with these settings:
- Resource owner:
octo-org - Repository access: only
octo-org/tools - Repository permission:
Issues: Read and write
Metadata is added automatically. Then run:
curl -s -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d '{"body":"Nightly report: all checks passed."}' \
https://api.github.com/repos/octo-org/tools/issues/7/commentsThe call returns 201 Created. Now try to change a file with the same token. The PUT to the contents endpoint returns 403, because the token has no Contents permission. If it leaks, an attacker can comment on issues in one repository. They cannot read your code or change it.
Exercise
A script reads files from octo-org/docs and opens a pull request that updates one of them, using the branch and pull request endpoints. It also lists the repository's open issues for a summary. Which repository permissions and levels give it the least access that still works, and why does a token that has these on octo-org/docs still fail on octo-org/api?
Show answer
Grant Contents: Read and write (to read files and create the branch and commit), Pull requests: Read and write (to open the pull request), and Issues: Read-only (to list issues). Metadata read-only is added automatically. The token fails on octo-org/api because permissions apply only to the selected repositories, and octo-org/api was not selected.
Exercise
A request with your token returns 403 Resource not accessible by personal access token. Name two ways to find out which permission is missing.
Show answer
Read the "Fine-grained access tokens" section of the endpoint's reference page, and print the response headers with curl -i to read the X-Accepted-GitHub-Permissions header, which lists the permissions that would allow the call.