Lorixen.
EN

Pull requests from forks: keeping GitHub Actions secrets safe

Short answer: GitHub deliberately keeps your repository's secrets away from workflows triggered by pull requests from forks, which is why builds that need a token fail on outside contributions. The tempting fix, switching to the pull_request_target trigger, runs with your secrets and write access, and if the workflow then checks out and runs the contributor's code, a malicious pull request can steal those secrets. Keep untrusted code in an unprivileged pull_request workflow, and do anything that needs secrets or write access in a separate workflow that never runs the contributor's code.

Why secrets are blank on fork pull requests

When someone forks your public repository and opens a pull request, the code in that pull request is written by someone you don't control. If GitHub handed that code your deploy keys, package registry tokens or cloud credentials, anyone could open a pull request that prints them to the log or sends them elsewhere.

So workflows triggered by the ordinary pull_request event from a fork run without your repository secrets and with a read-only token. Tests that only need the code work fine. Steps that publish a preview, post a comment, upload coverage to a service with a token, or call a paid API fail, and that's where teams go looking for a workaround.

Why pull_request_target is risky

pull_request_target runs in the context of your base repository, not the fork. GitHub Security Lab explains it was introduced so workflows can do safe, privileged chores such as labelling a pull request or leaving a comment, without building or running its contents.

The danger is combining it with an explicit checkout of the pull request's code. GitHub Security Lab's summary is blunt: combining the pull_request_target trigger with an explicit checkout of an untrusted pull request "is a dangerous practice that may lead to repository compromise". Once the contributor's code is on disk, anything that runs it, such as npm install with its scripts, a build command or a test runner, executes their code with access to your secrets and a token that may be able to write to your repository.

GitHub's secure-use reference adds that these privileged workflows share the cache of the main branch with other privileged workflows, so a poisoned cache entry can affect later trusted runs.

A safe pattern: split the work

GitHub Security Lab recommends separating the untrusted part from the privileged part.

Workflow 1, on pull_request. Check out the pull request, build it and run tests with no secrets and a read-only token. Save anything you need later, such as test results or the pull request number, as an artifact.

Workflow 2, on workflow_run. Triggered when workflow 1 finishes, it runs in your repository's privileged context. It downloads the artifact and uses it to post a comment or update a status. It never checks out or executes the pull request's code, and it treats the artifact's contents as untrusted data: validate a pull request number is really a number before using it, and never paste artifact contents into a shell command.

If you only need to label or comment based on metadata, such as which files changed, pull_request_target without checking out the code is acceptable for that narrow job.

Lock down the token everywhere

Whatever triggers you use, limit what each workflow can do. GitHub's reference recommends granting the GITHUB_TOKEN the minimum permissions required and setting the default to read-only for repository contents. Add a permissions: block to each workflow and grant write access only to the specific jobs that need it, such as pull-requests: write for a commenting job.

Other measures worth having:

  • Require approval before outside contributors' workflows run, especially first-time contributors. This is in the repository's Actions settings.
  • Put deploy secrets in environments with required reviewers, so a job can't use production credentials without a person approving it.
  • Pin third-party actions to a full commit SHA rather than a moving tag.
  • Don't interpolate pull request titles, branch names or bodies directly into run: scripts. Pass them through environment variables, because they're attacker-controlled text.

Checking your existing workflows

  1. Search your .github/workflows folder for pull_request_target and workflow_run.
  2. For each match, look for actions/checkout with a ref pointing at the pull request's head, or any step that installs, builds or runs code from it.
  3. Check whether those workflows have access to secrets or a token with write permissions.
  4. Add explicit permissions: blocks if they're missing.
  5. If you find a risky workflow that has run on outside pull requests, rotate the secrets it could reach. You can't prove they weren't read.

When preview deployments need secrets

Preview deployments for every pull request are where this comes up most. Options include deploying previews only for branches inside your own repository, requiring a maintainer to add a label before a privileged preview job runs (and re-checking after new commits), or using a hosting provider's own integration that builds pull requests in its isolated environment. Each trades convenience for safety; pick deliberately.

A worked example

An illustrative open-source WordPress plugin maintained by a small US studio wanted coverage comments on contributors' pull requests. A contributor suggested switching the test workflow to pull_request_target so the coverage token would work. Before merging, the studio split it: tests run on pull_request and upload the coverage report as an artifact, and a workflow_run job with pull-requests: write posts the comment. Contributors still get comments, and their code never runs alongside the studio's secrets.

FAQ

Is pull_request_target always unsafe?

No. It's designed for privileged tasks that don't run the pull request's code. The risk comes from checking out and executing that code.

Do private repositories have the same issue?

Forks of private repositories are more limited, but the principle holds: treat code from people outside your team as untrusted.

What if a contributor needs a secret to run tests?

Use test credentials with no access to real data, or have a maintainer run those tests after review.

How do I know if a secret was already exposed?

You usually can't be sure. Rotate any secret a risky workflow could reach.

If your repository runs automation on outside pull requests, send us the workflow files and we'll check them for exposed secrets and excess permissions.

Sources

WhatsAppBook a call