Sub topic 10
Forks, Dependabot, and event-specific behavior
This chapter assumes you can write a permissions block at workflow and job level. The token you get does not depend only on that block. The event that triggered the run, and who triggered it, can lower or raise what the token can do.
Fork pull requests
A fork is a copy of your repository owned by someone else. When a pull_request run comes from a fork:
- The
GITHUB_TOKENis read-only, even if your workflow asks forcontents: writeorpull-requests: write. - Repository secrets are not passed to the run, except
GITHUB_TOKENitself.
GitHub does this because the workflow file and the code come from an outsider. Letting that code write to your repository would be unsafe.
Private repositories can change this. An administrator can enable "Send write tokens to workflows from pull requests" in the repository settings. Leave it off unless you trust every fork author.
Dependabot
Dependabot is GitHub's bot that opens pull requests to update dependencies. Its branches live in your repository, but GitHub treats the runs it triggers like fork runs:
- The token is read-only by default.
- Secrets come from the separate Dependabot secrets store, not from Actions secrets.
Unlike a fork, a Dependabot run can raise the token with a permissions block. This is how auto-merge workflows work:
name: dependabot-automerge
on: pull_request
permissions:
contents: read
jobs:
merge:
if: github.actor == 'dependabot[bot]'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: dependabot/fetch-metadata@v2
id: meta
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
- if: steps.meta.outputs.update-type == 'version-update:semver-patch'
run: gh pr merge --auto --squash "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}The if condition keeps the elevated job from running for any other author. Write access is requested only in the one job that needs it.
Which event gives which token
| Trigger | Token | Secrets |
|---|---|---|
| `pull_request` from a fork | Read-only, cannot be raised | None |
| `pull_request` from Dependabot | Read-only by default, can be raised | Dependabot secrets |
| `pull_request_target` | Your configured permissions | Available |
| `push`, `schedule`, `workflow_dispatch` | Your configured permissions | Available |
pull_request_target runs the workflow from the base branch, so it keeps a writable token and secrets. That is why it is the dangerous case. A later chapter covers it.
Practical rules
- Design
pull_requestworkflows so they pass with a read-only token. Fork contributors then get the same checks as everyone else. - If a job must comment or label on fork PRs, use a separate workflow triggered by
workflow_run. Keep it free of untrusted code. - Add
if: github.actor == 'dependabot[bot]'to elevated Dependabot jobs.
Exercise
A workflow triggered by pull_request declares pull-requests: write and posts a comment. It works for your team's branches but fails with "Resource not accessible by integration" on a contributor's fork PR. Why, and what is a safe fix?
Show answer
Runs from forks get a read-only token, and the permissions block cannot raise it. A safe fix is to upload the comment text as an artifact in the pull_request job. A separate workflow_run workflow then downloads it and posts the comment. That workflow runs in the base repository and must not execute the fork's code.