All guides

GITHUB_TOKEN permissions: set them for every job

Every job in GitHub Actions gets a token, and every action in the job can use it. What the token may do depends on a default setting and on the permissions key in the workflow file.

Hands typing on a laptop that shows code

The automatic token

At the start of each job, GitHub creates a GITHUB_TOKEN. It is an installation access token for the GitHub App that GitHub installs on a repository when Actions is enabled. Its permissions are limited to the repository that contains the workflow. It expires when the job finishes or after its maximum lifetime.

You pass it to a step as ${{ secrets.GITHUB_TOKEN }}. An action can also read it from the github.token context when the workflow never passed it. GitHub's documentation therefore says to limit the token's permissions so that actions have only the minimum access they require.

The default setting

A repository and an organization each have a setting named Workflow permissions, under Settings, Actions, General. It has two values:

  • Restricted: read access for contents and packages only.
  • Permissive: read and write access for all permissions.

New organizations start restricted, and so do new repositories in a personal account. A repository in an organization inherits the organization's value. When the organization chooses restricted, its repositories cannot choose permissive.

The same page holds a setting named Allow GitHub Actions to create and approve pull requests. In a new organization, workflows are not allowed to do either.

Workflow permissions sets the default. Write access to a repository is enough to change the token's permissions: edit the permissions key in a workflow file.

The permissions key

permissions can sit at the top level of a workflow, where it applies to every job, or inside one job. Each permission takes read, write or none, and write includes read. The names include actions, checks, contents, deployments, id-token, issues, packages, pages, pull-requests, security-events and statuses.

Two documented rules follow:

  • If you specify the access for any permission, all of those you did not specify are set to none.
  • The same holds inside a job. In GitHub's example, a job grants write access for issues and pull-requests, and all other permissions have no access.

permissions: {} removes every permission. read-all and write-all set all of them at once.

A least-privilege workflow

YAML

name: CI
on:
  pull_request:

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: make test

  comment:
    needs: test
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - run: gh pr comment "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --body "Tests passed."
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.number }}

make test stands for your own test command, and the comment text is yours to choose. The test job checks out the repository with a token that can only read contents. The comment job has write access to pull requests and nothing else, and it checks out no code.

Pull requests from forks

When a pull_request event comes from a fork, GitHub changes every write permission to read, whatever the workflow asks for. It passes no secrets other than the token. Workflows triggered by Dependabot pull requests are treated the same way. Private repositories have a setting, Send write tokens to workflows from pull requests, that lifts the limit.

pull_request_target behaves differently. It runs in the context of the base repository's default branch, and its token can write even when a public fork triggered it. GitHub's documentation says to stay away from this event when the job has to build or run the pull request's code. actions/checkout@v7 refuses to check out fork code there unless you set allow-unsafe-pr-checkout: true.

The id-token permission for OIDC

id-token: write lets a job request an OpenID Connect token from GitHub, which a cloud provider exchanges for short-lived credentials. It grants no write access to any resource, and it accepts only write or none.

Naming it sets every other permission to none. Add contents: read next to it when the job checks out code:

YAML

permissions:
  id-token: write
  contents: read

What to check in each repository

  1. In Settings, Actions, General, Workflow permissions is restricted, and the pull request checkbox is off unless a workflow needs it.
  2. Every workflow file has a top-level permissions key.
  3. No workflow uses write-all.
  4. Each job that writes names the permission it needs inside the job.
  5. Someone has read every pull_request_target workflow and knows what it checks out.
  6. A job that needs another repository uses a GitHub App token. The GITHUB_TOKEN is limited to the repository that contains the workflow.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides