Sub topic 13
Fork approval settings and protected environments
This chapter assumes you know the difference between pull_request and pull_request_target, and how GITHUB_TOKEN permissions work. The earlier chapters on those topics cover both. Here you add two human checkpoints that sit around a workflow: approval before fork code runs, and approval before a job can read secrets.
Why add human gates
Safe workflow design is the main defense. Gates are a second layer. They limit the damage if a workflow has a mistake you missed. They also make an attacker wait for a person to say yes.
Fork pull request approval
In Settings → Actions → General, the section for fork pull request workflows controls when workflows from forks run without a maintainer's click. Public repositories offer these levels:
| Setting | Who needs approval |
|---|---|
| Require approval for first-time contributors who are new to GitHub | Accounts that are new to GitHub and have no prior contributions |
| Require approval for first-time contributors | Anyone who has not had a commit or PR merged in the repository |
| Require approval for all outside collaborators | Everyone who is not a member of the repository's organization or a collaborator |
Pick the strictest level your project can live with. An attacker can build a believable account over time, so the weakest option protects little against a patient attacker.
Two limits matter:
- The approval click runs the workflow as written in the PR. If you approve without reading the diff, the gate did nothing. Look at changes to
.github/workflows, package scripts, and build files before approving. - Approval does not fix an unsafe
pull_request_targetworkflow. That trigger runs the workflow file from the base branch, with secrets available. Do not treat the fork approval setting as protection for a pwn request. Fix the workflow instead.
The same settings page has options for sending write tokens and secrets to fork PR workflows. Leave both off unless you have a specific reason.
Protected environments
An environment is a named deployment target, such as production. You configure it under Settings → Environments. Secrets stored on the environment are released only to jobs that name it, and only after its protection rules pass.
Useful rules:
- Required reviewers: the job pauses until a listed person approves.
- Deployment branches and tags: only matching refs may deploy.
- Environment secrets: keep deploy credentials here, not in repository secrets.
name: deploy
on:
push:
branches: [main]
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
run: ./deploy.shDEPLOY_KEY is defined on the production environment. The job waits for a reviewer. Until then the secret is not exposed to it.
Gotchas
A branch restriction alone is weak against pull_request_target. That trigger runs against the base branch, so the ref often matches your allowed branch. Required reviewers are the rule that actually stops it.
Reviewers must read what will run. If the job checks out PR code, approving it is the same as approving that code with your secrets attached.
Do not put production secrets in repository secrets "for convenience". Any workflow in the repository that passes the other checks can read them, with no environment gate in between.
Exercise
A repository's deploy job uses pull_request_target and environment: production, and the environment only has a "deployment branches: main" rule. Why is this not enough to stop a malicious PR, and what rule would you add?
Show answer
pull_request_target runs in the context of the base branch, so the ref is main and the branch rule passes. Add required reviewers to the environment so a person must approve before secrets are released, and review what the job will run before approving.