如何区分 JS 不透明对象?

how can one distinguish JS Opaque Objects?

WebCrypto API introduces the notion of non exportable private keys, which can be exported to IndexDB but not not LocalStorage or over the web. This is nicely explained in Charles Engleke's blog "Saving Cryptographic Keys in the Browser”。

但是这些对象实际上是如何工作的呢?有没有办法从 JS 判断一个对象是否不透明?我找不到任何关于此的信息。

任何地方都没有神奇的"opaque flag"。 "Opaque" 此处仅表示对象中保存的数据从不 对脚本可见。您仍然可以使用 CryptoKey 实例执行一些操作 - 例如在这种情况下,将其存储在 Indexed DB 中或通过 postMessage().

发送到另一个上下文

这与例如一个 Blob 对象,其中 所有 持有的数据可以通过对象上的属性直接检查或通过 FileReader.

间接检查

另一个不透明的例子是 Response 作为跨源结果的对象 Fetch operation, which can be processed by a Service Worker 但主体无法被检查。

所以 "is there a way to tell from JS if an object is opaque?" - 这取决于。如果一个对象是 CryptoKey 的实例,那么您就知道存在隐藏数据,因此它是不透明的。如果对象是 Blob 的实例,那么您知道有一种方法可以访问数据,即使您需要其他 API 才能访问它,所以它不是不透明的。如果对象是 Response 的实例,则它可能是不透明的,具体取决于来源。