Sub topic 1
Least-privilege GITHUB_TOKEN permissions in GitHub Actions
Prerequisites
Assume you know basic GitHub Actions syntax: on, jobs, and steps. You should also be comfortable reading YAML and understand repositories, pull requests, and third-party actions.
What the GITHUB_TOKEN is and how it works
GITHUB_TOKEN is a short-lived credential that GitHub creates for each workflow job. It lets steps call the GitHub API or authenticate Git operations. GitHub removes or expires the token when the job ends.
The token is available through ${{ secrets.GITHUB_TOKEN }} or ${{ github.token }}. Its access depends on repository, organization, or enterprise defaults and the workflow's permissions settings. A token does not automatically grant every action available to a human user.
name: ci
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testWhy a broad token is a risk
Every step runs in the job's environment. If a third-party action is compromised, or a script executes untrusted input, it might use the token. A token with write access could modify files, create releases, change issues, or alter pull requests.
Limit the token so a compromised step has fewer useful capabilities. Read-only access is often enough for testing and building.
Setting the default to read-only
Set the default workflow permission in repository settings. Organization and enterprise settings can provide broader defaults for many repositories. Choose read-only when possible, then declare write access only for jobs that need it.
You can inspect and change a repository's default with GitHub CLI:
gh api repos/OWNER/REPO/actions/permissions/workflow
gh api --method PUT repos/OWNER/REPO/actions/permissions/workflow \
-f default_workflow_permissions=read \
-F can_approve_pull_request_reviews=falseThe permissions key and access levels
The permissions key controls named permission scopes. Common scopes include contents, issues, pull-requests, and packages. Each scope accepts read, write, or, in an empty block, no access.
Workflow-level permissions apply to every job. A job can request a narrower or additional set, but it should receive only what its own steps require.
permissions: {}
jobs:
label:
permissions:
contents: read
pull-requests: writeExercise
What permissions should a test job use if it only checks out source code and runs tests?
Show answer
Use contents: read; it does not need write access.