What are the best practices for keeping API keys and secrets secure in a GitHub repository? #211475
Replies: 4 comments
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
|
A good approach is to separate secrets by environment and never commit real credentials to the repository. For local development, use a I'd also recommend enabling Secret Scanning and Push Protection where available. Secret scanning helps identify exposed credentials, while push protection can block supported secrets before they reach the repository. These tools are useful safeguards, but they don't detect every possible secret, so code reviews and careful access management still matter. One important point: if a real key or password has already been pushed, deleting it from the latest commit isn't enough. Revoke or rotate the credential immediately, then clean up the repository history if necessary. Also, never expose server-side API keys in frontend code, since users can inspect browser-delivered JavaScript. Useful references: [GitHub secret security](https://docs.github.com/en/code-security/concepts/secret-security), [Push Protection](https://docs.github.com/en/code-security/concepts/secret-security/push-protection), and [Using secrets in GitHub Actions](https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets). A related community discussion is [How to safely store API keys in a GitHub project?](https://github.com/orgs/community/discussions/188310). |
|
One important principle is that secrets management should be treated as a lifecycle, not just a matter of adding I'd recommend the following practices:
Finally, document the required environment variables and the process for provisioning, rotating, and revoking them. This makes secure practices easier for contributors to follow instead of relying on everyone to remember them. Useful references:
The goal isn't simply to prevent secrets from being committed; it's to minimize their exposure, limit the damage if one leaks, and make recovery straightforward. |
|
1. Catch secrets before they are committed 2. If a secret was already pushed
3. Hosting platforms 4. Help collaborators Combined with Secret Scanning and Push Protection, this covers prevention, detection and recovery. |
Uh oh!
There was an error while loading. Please reload this page.
🏷️ Discussion Type
Bug
💬 Feature/Topic Area
Code quality
Discussion Details
I’m working on a project hosted on GitHub and want to make sure sensitive information, such as API keys, access tokens, and database passwords, is protected. What are the recommended practices for preventing secrets from being exposed in a repository, especially when collaborating with other developers?
I am Mohammed Kanor and I work at Neptune Alloys. A good starting point is to avoid hardcoding API keys or passwords in your source code. Store sensitive values in environment variables or a secrets manager, and use GitHub Actions secrets for credentials required by automated workflows. Also, add local environment files such as .env to your .gitignore file.
GitHub provides features such as Secret Scanning to help detect exposed credentials and Push Protection to prevent supported secrets from being pushed to repositories. Keep access permissions limited, review your repository’s security settings, and rotate any exposed credentials immediately. Remember that removing a secret from the latest commit does not necessarily remove it from the repository’s history.
All reactions