我开发了一个网站,我需要在客户端存储一个私钥。 如何在客户端安全地存储这个变量? 我可以使用 sessionStorage 吗?
(我希望只有当前用户可以访问此(可变)信息。一旦他关闭浏览器,就必须删除数据。)
谢谢。
最佳答案
在顶层,是的,sessionStorage
会做您需要的。引用MDN’s page on sessionStorage
:
- A page session lasts as long as the browser is open, and survives over page reloads and restores.
- Opening a page in a new tab or window creates a new session with the value of the top-level browsing context, which differs from how session cookies work.
- Opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window.
- Closing a tab/window ends the session and clears objects in sessionStorage.
虽然有几件事情要小心。首先,与服务器的任何连接都需要安全地完成。这需要一个 HTTPS 连接,此时可能使用 TLS 1.2 或 1.3。
其次,您需要确保页面环境干净。这意味着您无法加载可能泄露私钥的第三方 JavaScript。至少,您加载的任何第三方 JS 都需要先进行审核,然后才能获得 integrity
attribute。添加以确保它不会改变。
最后,您可能希望添加一些内容以在用户使用完页面后销毁 key 。这可能是警告他们在使用完系统后关闭页面,或者更自动的事情,比如在 x 分钟后退出 key 并让系统及时协商一个新 key 。显然,安全性和可用性之间存在平衡,但最好的系统可以以对用户透明的方式做到这一点。
关于javascript - 在客户端存储敏感信息(Web),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59235018/