Least-privilege secrets and tokens in GitHub Actions

By the CapyShield team · · 5 min read

In short

Least privilege in CI means every credential a job can reach is narrowly scoped, short-lived and gated. In GitHub Actions, that means an explicit read-only permissions block with writes granted per job, OIDC instead of stored cloud keys, and environment secrets behind required reviewers and branch rules. Secrets should also be passed to single steps rather than whole jobs, because install scripts and third-party actions can read the job's environment.

Every CI job runs code you didn't write: actions, package install scripts, test frameworks, build plugins. Any of it can read the job's environment. Least privilege in CI means accepting that and making sure what a job can reach is as small, short-lived and narrowly scoped as possible. In GitHub Actions that comes down to three tools: GITHUB_TOKEN permissions, OpenID Connect instead of stored cloud keys, and environment secrets with protection rules.

What does least privilege mean for CI secrets?

Four questions, asked of every credential a workflow touches:

  • Scope: can it do only what this job needs? A token that runs tests shouldn't be able to push to main or publish a package.
  • Lifetime: does it stop working when the job ends? A stolen credential that expires in an hour is a much smaller problem than a cloud access key that lives for years.
  • Exposure: which jobs and steps can see it? A secret set as a job-level environment variable is visible to every step in that job, including third-party actions and install scripts.
  • Gating: who or what has to approve before it is released? Production credentials shouldn't be available to a workflow triggered by any branch.

Supply chain attacks such as the March 2025 tj-actions/changed-files compromise worked by reading every secret a runner had. You can't prevent every compromised dependency, but you can decide how much it finds.

How do you restrict GITHUB_TOKEN permissions?

Declare a permissions: block in every workflow. When you list any permission, all the ones you don't list are set to none. Start read-only at the top and grant writes per job:

yaml
permissions:
  contents: read

jobs:
  label:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write   # this job only labels PRs
    steps: [...]

permissions: {} disables every permission, which suits jobs that never call the GitHub API. Don't depend on the repository default. GitHub changed it to read-only in February 2023, but only for enterprises, organizations and repositories created after that; existing ones kept a read and write token unless an admin changed the setting.

The explicit block matters even if you never pass the token to an action yourself. GitHub's docs point out that an action can read it through the github.token context regardless. The token also expires when the job finishes, so the risk is what it can do during the job, which is why its permissions are the control that counts.

How does OIDC replace long-lived cloud credentials in CI?

Instead of storing a cloud access key as a repository secret, the job asks GitHub's OIDC provider for a signed token describing itself (repository, branch, environment, workflow) and trades it with your cloud provider for an access token that is valid for that job and then expires. There is nothing long-lived to steal and nothing to rotate.

yaml
permissions:
  id-token: write    # allow this job to request an OIDC token
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: prod
    steps:
      - uses: aws-actions/configure-aws-credentials@<full-commit-sha> # pin it
        with:
          role-to-assume: arn:aws:iam::123456789012:role/deploy
          aws-region: us-east-1

The security comes from the trust policy on the cloud side. Match the token's sub claim as precisely as you can. For a job that uses an environment, GitHub sets it to repo:octo-org/octo-repo:environment:prod, so a policy that accepts exactly that string only lets jobs bound to the prod environment of that repository assume the role. A wildcard such as repo:octo-org/* hands the role to every workflow on every branch in the organization.

Only grant id-token: write to the jobs that need it. With it, any step in the job can mint a token and request the role.

When should you use environment secrets in GitHub Actions?

Use them for anything that deploys or publishes. An environment secret is only available to a job that references the environment, and that job has to pass the environment's protection rules before it starts or reads the secrets. The useful rules are:

  • Required reviewers: up to six people or teams, one of whom must approve the job. Turn on "Prevent self-review" so the person who triggered a run can't approve it.
  • Deployment branches and tags: limit the environment to main or release tags, so a workflow on a feature branch can't reach production credentials at all.
  • Wait timer: a delay before the job proceeds, giving you a window to cancel.

Combined with OIDC, the environment name also ends up in the sub claim, so the cloud role and the GitHub approval rule protect the same thing. Environments and environment secrets are available in public repositories on all current plans; private repositories need GitHub Pro, Team or Enterprise.

How do you keep CI secrets away from install scripts and third-party tools?

Give each secret to the one step that needs it, not the whole job. A secret in a step's env: isn't in the environment of the npm ci step before it. Set persist-credentials: false on actions/checkout so the token isn't left in git configuration for later steps to read. And assume the install step is the riskiest code in the job: it runs lifecycle scripts from hundreds of packages.

Values that aren't stored as GitHub secrets but are still sensitive, such as a token a step fetches at run time, aren't redacted from logs automatically. Mask them with echo "::add-mask::$VALUE" before anything prints them. Masking only protects the logs, though: it does nothing about code in the same job that reads the value and sends it somewhere else.

That last step is where an egress guard helps. capyshield guard wraps a command and inspects what it sends out; with egress.scan.runtime_env_fingerprints on it recognizes the values of sensitive variables, and in mitm TLS mode it checks HTTPS request bodies too. A blocked leak exits with code 2 and fails the job. It also removes its own token from the wrapped command's environment, so install scripts never see it:

yaml
- run: capyshield guard --config capyshield.yaml --sync -- npm ci
  env:
    CAPYSHIELD_TOKEN: ${{ secrets.CAPYSHIELD_TOKEN }}
- run: capyshield actions --fail-on high   # flags write-all, missing permissions, persisted checkout credentials

capyshield actions reports write-all-permissions, missing-permissions and persist-credentials findings, so a workflow that slips back to a broad token fails review. Setup for both is in the docs.

Frequently asked questions

How do I limit GITHUB_TOKEN permissions?
Add a permissions: block to every workflow, such as permissions: contents: read, and grant extra write permissions only at the job level where needed. When any permission is listed, all unlisted permissions are set to none, and permissions: {} disables them all.
Why use OIDC instead of cloud secrets in GitHub Actions?
With OIDC the job exchanges a signed GitHub identity token for a cloud access token that is valid only for that job and then expires, so there is no long-lived key to steal or rotate. The job needs permissions: id-token: write, and the cloud trust policy should match the token's sub claim exactly.
What are environment secrets in GitHub Actions?
Environment secrets are only available to jobs that reference a specific environment, after the environment's protection rules pass. Rules include up to six required reviewers, allowed deployment branches and tags, and a wait timer.
Is the default GITHUB_TOKEN read-only?
Only for enterprises, organizations and repositories created after GitHub changed the default in February 2023; older ones kept a read and write token unless an admin changed it. Declaring permissions explicitly in each workflow avoids depending on the default.

Sources