BM: Baca dalam Bahasa Melayu
World C4: Pipeline Shields
pull_request_target and forks
You will learn: why fork PRs get no secrets, and the trap in pull_request_target.
Checked with GitHub Actions 2026-10 on 31 Oct 2026
The problem
A stranger forks your shop repo and opens a PR. Your preview job needs your Cloudflare token. Should their code get it?
Pick one answer.
Try it
Run both events. Find the one where the preview stops.
Try the events. Find the one asked for.
Workflow file .github/workflows/preview.yml
name: Preview on: pull_request: permissions: contents: read jobs: preview: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - run: npm ci - run: npx wrangler deploy --env preview env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
The line
Pull request · feature → main
Hint
Try the pull request from a fork.Watch
Some teams want previews for forks too. So they switch to pull_request_target: A PR trigger that runs with your secrets and a write key, even for a stranger's fork. Never run their code in it.. It runs with your secrets and a write key, even for forks. Sort the two triggers.
Drag a card into a box. Or tap the card, then tap the box.
Predict
This preview switches to pull_request_target. It also checks out the PR’s own code. A stranger’s fork PR arrives. What happens?
Pick one answer.
Watch
Pick an event. Press Run and watch the line.
A stranger's PR on pull_request_target. Every station turns green, with your token in the job.
Workflow file .github/workflows/preview.yml
name: Preview on: pull_request_target: permissions: contents: read jobs: preview: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 with: ref: ${{ github.event.pull_request.head.sha }} - run: npm ci - run: npx wrangler deploy --env preview env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
The line
Pull request from a fork · stranger:feature → main
Green! The stranger’s npm ci ran with your token in reach. One sneaky install script could copy it.
Try it
Tap the two lines that turn this workflow into a trap.
Tap every risky line. Each one shows the attack and the fix.
Workflow file .github/workflows/welcome.yml
Hint
Which code does the checkout fetch? And how many keys does the job get?Check
Fine on pull_request_target, or a trap? Swipe each job.
Swipe right for good, left for needs work. Or use the buttons.
Add a label to a fork PR, without checking out its code
You did it!
You can now keep a stranger’s PR away from your keys.
Go deeper (optional)
Safer pattern: test fork code with pull_request. Need secrets afterwards? Use a separate workflow on workflow_run, and treat its artifacts as untrusted. First-time contributors may need a maintainer to approve their runs. Real rep: search your repos for pull_request_target and check each one.
Bahasa Melayu
Orang asing fork (buat salinan) repo kedai anda dan buka PR. Job pratonton anda perlukan token Cloudflare anda. Patutkah kod mereka dapat token itu?
Jalankan kedua-dua event. Cari yang mana pratonton berhenti.
Sesetengah pasukan mahu pratonton untuk fork juga. Jadi mereka tukar ke pull_request_target. Ia jalan dengan secret anda dan kunci tulis, walaupun untuk fork. Susun dua pencetus ini.
Pratonton ini tukar ke pull_request_target. Ia juga check out kod PR itu sendiri. PR fork orang asing sampai. Apa jadi?
Hijau! npm ci orang asing itu jalan dengan token anda dalam jangkauan. Satu skrip pemasangan yang licik boleh menyalinnya.
Tekan dua baris yang jadikan workflow ini perangkap.
Okey pada pull_request_target, atau perangkap? Leret setiap job.
Kini anda boleh jauhkan PR orang asing daripada kunci anda.
You finished this before. Practice is always welcome!
Official docs: Events: pull_request and pull_request_target · Secure use reference
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?