Skip to content

DevOps Operations / Continuous integration · Step 7 of 7

Lesson: Workflow PermissionsSwitch
← DevOps Operations

Delivery and containers

Workflow Permissions

Limit what a workflow's token can do and handle secrets and third-party actions safely.

A workflow runs code automatically, sometimes code you did not write. If that code can change your repository, a mistake or an attack spreads quickly. The rule is least privilege: grant only what a job needs.

The GITHUB_TOKEN

GitHub Actions creates a token called GITHUB_TOKEN for each job. It lets steps call GitHub, and it expires when the job finishes (or reaches its maximum lifetime). Its access is limited to the repository that contains the workflow. The default level depends on repository and organisation settings, so do not rely on it.

The permissions key

State what you need in the workflow file:

yaml
permissions:
  contents: read

This can sit at the top level or inside one job. When you list any permissions, those you do not list are set to no access (reading repository metadata is always allowed).

Give extra rights only to the job that needs them:

yaml
permissions:
  contents: read
jobs:
  comment:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - run: gh pr comment "$PR" --body "Checks started"
        env:
          GH_TOKEN: ${{ github.token }}
          GH_REPO: ${{ github.repository }}
          PR: ${{ github.event.pull_request.number }}

This job assumes the workflow is triggered by pull_request. A job-level block replaces the top-level one for that job.

Secrets and forks

Store passwords and keys as secrets in the repository settings, and pass them only to the step that needs them. Workflows triggered by pull_request from a fork get a read-only token and, apart from GITHUB_TOKEN, no secrets by default.

pull_request_target runs in the context of the base repository, with access to its secrets. Checking out and running the pull request's code under it lets a stranger run code with your rights. Avoid it unless you understand the risk.

Untrusted input

Titles and branch names are chosen by other people. Do not paste them directly into a script with ${{ }}, because the text becomes part of the script. Pass them as environment variables and quote them:

yaml
      - env:
          TITLE: ${{ github.event.pull_request.title }}
        run: echo "$TITLE"

For third-party actions, a tag such as @v4 can be moved. Pinning to a full-length commit SHA fixes exactly which code runs, though you must then update it yourself.

Try it

Add the permissions block with contents: read to a practice workflow. Open the run log and expand Set up job to see the permissions the token received. Then read the security guidance in the official documentation. For the Linux side of least privilege, try permissions in depth.

Check your understanding

Workflow Permissions

All 4 sections · no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.