Sub topic 10
Forks, Dependabot, and event-specific behavior
This chapter assumes you can write a basic workflow (on, jobs, steps), that you know the permissions key from earlier chapters, and that you know how workflow-level and job-level permissions combine. Here you will see that the token your job receives depends on who triggered the run and which event started it. A permissions block states what you ask for. GitHub may then reduce it.
Why the event matters
The GITHUB_TOKEN is created for each job, and its final permissions come from three inputs:
- The default workflow permissions in repository, organization, or enterprise settings.
- The
permissionskey in the workflow or job. - Event-specific restrictions that GitHub applies last.
The third input can only lower access. A permissions block can never raise the token above what the event allows. If you request contents: write in a run that GitHub limits to read-only, the token is still read-only.
Pull requests from forks
A fork is a copy of a repository owned by another account. A contributor pushes to their fork and opens a pull request against yours. If your workflow uses the pull_request event, GitHub treats the code as untrusted and restricts the run:
- The
GITHUB_TOKENis read-only, even if your workflow asks for write scopes. - Repository secrets are not passed to the run, except
GITHUB_TOKEN. - The workflow file used is the one from the merge commit, which contains the contributor's changes.
This protects you. If a fork could get a write token, anyone could open a pull request that edits the workflow to push code, create releases, or read secrets.
The consequence is a common surprise. A workflow that labels pull requests, posts comments, or uploads results works for pull requests from branches in your own repository. The same workflow fails for fork pull requests with Resource not accessible by integration.
Consider this workflow:
name: label
on: pull_request
permissions:
contents: read
pull-requests: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v5For a same-repository pull request, the token has pull-requests: write and labeling works. For a fork pull request, the token has pull-requests: read, so the labeling call is rejected.
Options for fork pull requests
You have three reasonable choices, ordered from safest to riskiest.
- Accept read-only behavior. Run tests and linters on
pull_request, and skip steps that write. This is the right default for CI. - Split the workflow. Run untrusted work in a
pull_requestworkflow with no privileges. Save the results as an artifact. A second workflow, triggered byworkflow_run, downloads the artifact and performs the privileged action, such as posting a comment. The second workflow runs in the context of the default branch, so it has the write token, but it must treat the artifact contents as untrusted data. - Use
pull_request_target. This event runs with the workflow from the base branch and gives a token with the permissions you request. It is powerful and easy to misuse, so it gets its own chapter on untrusted code.
Dependabot
Dependabot is GitHub's service that opens pull requests to update dependencies. When Dependabot is the actor for a workflow run, GitHub treats it much like a fork:
- For events such as
pull_request,pull_request_review, andpushon Dependabot branches, theGITHUB_TOKENis read-only by default. - Repository Actions secrets are not available. Dependabot has its own separate secret store, found under the repository's Secrets and variables settings in the Dependabot section.
The reason is that Dependabot pull requests contain third-party dependency changes. A malicious package version should not be able to run with your secrets in the same job that installs it.
This breaks workflows that auto-merge, label, or comment on Dependabot pull requests. There are two approaches:
- Read the secrets you need from the Dependabot secrets store. Workflows triggered by Dependabot can read those, and they are not exposed to forks.
- Request write permissions only in a job that checks
github.actor == 'dependabot[bot]'. Be aware that setting the permission in the file does not always restore write access for events GitHub restricts. If the run is still read-only, use the split pattern orpull_request_targetcarefully.
A narrowly scoped job for approving and auto-merging Dependabot updates looks like this:
name: dependabot-automerge
on: pull_request_target
permissions: {}
jobs:
automerge:
if: github.actor == 'dependabot[bot]'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Enable auto-merge
run: gh pr merge --auto --squash "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}Notice what this job does not do. It never checks out the pull request's code and never runs anything from it. Only the gh command runs, and the pull request URL reaches the shell through an environment variable instead of being pasted into the script text. Keep to this pattern whenever a privileged job runs on a pull request event.
Event-by-event summary
The table lists the behavior for common events. "As requested" means the token matches your permissions block, limited by repository defaults and organization policy.
| Event | Token permissions | Secrets available | Notes |
|---|---|---|---|
| `push` to your branch | As requested | Yes | Runs the workflow from that branch. |
| `pull_request`, same repository | As requested | Yes | Runs the merge-commit workflow. |
| `pull_request`, from a fork | Read-only | No | Write scopes are ignored. |
| `pull_request`, by Dependabot | Read-only by default | Dependabot secrets only | Same limits as a fork. |
| `pull_request_target` | As requested | Yes | Uses the base branch workflow. Dangerous with untrusted checkout. |
| `workflow_run` | As requested | Yes | Runs on the default branch. Treat artifacts as untrusted. |
| `schedule`, `workflow_dispatch` | As requested | Yes | Run on the default branch (schedule) or the chosen ref. |
Two further details can catch you out:
issue_commentandissuesevents run from the default branch and are not restricted like fork pull requests. Anyone who can comment can trigger them, so check the commenter's association before doing privileged work.- Organization and enterprise policy. An owner can stop forks from receiving write tokens or from running workflows without approval. Settings for first-time contributors may require a maintainer to approve a run before it starts.
Checking the effective permissions
Do not guess what the token has. Each job log begins with a Set up job step. Expand GITHUB_TOKEN Permissions and you will see the list the job actually received. Compare it with your permissions block. If they differ for a fork or Dependabot run, an event restriction explains it.
You can also test the token directly from a step:
name: show-permissions
on: pull_request
permissions:
contents: read
pull-requests: write
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Try a write call
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
gh api --method POST "repos/${{ github.repository }}/issues/${PR_NUMBER}/labels" \
-f 'labels[]=ci-check' || echo "Write was refused; token is read-only for this run."Open one pull request from a branch in the repository and one from a fork. The first call succeeds and the second prints the fallback message.
Common mistakes
- Adding
pull-requests: writeto fix a fork failure. The token stays read-only and the error remains. - Switching a workflow to
pull_request_targetto get write access, then checking out the pull request head and running its code. This gives untrusted code a privileged token and secrets. - Putting secrets in Actions secrets and expecting Dependabot runs to see them. Copy them to the Dependabot store.
- Testing only with pull requests from your own branches, so fork behavior is discovered by a contributor.
Exercise
A workflow triggered by pull_request requests pull-requests: write to post a coverage comment. It works for maintainers but fails with "Resource not accessible by integration" on pull requests from forks. Why does the failure happen, and what is a safe design that still posts the comment?
Show answer
For fork pull requests, GitHub gives the pull_request event a read-only token and withholds secrets, so the permissions block cannot raise it. A safe design splits the work. The pull_request workflow runs tests with read-only access and uploads the coverage summary as an artifact. A second workflow triggered by workflow_run runs on the default branch with pull-requests: write, downloads the artifact, validates its contents as untrusted data, and posts the comment. It never checks out or executes the contributor's code.
Exercise
Dependabot opens a pull request, and your workflow needs a registry token stored in repository Actions secrets. The secret is empty during the run. What is the cause and the fix?
Show answer
Runs triggered by Dependabot do not receive repository Actions secrets, in the same way that fork runs do not. The fix is to add the secret to the Dependabot secrets store in the repository settings, where Dependabot-triggered workflows can read it.