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
- Prefer
GITHUB_TOKENin Actions: For most workflow tasks within the same repository (like checkout, commenting on PRs, uploading artifacts), use the built-in, short-livedGITHUB_TOKEN. It’s automatically available and requires no setup. Define minimal permissions for it using thepermissionskey. - Use GitHub Secrets for Custom Tokens: If you need to access other repositories, external services, or require higher privileges than
GITHUB_TOKENallows, 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. - 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.
- 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.
- 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 likerepooradmin:org. Use fine-grained PATs or specific App permissions. - 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).
- 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.
- 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.
- Verify Webhook Signatures: If your application receives webhooks from GitHub, always configure a webhook secret and verify the
X-Hub-Signature-256header on every incoming request. This ensures the request genuinely came from GitHub and wasn’t forged by an attacker.

