Sub topic 2
Classic tokens and why broad scopes are a problem
This chapter assumes you can call a REST API with an Authorization header and have a rough idea of what a bearer token is. It covers what a classic personal access token (PAT) is, how its scopes work, and why those scopes are a poor fit for least-privilege access.
What a classic token is
A classic PAT is a string that GitHub generates for your user account. When you send it in the Authorization header, GitHub treats the request as coming from you. The token carries one or more OAuth scopes, which are coarse labels such as repo or admin:org. Classic tokens usually start with the prefix ghp_.
The request looks the same as with any other token:
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/userGitHub also returns the token's scopes in the X-OAuth-Scopes response header. You can read it with curl -I to check what a token can do.
How scopes work
A scope is a bundle of permissions. You choose scopes when you create the token, and you cannot narrow them per repository afterwards.
| Scope | What it grants |
|---|---|
| `repo` | Full read and write access to every repository you can access, public and private, including code, issues, pull requests, and settings |
| `workflow` | Permission to change GitHub Actions workflow files |
| `admin:org` | Full management of organizations and teams you belong to |
| `delete_repo` | Permission to delete repositories you administer |
The key point is that a scope is checked against you, not against a single project. A token with repo can reach every repository your account can reach, now and in the future.
Why broad scopes are a problem
Suppose a script only needs to read issues in one repository. With a classic token, the closest choice is repo. The token then has several unneeded abilities:
- Too many repositories. It works on every repository you can access, including private ones in organizations you join later.
- Too many actions. It can push code, change settings, and delete content, not just read issues.
- Long lifetime. Classic tokens can be created with no expiration date, so a leaked token may stay valid for years.
- Tied to a person. The token acts as you. If you leave a team, any automation using your token breaks, or keeps running with your access.
If such a token leaks through a log, a public commit, or a compromised laptop, the attacker gets everything the scope allows. The damage depends on the scope, not on what your script actually needed.
Common mistakes
- Ticking
repobecause it "just works". Pick the smallest scope that passes your test, or move to a fine-grained token. - Using one token for many scripts. One leak then exposes everything. Create one token per purpose so you can revoke each independently.
- Setting "no expiration". Prefer a short expiry and rotate the token.
- Reusing a personal token in CI. Pipelines should use a credential that is not tied to one person.
Where this leads
Classic tokens are not forbidden, and some older tools still require them. But their design makes least privilege hard: scopes are coarse, they apply to all your repositories, and they have no built-in expiry. The next chapters show fine-grained tokens, which limit a token to chosen repositories and specific permissions.
Exercise
A script needs to read issues in one private repository. You create a classic token with the repo scope. Name two things that token can do that the script does not need.
Show answer
Any two of: access other repositories you can reach, including private organization repositories; push code; change repository settings; delete content. The scope applies to everything your account can access, not only the one repository.
Exercise
How can you check which scopes a classic token has, using the API?
Show answer
Make any authenticated request, for example curl -I -H "Authorization: Bearer $GITHUB_TOKEN" https://api.github.com/user, and read the X-OAuth-Scopes response header.