PC-004CRITICALSecrets & API Keys

GitHub Personal Access Token or Fine-Grained PAT

Detects GitHub classic personal access tokens (ghp_), fine-grained PATs (github_pat_), and OAuth tokens (gho_) hardcoded in scripts, CI configs, or package manager files.

Rule Slug
github-personal-access-token
Affected Files
*
Detection Method
Regex + Entropy (>= 3.4)
Live Rule Tester100% Browser-Local
CRITICAL: Detected 1 violation
Line 3:ghp_7K9mN2••••••••••••••fG0hI2(entropy: 4.816)

Vulnerable Pattern vs. Hardened Fix

Vulnerable Pattern
# .npmrc
@acme:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=ghp_7K9mN2pQ4rS6tU8vW0xY2zA4bC6dE8fG0hI2
Recommended Fix
# .npmrc (reads token from local shell or CI secret)
@acme:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_PACKAGES_TOKEN}

Why This Fails in Production

A GitHub Personal Access Token acts as a direct programmatic key to your source code supply chain. Classic tokens prefixed with ghp_ are notoriously over-privileged because GitHub’s legacy repo scope grants read and write access to every private and public repository accessible to that user account, including organization repositories. If a leaked token also carries workflow, admin:org, or write:packages scopes, an attacker can inject malicious code directly into release branches, hijack GitHub Actions runners, or publish trojanized npm and container packages under your brand.

Fine-grained tokens (github_pat_) narrow repository scope but still pose severe risk whenever they grant contents:write, secrets:write, or pull_requests:write permissions. OAuth tokens (gho_) similarly permit authenticated API actions on behalf of the authorizing developer. A frequent leak vector is embedding a token inside a .npmrc file for private GitHub Packages, hardcoding an HTTPS git clone URL containing the token inside a Dockerfile or shell script, or pasting a temporary token into an API integration test.

Supply-chain attackers actively hunt for leaked GitHub tokens because compromising a single developer PAT often unlocks private repositories containing additional production secrets, Terraform state files, and deployment keys, multiplying the initial leak into a full infrastructure takeover. Even when GitHub’s push protection or automated secret scanning revokes a public leak, private repository leaks or mirrored remotes may go unnoticed for months.

Instead of static personal tokens, automate repository operations using ephemeral GitHub Actions GITHUB_TOKEN credentials or GitHub Apps with short-lived installation tokens scoped strictly to required permissions.

Remediation Steps

  1. 1Revoke the exposed token immediately under GitHub Settings -> Developer settings -> Personal access tokens (or revoke the OAuth application authorization).
  2. 2Audit your GitHub account security log and organization audit log for unexpected repository clones, SSH key additions, branch pushes, or workflow edits.
  3. 3Replace static tokens in CI/CD with the built-in GITHUB_TOKEN or a scoped GitHub App; for local package registries, reference ${GITHUB_TOKEN} from your shell environment inside .npmrc.
  4. 4Clean the offending commit from Git history if the repository is shared or mirrored.

Frequently Asked Questions

Why are classic ghp_ tokens riskier than fine-grained github_pat_ tokens?
Classic tokens cannot be restricted to a single repository: granting the repo scope exposes every private repository your account can access across all organizations. Fine-grained tokens can be limited to one repo and specific permissions, though neither should ever be committed to Git.
How do I safely authenticate GitHub Packages in Docker builds?
Use Docker BuildKit secret mounts (RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci) rather than passing tokens via ENV or ARG, which persist inside image layers.
Does GitHub automatically revoke leaked tokens?
GitHub revokes recognized token patterns pushed to public repositories, which immediately breaks any CI job using that token. Private repositories without GitHub Advanced Security may not block or revoke leaks automatically.

Related Secrets & API Keys Rules

Scan local project folder →