在JavaScript中访问GitHub秘密
我有一个与API互动的学校项目的网页。当前,API密钥保存在JavaScript文件中,我意识到这是安全风险。我将价值存储在一个github的秘密中;我对如何使用github workflows使其成为环境变量有一个粗略的想法,但绝对不知道如何在JavaScript中访问它。我该怎么办?
该项目完全是前端,并使用普通的JavaScript(以及被授予);我不知道如何做任何比API所说的更复杂的后端事情。
I have a webpage I made as a school project that interacts with an API. Currently, the API key is saved in the JavaScript file, which, I realize, is a security risk. I stored the value in a GitHub secret; I have a rough idea of how to make it an environment variable using GitHub workflows, but absolutely no idea how to access it in the JavaScript afterwards. How do I do so?
The project is entirely front-end and built with plain JavaScript (and jQuery, granted); I have no idea how to do any backend stuff more complicated than the API calls themselves.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此对于任何仅浏览您的代码的人来说,它都不可见。
不幸的是,在此处使用GitHub Actions的秘密以进行安全性没有太多意义 - 使令牌无法源源控制, API密钥将需要在用户浏览器的代码中的某个位置。
即使您以某种方式将其混淆,他们也能够使用浏览器的网络检查器查看API键。
There is unfortunately not much point in using GitHub Actions' secrets here for security - for keeping the token out of source control, so it's not immediately visible for anyone just browsing your code, sure, but beyond that:
If your project is frontend only, the API key will need to be somewhere in the code served to the user's browser.
Even if you'd obfuscate it somehow, they'd be able to use the browser's network inspector to see the API key.