Skip to content

Managing secrets in GitHub Actions safely

How GitHub Actions secrets work, where they leak (forks, pull_request_target, logs, third-party actions) and a checklist for every secret scope.

Keysquid team4 min read
On this page
  1. How Actions secrets work
  2. Where Actions secrets leak
  3. Untrusted code in a job that has secrets
  4. Logs
  5. Third-party actions
  6. Over-powered GITHUB_TOKEN
  7. Secrets copied into artifacts and caches
  8. Self-hosted runners
  9. Prefer OIDC to stored cloud keys
  10. Third-party API keys still need rotating
  11. Checklist
  12. FAQ

Store GitHub Actions secrets at the narrowest scope that works (an environment with required reviewers for production), never run untrusted code in a job that has secrets, pin third-party actions to a commit SHA, and prefer OIDC to long-lived cloud keys. Those four habits prevent nearly every CI secret leak we know of. This guide explains how Actions secrets work, where they escape, and what to set up.

How Actions secrets work

GitHub encrypts a secret before it reaches GitHub, using a libsodium sealed box, and it stays encrypted until a workflow uses it (GitHub: secret types). A workflow reads it through the secrets context:

steps:
  - run: ./deploy.sh
    env:
      STRIPE_KEY: ${{ secrets.STRIPE_KEY }}

There are three places to define one, and the choice matters more than anything else in this guide:

ScopeWho can use itUse it for
RepositoryEvery workflow in the repoLow-risk keys used by many jobs
EnvironmentOnly jobs that reference the environmentProduction deploy keys; add required reviewers
OrganizationRepos you selectKeys genuinely shared across repos

If an environment, repository and organization secret share a name, the environment one wins (GitHub: secrets reference). A few limits are worth knowing: a secret is at most 48 KB; secret names can't start with GITHUB_; on GitHub Free, organization secrets aren't available to private repositories; and workflows started by Dependabot don't receive Actions secrets at all, only Dependabot secrets (GitHub: using secrets).

Where Actions secrets leak

Untrusted code in a job that has secrets

GitHub doesn't pass secrets to workflows triggered by a pull request from a fork. The pull_request_target trigger is the exception: it runs in the context of your base branch, with secrets, so it can comment on or label pull requests. If such a workflow checks out the pull request's code and runs it (an install script, a test, a build), the contributor's code runs with your secrets. GitHub's security guidance warns specifically against this pattern (GitHub: secure use reference). Keep pull_request_target workflows to metadata only, and run builds of fork code in pull_request workflows, which have no secrets.

Logs

GitHub redacts secret values that appear in logs, but only exact matches. If a step base64-encodes a key, slices it, or prints a JSON blob that contains it, the transformed value isn't masked. GitHub's advice is to avoid structured data as secret values and to mask any derived value yourself:

echo "::add-mask::$DERIVED_TOKEN"

Better still, never print secrets or the whole environment. Remove set -x, env and printenv from scripts that run with secrets.

Third-party actions

An action you call runs with whatever secrets you pass it, and a tag like @v3 can be moved to point at different code. Pinning to a full commit SHA is GitHub's recommended way to make an action immutable:

- uses: some-org/some-action@8f4b7f84864484a7bf31766abe9204da3cbe65b3  # v3.1.0

Pass each action only the secrets it needs, as inputs, rather than exporting everything as job-level environment variables.

Over-powered GITHUB_TOKEN

Every job gets an automatic GITHUB_TOKEN. Set permissions: at the top of each workflow so it gets only what it needs (often contents: read), which limits what a compromised step can do to your repository.

Secrets copied into artifacts and caches

A build step that writes a config file containing a secret, then uploads the workspace as an artifact or saves it to the cache, has published that secret to everyone who can download artifacts from the repository. The same goes for Docker images built in CI with a key passed as a build argument: build arguments are recorded in the image history. Write secrets to files only in temporary locations outside the upload paths, and use Docker build secrets rather than build arguments.

Self-hosted runners

A self-hosted runner keeps its disk between jobs unless you make it ephemeral. A secret written to disk by one job can be read by the next, including a job from a different repository if the runner is shared across an organization. Use ephemeral runners for anything that handles production secrets, and never attach self-hosted runners to public repositories.

Prefer OIDC to stored cloud keys

The safest secret is one you don't store. For AWS, Google Cloud, Azure and other providers that support OpenID Connect, a workflow can exchange a short-lived GitHub-issued token for temporary cloud credentials, so there is no long-lived access key in your secrets at all. If you still keep AWS keys in Actions, see rotating AWS access keys for how to replace them safely.

Third-party API keys still need rotating

OIDC doesn't help with OpenAI, Stripe, Twilio or most SaaS APIs: those need a real key, stored as a secret. Two problems follow. The same key is usually also in your hosting platform and on developers' laptops, so updating it means editing several places by hand. And because updating it is tedious, it rarely happens.

Keysquid treats GitHub Actions as one sync destination among many. It rotates the key at the provider, writes the new value to the repository or environment secret, updates every other destination at the same time, and revokes the old key after a grace period. Values are encrypted to an agent you run, so neither Keysquid nor your AI tools see them. See which providers rotate automatically.

Checklist

DoWhy
Put production keys in an environment with required reviewersA merged workflow can't use them without approval
Use repository secrets only for low-risk, widely used keysEvery workflow in the repo can read them
Never run fork code in pull_request_target workflowsThat code would run with your secrets
Pin third-party actions to a commit SHATags can be moved to new code
Set permissions: on every workflowLimits the automatic token
Mask derived values with ::add-mask::Redaction only catches exact matches
Use OIDC for cloud providersNo long-lived key to leak
Rotate stored API keys on a scheduleA leaked key stops working

Secrets also leak before they ever reach Actions, when someone commits them. Block API keys before they reach GitHub adds a pre-commit hook and a pull-request check, and our free GitHub secret scanner checks a public repository's history.

Frequently asked questions

Are GitHub Actions secrets available to pull requests from forks?

No. GitHub doesn't pass Actions secrets to workflows triggered by a pull request from a fork. Workflows triggered with pull_request_target do run with secrets, which is why they must never check out and run the fork's code.

Does GitHub hide secrets in workflow logs?

GitHub redacts the values of secrets that are printed to logs, but only exact matches. A secret that is transformed (base64-encoded, split, or embedded in JSON) can slip through. Use ::add-mask:: for derived values and avoid printing secrets at all.

What's the difference between repository, environment and organization secrets?

Repository secrets are available to every workflow in the repo. Environment secrets are only available to jobs that reference that environment, which can require approval before the job runs. Organization secrets can be shared with selected repositories.

How big can a GitHub Actions secret be?

Secrets are limited to 48 KB. GitHub's documentation describes encrypting larger files and storing the passphrase as a secret instead.

Sources

  1. docs.github.com/en/actions/reference/security/secrets
  2. docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets
  3. docs.github.com/en/code-security/reference/secret-security/secret-types
  4. docs.github.com/en/actions/reference/security/secure-use

For AI agents: this article is also available as Markdown.

Managing secrets in GitHub Actions safely · Keysquid