Attempting to store a WebCrypto CryptoKey in an object store of IndexedDB results in a DataCloneError: "Failed to store record in an IDBObjectStore: An object could not be cloned." See this test case for a demonstration: https://embed.plnkr.co/9BWkuIAmxX2EITOWI2yR/
Sounds like we do not support cloning / serializing CryptoKey objects. Likely not truly an IDB issue. Jiewen should probably take a look.
In particular, I suspect we did not implement this part of the spec:
The test URL does not seem to work for me. I see it installing a service worker (not sure what this bug has to do with service workers). If I inspect the service worker, I see there is code using IndexedDB but it is not executed.
We do support storing CryptoKey into IndexedDB. However, due to some legacy reasons, we wrap the objects before storing them into IndexedDB. Here is file: See SerializedCryptoKeyWrap.h. For any new implementations, I recommend we don't wrap CryptoKeys anymore. Since the wrapping APIs will be called in SerializedScriptValue.h, failing to implement those would fail IndexedDB interactions. An example to circumvent that is to implement dummy APIs, see SerializedCryptoKeyWrapGCrypt.cpp.
I am not sure if ServiceWorker needs to implement this dummy APIs though.
The error occurs in service workers only. The example stores a set of objects including a WebCrypto key upon fetch. Please edit the source in the test to trigger the fetch. (and the error)
I also am running into this issue.
It is not specific to IndexedDB as noted.
It occurs with postMessage as well, so it is indeed related to the structured clone algorithm from within the Service Worker Global Scope context.
I am able to use the structured clone algorithm for CryptoKeys within the Window context, but not within the ServiceWorkerGlobalScope context.
You can get the same exception by trying to `postMessage` to a `Client` object from the service worker the `CryptoKey`. And you will get the same 'DataClone' error.
But a `postMessage` on the `window` object via `window.navigator.serviceWorker.controller.postMessage` will not throw the exception. In this case the structured clone succeeds.
I can prepare a Plunkr soon.
I am hypothesizing that a workaround would be for the service worker to only perform `read` operations into IndexedDB for the CryptoKey, and all write operations will have to happen on the Main thread.
Here is a Plunk with a Repro: https://embed.plnkr.co/s2I0U0/
Is it possible that SerializedScriptValue.h for CryptoKey objects runs a different implementation when in the main thread vs. Service Worker thread context?