How do you prevent accidentally exposing API keys and secrets in GitHub repositories? #213429
🏷️ Discussion TypeQuestion 💬 Feature/Topic AreaSecret scanning Discussion DetailsHi everyone! I'm curious how other developers handle sensitive information when working on personal projects or production applications. It's easy to accidentally commit API keys, database credentials, or other secrets, especially when working with environment variables and multiple environments. Aside from using .gitignore and environment files, what practices do you follow to prevent secrets from being exposed? Have you used GitHub Secret Scanning or push protection? How effective have they been in your workflow? Would love to hear your experiences and recommendations! |
Replies: 2 comments 2 replies
|
I usually keep sensitive values in environment variables and make sure For GitHub projects, Secret Scanning and push protection are useful extra layers of security. They can help catch exposed secrets before they become a bigger problem. One thing I always keep in mind is that if a secret gets pushed accidentally, deleting it from the code isn't enough. You should revoke or rotate the exposed key immediately. |
|
The uncomfortable bit is that What actually moved the needle for us: Stop shipping long-lived credentials. If the secret exists only in a vault and the workload gets a short-lived OIDC token at runtime, there's nothing to leak into a repo. GitHub Actions supports OIDC federation directly for AWS/GCP/Azure and Vault. This eliminated most of our Make the secret format detectable. Push protection is good but it blocks known provider patterns and some generic structured secrets. Internal tokens, hand-rolled API keys, and connection strings can sail through unless you define a custom pattern (secret scanning supports up to 500 per org, and those patterns can be push-protected too). A five-minute regex for your own Commit the shape, not the value. Treat exposure as a rotation event, not a cleanup event. Revoke first, then worry about history. So yes, push protection is worth enabling org-wide — just don't mistake it for the whole strategy. It's the last net, not the floor. |
I usually keep sensitive values in environment variables and make sure
.envfiles are included in.gitignore. I also avoid hardcoding API keys or database credentials directly into the source code.For GitHub projects, Secret Scanning and push protection are useful extra layers of security. They can help catch exposed secrets before they become a bigger problem.
One thing I always keep in mind is that if a secret gets pushed accidentally, deleting it from the code isn't enough. You should revoke or rotate the exposed key immediately.