Skip to main content

Common Misconfiguration

Exposed GitHub tokens can lead to unauthorized repository access, code theft, and supply chain attacks. 😱

Vulnerable Example

Secure Example


Detection Patterns

  • GitHub Personal Access Token (Classic): `ghp_[a-zA-Z0-9]{36}`
  • GitHub Personal Access Token (Fine-Grained): `github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59}`
  • GitHub OAuth Access Token: `gho_[a-zA-Z0-9]{36}`
  • GitHub App User-to-Server Token: `ghu_[a-zA-Z0-9]{36}`
  • GitHub App Server-to-Server Token: `ghs_[a-zA-Z0-9]{36}`
  • GitHub App Refresh Token: `ghr_[a-zA-Z0-9]{36}`

Prevention Best Practices

  1. Prefer GITHUB_TOKEN in Actions: For most workflow tasks within the same repository (like checkout, commenting on PRs, uploading artifacts), use the built-in, short-lived GITHUB_TOKEN. It’s automatically available and requires no setup. Define minimal permissions for it using the permissions key.
  2. Use GitHub Secrets for Custom Tokens: If you need to access other repositories, external services, or require higher privileges than GITHUB_TOKEN allows, store Personal Access Tokens (PATs) or service keys in GitHub Encrypted Secrets (at the repository, organization, or environment level). Access them via ${{ secrets.MY_SECRET_NAME }}. Never hardcode them.
  3. Prefer GitHub Apps over PATs: For automation or integrations, especially cross-repository or organization-level tasks, create a GitHub App. Apps have more granular permissions, use short-lived installation tokens (generated via a private key), and are generally more secure and manageable than long-lived PATs tied to a user account.
  4. Implement Token Rotation: All static tokens, especially PATs, should have a defined lifespan. Regularly rotate (delete and create new) your tokens to limit the window of opportunity if one is leaked. GitHub’s fine-grained PATs can have expiration dates.
  5. Enforce Least Privilege: Whether using GITHUB_TOKEN, PATs, or App tokens, grant only the absolute minimum permissions required for the task. Avoid overly broad scopes like repo or admin:org. Use fine-grained PATs or specific App permissions.
  6. Use Fine-Grained PATs: When you must use a PAT, prefer the newer fine-grained tokens over classic ones. Fine-grained tokens allow you to specify repository access and much more granular permissions (e.g., read-only access to code, write access only to issues).
  7. Enable SSO/SAML for Org Tokens: If your organization uses SAML Single Sign-On, require PATs and SSH keys to be authorized for SSO access. This links their validity to the user’s active session.
  8. Monitor Audit Logs: Regularly review GitHub’s audit logs (organization and enterprise levels) for suspicious token usage, creation of new high-privilege tokens, or unexpected API activity.
  9. Verify Webhook Signatures: If your application receives webhooks from GitHub, always configure a webhook secret and verify the X-Hub-Signature-256 header on every incoming request. This ensures the request genuinely came from GitHub and wasn’t forged by an attacker.