Sub topic 2
Classic tokens and why broad scopes are a problem
This chapter assumes you can make authenticated REST calls with curl, you know what a bearer token is, and you have used GitHub repositories and organizations. It covers classic personal access tokens (PATs) and their scopes, and explains why fine-grained credentials replace them in most cases.
What a classic personal access token is
A classic personal access token is a string that stands in for your password when you call the GitHub API or use Git over HTTPS. You create it in your account settings under Developer settings. Classic tokens usually start with the prefix ghp_.
A classic token has three properties that matter for security:
- It acts as you. Every request made with the token is attributed to your user account.
- It is limited only by scopes. A scope is a coarse label, such as
repo, that you tick when you create the token. - It is not tied to a repository list. The scopes apply to every repository your account can reach.
The third property is the root of the problem. When you create a classic token, you cannot say "only this repository". You can only say what kind of access the token has. The token then applies that access to everything you can reach.
How scopes work
GitHub's classic tokens use OAuth scopes. A scope names a category of access. The table lists the ones you meet most often.
| Scope | What it grants |
|---|---|
| `repo` | Full read and write access to code, issues, pull requests, commit statuses, deployments, and settings in every repository you can access, including private ones |
| `public_repo` | The same kind of access, limited to public repositories |
| `workflow` | Permission to create and update GitHub Actions workflow files |
| `read:org` | Read access to organization membership and teams |
| `admin:org` | Full management of organization settings and membership |
| `delete_repo` | Permission to delete repositories you administer |
| `gist` | Permission to create and edit gists |
| `admin:repo_hook` | Full control of repository webhooks |
Some scopes include others. For example, repo is a group that contains repo:status, repo_deployment, public_repo, repo:invite, and security_events. Ticking one box can therefore grant several kinds of access.
You can see which scopes a classic token holds by reading a response header. The API returns X-OAuth-Scopes for scopes the token has and X-Accepted-OAuth-Scopes for scopes the endpoint accepts.
curl -s -I \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/user | grep -i "x-oauth-scopes"For a token created with only repo and read:org, the output looks like this:
x-oauth-scopes: read:org, repoFine-grained tokens do not return this header, because they do not use scopes. Seeing it is one quick way to tell the two token types apart.
Why broad scopes are a problem
A scope is a category, not a target. Three consequences follow from that.
The blast radius is your whole account
Suppose you want a script to read issues in one repository. The nearest classic scope is repo. That scope gives the token read and write access to every repository you can reach, in every organization where you hold access. If the token leaks, an attacker gets the same reach. They can push code, read private source, change settings, and read secrets exposed through those repositories.
The diagram shows how one classic token fans out.
Scopes cannot express the task
The scope list is short and coarse. There is no scope that means "read issues in one repository" or "write commit statuses only". To get a narrow capability you usually accept a much wider one. This is why scripts end up holding repo even when they need almost none of it.
Tokens are long-lived and portable
Classic tokens can be created with no expiration. A token with no expiry stays valid until someone revokes it. People paste such tokens into CI settings, shell profiles, .env files, and chat messages. Each copy is another place the token can leak, and nothing limits where it can be used from. Anyone holding the string can use it from any machine.
Combine the three: a token that is powerful, hard to narrow, long-lived, and easy to copy. Leaked credentials of this kind are a common route to supply-chain incidents, because an attacker with write access to code can alter what other people build and run.
Organizations and classic tokens
Organization owners have some controls over classic tokens, but they are blunt.
- An organization using SAML single sign-on requires you to authorize a classic token for the organization before it works there. The authorization is a switch for the whole token, not a narrowing of what it can do.
- An organization can restrict classic token access, so that classic tokens cannot reach its resources at all. This is an all-or-nothing choice.
- Organization owners cannot easily see why a member's token needs the access it has, since the token carries only scope names.
The result is that an owner can allow the risk or block the token type. There is no middle option such as "allow this token on these three repositories with read-only issue access". Fine-grained tokens add that middle option, which later chapters cover.
A concrete example of over-grant
Consider a deploy script that must read the latest release of a private repository. Here is what the call needs.
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/releases/latestThe call is read-only and touches one repository. A classic token that makes it work needs repo. With repo, the same token can also run these calls, even though the script never does:
DELETE /repos/OWNER/REPOis blocked only becausedelete_repois a separate scope, butPUT /repos/OWNER/REPO/contents/PATHcan overwrite files.POST /repos/OTHER-OWNER/OTHER-REPO/issuescan create issues in any other repository you can write to.GET /user/reposlists every private repository you can reach, which helps an attacker plan.
The gap between what the script needs and what the token allows is the over-grant. The goal of least privilege is to shrink that gap to nothing or close to it.
Where classic tokens still appear
Classic tokens are not removed, and you will still meet them. Reasons include:
- Some older tools and scripts only accept them.
- A few API areas and workflows have historically needed classic tokens, so check the current GitHub documentation before assuming a fine-grained token works for a given endpoint.
- Existing tokens keep working until they expire or are revoked.
Treat a classic token as a legacy credential. If you must use one, reduce the risk:
- Choose the smallest scope that works. Prefer
public_repooverrepowhen only public repositories are involved. - Set an expiration date, such as 30 days.
- Give it a note that names the tool and the owner, so you can find and revoke it later.
- Store it in a secret manager or CI secret store, never in source code.
- Review the list of tokens regularly and delete those you no longer use.
Spotting classic tokens in your own account
You can audit your tokens in the browser at Settings, Developer settings, Personal access tokens, Tokens (classic). Each entry shows its note, scopes, and last-used information. Look for tokens that have:
- the
reposcope together with no expiration, - no recent use,
- a vague or empty note.
Delete any token whose purpose you cannot name. If a script breaks, you have learned that it still depended on the token, and you can replace it with a narrower credential.
From classic scopes to fine-grained credentials
The problems in this chapter point to what a better credential needs:
- A chosen owner and a chosen list of repositories, instead of "everything you can reach".
- Named permissions with read or write levels, instead of broad scope groups.
- A required expiration, instead of tokens that never end.
- An approval step in organizations, instead of a single yes or no for the token type.
Fine-grained personal access tokens provide these four properties, and GitHub Apps provide them for automation that should not depend on one person's account. The next chapters build on this, starting with how fine-grained personal access tokens are structured.
Exercise
A script only needs to read releases from one private repository. A teammate suggests creating a classic token with the repo scope. Give two reasons this is a poor choice, and name what you would want from a better credential.
Show answer
First, the repo scope is a category, not a target. The token would get read and write access to every repository the teammate's account can reach, so a leak exposes far more than the one repository. Second, classic tokens can have no expiration and can be used from anywhere, so a copied token stays dangerous until someone revokes it. A better credential would be limited to the one repository, carry only read access to the release data, and expire on a set date. A fine-grained personal access token or a GitHub App installation token fits that description.
Exercise
You run curl -s -I against https://api.github.com/user with a token and the response has no x-oauth-scopes header. What does that suggest about the token, and why?
Show answer
It suggests the token is not a classic token. Fine-grained tokens and installation tokens do not use OAuth scopes, so the API does not return the header for them. Check the token prefix and the token type in your settings to confirm.