Sub topic 13
Fork approval settings and protected environments
This chapter assumes you can read a GitHub Actions workflow and know how pull_request and pull_request_target differ in token and secret access. It also assumes you know what a fork is. The earlier chapters covered how to remove injection and pwn-request bugs from workflow code. This chapter covers two settings-level controls that add a human decision before risky code gets privileges. They limit damage when a workflow mistake slips through. They do not replace fixing the workflow.
Two gates, two different questions
GitHub offers two separate gates, and they answer different questions.
| Gate | Where you configure it | Question it answers | What it blocks |
|---|---|---|---|
| Fork pull request approval | Repository, organization or enterprise Actions settings | Should this contributor's fork workflow run at all yet? | The whole workflow run, until a maintainer clicks approve |
| Protected environment | Repository Settings, Environments | Should this job get its secrets and deploy rights yet? | One job, until a reviewer approves it |
The first gate is coarse and applies by contributor. The second gate is precise and applies by job. Using both gives you defense in depth.
Fork pull request approval
By default, a workflow triggered by a pull request from a fork does not run automatically for every contributor. You choose how strict this is under Settings > Actions > General > Fork pull request workflows from outside collaborators. The options are:
- Require approval for first-time contributors who are new to GitHub.
- Require approval for first-time contributors.
- Require approval for all outside collaborators.
Option 3 is the strictest and suits repositories where workflows touch anything sensitive. A maintainer sees the pending run on the pull request and chooses Approve and run after reading the diff. The approval covers that run. A later push by the same contributor can require approval again, depending on the setting and contributor history.
Private repositories have extra switches in the same area:
- Run workflows from fork pull requests: if off, fork pull requests trigger nothing.
- Send write tokens to workflows from fork pull requests: leave this off. It would give fork code a write-capable
GITHUB_TOKEN. - Send secrets to workflows from fork pull requests: leave this off. It would hand repository secrets to fork code.
- Require approval for fork pull request workflows: adds the approval step.
Organization and enterprise owners can set a minimum policy. A repository can then be stricter than that policy but not looser.
What approval does not protect
The approval prompt is about the pull_request trigger, where fork code runs with a restricted token. Do not treat it as protection for a vulnerable pull_request_target workflow. That trigger runs the workflow file from the base branch with a privileged context. The risk is not that the contributor is unknown. The risk is that your workflow runs untrusted data or code with privileges. A maintainer approving a run reads a diff, and an injection payload can sit in a PR title, a branch name or a comment, none of which appear in the diff. Fix the workflow first. Treat approval as a second layer.
Approval also has a human weakness. Reviewers get tired, and a malicious change can look like a harmless dependency bump. A one-line change to an install script in package.json is enough. Read what the run will execute, not just what the pull request says it does.
Protected environments
An environment is a named deployment target such as production or pr-preview. A job opts in with the environment: key. Environments can have protection rules:
- Required reviewers: up to six people or teams. One approval from any of them releases the job.
- Prevent self-review: the person who triggered the run cannot approve it.
- Wait timer: a delay before the job starts, which gives time to cancel.
- Deployment branches and tags: only listed refs may deploy to this environment.
Environments also hold their own secrets and variables. This is the important security property. Environment secrets are released to a job only after its protection rules pass. A job without environment: cannot read them at all. Repository-level secrets, by contrast, are available to any job in the repository that references them.
Moving high-value secrets, such as cloud deploy credentials, from repository secrets to environment secrets means a compromised unprivileged job simply has nothing to steal.
Some plan limits apply to private repositories. Required reviewers and some other rules need a paid plan. Check your plan before you design around them.
A gated pull_request_target job
Sometimes you really do need secrets for a fork pull request, for example to publish a preview build. The pattern is to put the privileged job behind an environment with required reviewers, and to pin the checkout to the exact commit the reviewer saw.
name: preview
on:
pull_request_target:
types: [opened, synchronize]
permissions:
contents: read
jobs:
preview:
runs-on: ubuntu-latest
environment: pr-preview
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- env:
PREVIEW_TOKEN: ${{ secrets.PREVIEW_TOKEN }}
run: ./scripts/deploy-preview.shHere is what each part does:
environment: pr-previewpauses the job. ThePREVIEW_TOKENenvironment secret is not released until a reviewer approves.ref: ${{ github.event.pull_request.head.sha }}pins the code to a commit hash, not a branch name. The branch can move after approval, but a hash cannot. This closes a time-of-check to time-of-use gap. Without it, an attacker could push a new commit between approval and checkout.persist-credentials: falsestops the checkout step from leaving the token in the repository's git configuration, where later steps could read it.contents: readkeeps theGITHUB_TOKENminimal.
This is still the pwn-request shape from the earlier chapter: fork code runs where a secret exists. The environment does not make that code safe. It makes sure a person decided to run it. Approve only after reading the files the job will execute, including deploy-preview.sh, package lifecycle scripts and any build configuration the fork controls.
If you can avoid the pattern, do. Building in an unprivileged pull_request workflow and deploying from a separate privileged workflow, as covered in the safe-patterns chapter, removes the need to run fork code with a secret.
Why branch restrictions do not help here
The Deployment branches and tags rule restricts which ref may deploy. It is useful for push-based deployments, where you can allow only main. It gives little protection for pull_request_target. That event runs in the context of the base branch, so the job's ref is the base branch, and a rule allowing main passes. For pull-request-triggered jobs, rely on required reviewers, not branch rules.
A related point is environment names. A fork cannot change the workflow file under pull_request_target, because GitHub takes it from the base branch. That is why the environment: line is trustworthy there. Under plain pull_request, a fork pull request does change the workflow file, but that run has no secrets, so a renamed environment gains nothing.
How the gates combine
The diagram shows a fork pull request moving through both gates.
Common mistakes
- Putting deploy secrets at repository level. Any job can request them. Move them to an environment.
- Environments with no reviewers. An environment with no protection rules gates nothing. Secrets are released immediately.
- Letting the author approve their own run. Turn on prevent self-review for sensitive environments.
- Approving on the PR description. Read the code and the scripts the job executes.
- Checking out a branch name after approval. Use the commit SHA from the event payload.
- Treating approval as a fix. An injectable
runstep stays injectable whether or not a person clicked approve.
Checklist
- Set fork approval to require approval for all outside collaborators on repositories with sensitive workflows.
- Leave "send write tokens" and "send secrets" to fork pull requests turned off.
- Move deploy credentials into environment secrets.
- Add required reviewers and prevent self-review to each environment that holds them.
- In any gated job that checks out fork code, pin
reftohead.shaand setpersist-credentials: false. - Keep
permissionsexplicit and minimal in every workflow.
Exercise
A workflow uses pull_request_target and runs a job that checks out github.event.pull_request.head.ref and deploys with a repository secret. The team enables "Require approval for all outside collaborators". They also add an environment named preview with required reviewers, but the job has no environment: key. Is the secret now protected? Name two changes that would fix the design.
Show answer
No. The secret is a repository secret and the job does not name the environment, so the reviewer rule never applies. The fork approval setting is aimed at the pull_request trigger and is not a safe control for this workflow. Fixes: move the secret into the preview environment and add environment: preview to the job so a reviewer must approve before the secret is released, and pin the checkout to github.event.pull_request.head.sha instead of the branch name so the approved commit is the one that runs. Better still, split the build into an unprivileged pull_request workflow and deploy from a separate privileged workflow.