How to use window.crypto.subtle in vscode webview

Viewed 328

In VSCode Webview, window.crypto.subtle not working.

The possible reason is (from MDN):

This feature is available only in secure contexts (HTTPS)

And in VSCode Webview, the window.location.protocal is data:

enter image description here

So, it has a polyfill to resolve this?

PS: I have tried some lib(eg webcrypto-liner, webcrypto-shim), none of them working.

1 Answers

I haven't tested your exact same case but I was facing a similar issue. I wanted to use https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID, which has the same restriction

This feature is available only in secure contexts (HTTPS)

I didn't realize it because was accessing a remote server by IP (protocol HTTP), and then realized it testing exposing my own localhost port with ngrok (which hosts over HTTP/HTTPS) and it worked successfully only on HTTPS hosted link.

Hope this helps! :)

Related