Sub topic 8
Limiting token permissions and handling secrets
This chapter assumes you can read a workflow file (events, jobs, steps), know how uses and with work, and have pinned an action to a tag or SHA. Here you will limit what a third-party action can do with the GITHUB_TOKEN, and pass secrets to actions only where they are needed.
Why this matters
An action runs inside your job. Anything your job can reach, the action can reach: the GITHUB_TOKEN, secrets you pass in, and files in the workspace. You cannot make an action harmless by reading its README, so you shrink what is available to it.
What the GITHUB_TOKEN is
GitHub creates a temporary token for each workflow job and expires it when the job ends. Actions can read it through the github.token context or the secrets.GITHUB_TOKEN expression. Its default permissions depend on repository and organization settings. In older repositories the default can be read and write access to most scopes.
Set permissions explicitly so the result does not depend on a setting you cannot see in the file.
Setting permissions
Use the permissions key. Put it at the top level to apply to every job, or inside a job to override the top level for that job.
name: CI
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Tests run with read-only repository access"
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- run: echo "Only this job may write to pull requests"Two rules to remember:
- When you list any permission, every scope you do not list becomes
none. Listingcontents: readremoves access to issues, pull requests, packages, and the rest. permissions: {}removes all access. Use it for jobs that only run local commands.
| Value | Effect |
|---|---|
| `read` | The token can read that scope |
| `write` | The token can read and write that scope |
| `none` | The token has no access to that scope |
Start with contents: read at the top level, then add the narrowest write permission to the single job that needs it. If an action fails with a Resource not accessible by integration error, the usual cause is a missing permission. Check the action's documentation for the scope it needs.
Passing secrets to actions
A secret is an encrypted value stored in the repository, environment, or organization settings. Read it with the secrets context. An action receives a secret only if you pass it.
steps:
- uses: actions/checkout@v4
- name: Deploy
uses: example-org/deploy-action@v2
with:
api-key: ${{ secrets.DEPLOY_API_KEY }}Pass a secret to the single step that needs it. Avoid setting it in a job-level or workflow-level env block, because then every step, including every third-party action in that job, can read it.
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Avoid: this exposes the secret to every step below.
# env:
# DEPLOY_API_KEY: ${{ secrets.DEPLOY_API_KEY }}
- run: ./scripts/build.sh
- run: ./scripts/deploy.sh
env:
DEPLOY_API_KEY: ${{ secrets.DEPLOY_API_KEY }}How secrets are protected, and where that stops
GitHub masks secret values in logs, replacing them with ***. This is a convenience, not a security boundary. A malicious or careless action can transform a value (for example by encoding it) so the mask no longer matches, or send it to an external server.
Practical habits:
- Never print secrets, even for debugging.
- Do not build secrets from structured data such as JSON blobs, because individual fields are not masked.
- Prefer short-lived credentials, such as OpenID Connect (OIDC) to a cloud provider, over long-lived keys stored as secrets. OIDC needs the
id-token: writepermission, which you grant only to the deploying job. - Use environment secrets with required reviewers for production credentials, so a job cannot read them without approval.
Pull requests from forks
Workflows triggered by pull_request from a fork do not receive your secrets, and the GITHUB_TOKEN is read-only. Switching to pull_request_target gives the workflow secrets and a write token. Do not check out and run the fork's code in that workflow, because untrusted code would then run with your credentials.
Putting it together
Exercise
A workflow sets permissions: { contents: read } at the top level. A job in it uses an action that comments on pull requests and fails with "Resource not accessible by integration". What change fixes it with the least access, and why is setting DEPLOY_KEY in the workflow-level env a poor choice for a deploy step?
Show answer
Add permissions: { contents: read, pull-requests: write } to that job only, leaving the top-level setting unchanged. A workflow-level env exposes DEPLOY_KEY to every step in every job, including third-party actions that do not need it. Set it with env or with on the one deploy step instead.