Sub topic 2
Why a broad token is a risk
This chapter assumes you can read a basic workflow (on, jobs, steps) and that you know workflows often run third-party actions. It explains what an attacker gains from a token with too many permissions.
The token is available to every step
GitHub creates a GITHUB_TOKEN when a job starts and revokes it when the job ends. It expires after 24 hours at most. The short life limits how long a stolen token is useful. It does not limit what the token can do while the job runs.
Every step in the job can use the token. Actions receive it as an input or through the github.token context. The actions/checkout action also stores it in the local git configuration by default. A step you did not write can therefore act as your workflow.
name: build
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: some-org/formatter@v1
- run: make testIn this workflow, some-org/formatter@v1 is third-party code. If its maintainer's account is hijacked, or the v1 tag is moved to a malicious commit, the new code runs inside your job. It can read the token and call the GitHub API with it. Dependencies installed during make test have the same reach.
What the attacker can do
The damage depends on the permissions the token holds. These are examples:
| Permission | What a compromised step can do |
|---|---|
| `contents: write` | Push commits or tags, create releases, change branches that are not protected |
| `pull-requests: write` | Edit or close pull requests, and approve them if the repository allows workflows to do so |
| `issues: write` | Edit issues, post comments containing malicious links |
| `packages: write` | Publish a new version of your container image or package |
| `actions: write` | Cancel runs or delete artifacts and caches |
| `id-token: write` | Request an OIDC token and exchange it for cloud credentials |
Pushing a poisoned package or release is the most serious case. Your users then install the attacker's code, and the attack spreads beyond your repository.
Some limits apply even to a broad token. GITHUB_TOKEN cannot modify files under .github/workflows. It cannot reach other private repositories, and events it creates generally do not start new workflow runs. These limits narrow the attack, but they do not protect your code, releases, or packages.
Why the default makes this worse
Repositories and organizations created before GitHub changed the default can still give the token read and write access to most scopes. If nobody has set a permissions key, a job with no write needs still carries that access. Most jobs only check out code and run tests, so the extra access serves no purpose and only adds risk.
Least privilege answers this by limiting the blast radius. You cannot guarantee that every action you use stays trustworthy. You can ensure that a compromised step in a test job holds a token that can only read.
The next chapters show how to change the default and how to set permissions per workflow and per job.
Exercise
A test job runs actions/checkout and a third-party linter. The repository still uses the old permissive default. Name two harmful things a compromised linter could do with the token, and the one change that would prevent them without breaking the job.
Show answer
It could push commits or tags (contents: write) and publish a package (packages: write). Setting permissions: contents: read on the workflow or job removes the write access, and the job can still check out code and run tests.