共享 github 项目而不共享 API-Key

Sharing github project without sharing API-Key

我想在 GitHub 上与其他人分享我的项目,以便他们可以查看我的代码。

但是,在项目文件中有我的 API-Key,每个人都可以看到它。

感谢任何提示或建议。

However, inside the project files there is my API-Key, which would be visible for every one.

是的,每个人都可以看到它们,它们永远不应该放在您的代码中。

会不会有问题?

  • Would it be a problem?

在您的代码中硬编码秘密的问题在于,它们可能会在 git 提交您在线托管的任何存储库时意外暴露。黑客正在监听 Github API 端点 /events 并将立即扫描任何提交的秘密,并且在几分钟内他们可能会使用您的秘密访问任何第三方服务代表您,然后您可能会有一个故事要讲,比如著名的 我的 2375 美元亚马逊 EC2 错误

所有这些以及更多内容都可以在我最近写的一篇文章中找到,可以在 here:

中找到

Here are some examples of places where exposure of API Keys, tokens, passwords, cloud credentials, and other secrets have been found:

  • In code that is committed to github, gitlab and other online repositories of code.
  • At CI pipelines and automation tools on the cloud.
  • When copy pasting code to places like Whosebug, Forums, Issues trackers, etc.

有什么方法可以隐藏 API 键吗?

  • Is there any way to hide the API-Key?

好吧,要从源代码中隐藏它,您只需要使用环境变量,通常编程语言都支持它,最常见的方法是将它们放在项目根目录下的 .env 文件中.请记住,必须将此 .env 文件添加到 .gitignore 文件,以防止将您的秘密泄露到代码存储库中。

通过使用保险库为您的代码提供秘密,可以在生产部署中实现进一步的改进,例如在 Github:

上使用 Hashicorp Vault 项目

Vault is a tool for securely accessing secrets. A secret is anything that you want to tightly control access to, such as API keys, passwords, certificates, and more. Vault provides a unified interface to any secret, while providing tight access control and recording a detailed audit log.

关于共享项目,我还应该知道什么吗?

  • Anything else that I should know in regard of sharing projects?

通过以 security@yourdomain.com 的形式提供电子邮件或将您的 Twitter 处理程序放在 README 中并要求他们联系,始终方便人们联系您以报告项目中的安全问题你通过私人消息,但记得让私人消息对任何 Twitter 用户开放,否则他们将无法向你发送。

您还应该在 README 中提到 Github 问题不应该用来报告安全漏洞,因为那样会让每个人都看到它们。