Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: crypto.subtle.generateKey instanceof with undefined object #457

Closed
edevil opened this issue Dec 9, 2022 · 4 comments
Closed

BUG: crypto.subtle.generateKey instanceof with undefined object #457

edevil opened this issue Dec 9, 2022 · 4 comments
Milestone

Comments

@edevil
Copy link

edevil commented Dec 9, 2022

A simple repro:

❯ npx miniflare --repl
> await crypto.subtle.generateKey(
...         {
...             name: "ECDSA",
...             namedCurve: "P-384"
...         },
...         true,
...         ["sign", "verify"]
...     )
Uncaught TypeError: Right-hand side of 'instanceof' is not an object
    at REPL8:1:33
    at Proxy.generateKey (/Users/acruz/.npm/_npx/46ed1b29b205fa85/node_modules/@miniflare/core/src/standards/crypto.ts:128:7)
❯ node -v
v19.2.0

Debugging the generated Javascript code, https://github.com/cloudflare/miniflare/blob/master/packages/core/src/standards/crypto.ts#L128 is the place where instanceof is called with an undefined object.

@hanneslemberg
Copy link

Just had the same issue working with Jose locally. Workaround is downgrade to node 18.

Discussion in Jose: panva/jose#487

@panva
Copy link

panva commented Dec 21, 2022

The issue is likely that miniflare depends on the webcrypto of node having CryptoKey attached to the webcrypto object. That was a workaround before webcrypto in node was stable and available on the global object in 19.x moving forward.

miniflare should get an update to detect CryptoKey on the global object first, then try and get it from the webcrypto export.

@panva
Copy link

panva commented Dec 21, 2022

I think this patch ought to help with Node 19 or 18 with the --experimental-global-webcrypto flag.

diff --git a/packages/core/src/plugins/core.ts b/packages/core/src/plugins/core.ts
index dc5ddaa..7437f11 100644
--- a/packages/core/src/plugins/core.ts
+++ b/packages/core/src/plugins/core.ts
@@ -543,7 +543,7 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
       Math,
 
       crypto,
-      CryptoKey: crypto.CryptoKey,
+      CryptoKey: globalThis.CryptoKey || crypto.CryptoKey,
       TextDecoder,
       TextEncoder,
 
diff --git a/packages/core/src/standards/crypto.ts b/packages/core/src/standards/crypto.ts
index 6883d6a..649484c 100644
--- a/packages/core/src/standards/crypto.ts
+++ b/packages/core/src/standards/crypto.ts
@@ -125,7 +125,7 @@ const generateKey: typeof webcrypto.subtle.generateKey = async function (
     // @ts-expect-error TypeScript cannot infer the correct overload here
     await webcrypto.subtle.generateKey(algorithm, extractable, keyUsages);
   // noinspection SuspiciousTypeOfGuard
-  if (key instanceof webcrypto.CryptoKey) {
+  if (key instanceof (globalThis.CryptoKey || webcrypto.CryptoKey)) {
     return ensureValidWorkerKey(key);
   } else {
     key.publicKey = ensureValidWorkerKey(key.publicKey);

@dagnelies
Copy link

I noticed that the last release was October 2022 and this issue is blocking me too. Is there some schedule for the next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants