AI Hints
CI/CD with GitHub Actions · Lesson 108 of 138

pull_request_target Dangers

Secrets plus strangers.

⏱ 2 min read▶ Video at 2:06:03🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

For pull requests from forks, GitHub is careful. The normal pull request trigger runs without your secrets, and with a read only token. Pull request target is different. It runs with your secrets, so it's powerful, and risky.

pull_request_target Dangers
From the video: pull_request_target Dangers
  • Forks get no secrets
  • pull_request_target does
  • Powerful and risky

The classic mistake

This workflow uses pull request target, so it has secrets and a write token. Then it checks out the pull request's own code. And runs NPM install and test on it. A stranger's package dot JSON can run any script during install, with your secrets.

.github/workflows/pr.ymlyaml
on: pull_request_target   # runs with secrets!
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - run: npm install && npm test   # the stranger's code

Which trigger?

Use the normal pull request trigger for building and testing code from pull requests. It's safe by default. Use pull request target only for jobs that don't run the pull request's code, like adding labels, or posting a comment.

pull_request

  • Build and test PR code
  • No secrets for forks
  • Read-only token

pull_request_target

  • Labels, comments
  • Has secrets
  • Never run PR code

Summary

Pull request target has secrets, so never check out and run untrusted code with it. Next, giving every job only the permissions it needs.

Key takeaways
  • pull_request: safe
  • _target: has secrets
  • Never run fork code
Next up: Least Privilege — only what you need