GitHub Actions secret is empty on pull requests: 6 causes, and why pull_request_target is the unsafe fix

2026-11-28来源 dev.toEnglish

Your GitHub Actions workflow works on your branch, then fails on a pull request with an empty value:

Error: Input required and not supplied: token or an API key that is just blank.

Why secrets are empty

  1. The PR comes from a fork. GitHub does not pass repository secrets to workflows triggered by pull requests from forks. This is deliberate.
  2. Secret name typo. secrets.SUPABASE_KEY and secrets.SUPABASE_ANON_KEY are different. A missing secret is an empty string, not an error.
  3. Wrong scope. The secret is set on an environment, but the job does not declare environment:. Or it is set on another repo.
  4. Dependabot. Dependabot PRs use a separate secrets store.
  5. Reusable workflow. Secrets must be passed in with secrets: or secrets: inherit.
  6. Used in an if: condition. Secrets cannot be read directly there. Map to an env var first.

The unsafe fix

The tempting fix for cause 1 is to switch the trigger to pull_request_target, which does give secrets to the run. If that workflow then checks out and runs code from the PR, a stranger's pull request can run code with your secrets and a write token. This is a well known way repository secrets get stolen.

Other unsafe fixes:

  • Printing the secret with echo to see if it is set. Logs can be read, and masking is not perfect once a value is transformed.
  • Hardcoding the key in the workflow file or committing a .env.
  • Passing the secret into a build that outputs it into your public JavaScript bundle.

The safe pattern

  • Keep pull_request for untrusted code. Run tests that need no secrets.
  • Run secret-using steps only on pushes to your protected branch, or behind a required-reviewer environment.
  • Give the workflow the least permissions: set permissions: explicitly.
  • Rotate any key that was ever echoed or committed.

An outside view

ProofForge at proofforge.live runs a free public-side scan of AI-built apps. It never signs in, so it checks what a visitor can see on the live site, such as secret-shaped strings in the shipped bundle, which is where a leaky build step ends up. It cannot see your workflow files or repository settings.

原文:https://www.a8q9.cn/en/article/github-actions-secret-is-empty-on-pull-requests-6-causes-and-why-pull-request-ta-36e724.html

相关文章