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

crypto.subtle.importKey does not accept ArrayBuffer (ArrayBuffer.prototype mismatch) #813

Open
tksst opened this issue Feb 23, 2024 · 8 comments

Comments

@tksst
Copy link

tksst commented Feb 23, 2024

Node.js 20.11.1
edge-runtime@2.5.9

Bug Report

Current behavior

foo.js:

(async () => {
	const key = new ArrayBuffer(8);
	const data = new ArrayBuffer(8);

	const importedKey = await crypto.subtle.importKey(
		"raw",
		key,
		{
			name: "HMAC",
			hash: { name: "SHA-256" },
		},
		false,
		["sign"],
	);

	await crypto.subtle.sign("HMAC", importedKey, data);
})();

cmd:

$ npm exec edge-runtime foo.js

result(error):

node:internal/crypto/webidl:45
  const err = new TypeError(message);
              ^

TypeError: Failed to execute 'importKey' on 'SubtleCrypto': 2nd argument is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.
    at codedTypeError (node:internal/crypto/webidl:45:15)
    at makeException (node:internal/crypto/webidl:54:10)
    at converters.BufferSource (node:internal/crypto/webidl:221:11)
    at SubtleCrypto.importKey (node:internal/crypto/webcrypto:589:36)
    at evalmachine.<anonymous>:6:45
    at evalmachine.<anonymous>:19:3
    at Script.runInContext (node:vm:133:12)
    at runInContext (node:vm:287:6)
    at EdgeVM.evaluate (/dev/edge/node_modules/@edge-runtime/vm/dist/vm.js:26:38)
    at new EdgeVM (/dev/edge/node_modules/@edge-runtime/vm/dist/edge-vm.js:49:18) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v20.11.1

Expected behavior/code

Done without error

Additional context/screenshots

The above code has been verified to work with raw Node.js, Firefox and Chrome.

ArrayBuffer.prototype seems to be different from the original. Node.js checks here:
https://github.com/nodejs/node/blob/9b1bf44ea9e7785e38c93b7d22d32dbca262df6c/lib/internal/crypto/webidl.js#L183

return ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V);

Wrapping with uint8array is a workaround:

(async () => {
	const key = new ArrayBuffer(8);
	const data = new ArrayBuffer(8);

	const importedKey = await crypto.subtle.importKey(
		"raw",
		new Uint8Array(key),
		{
			name: "HMAC",
			hash: { name: "SHA-256" },
		},
		false,
		["sign"],
	);

	await crypto.subtle.sign("HMAC", importedKey, new Uint8Array(data));
})();
@glenntws
Copy link

Occuring the same issue while using gr2m/universal-github-app-jwt. They obviously generate an ArrayBuffer, but importKey fails with the exact same type error.

I can also replicate it simply by doing await crypto.suble.importKey("pkcs8", new ArrayBuffer(), .....).

@god-benten
Copy link

I wrote a simple test code.

new ArrayBuffer().constructor === ArrayBuffer

above code evaluated as false on edge-runtime though i'm not sure why...

@Moe03
Copy link

Moe03 commented Jul 17, 2024

any update on this? seems to not give the same error on cloudflare workers

@Noitidart
Copy link

Same situation here in my local development. I have not yet pushed this up to Vercel to test in production.

Using next 14.0.4 and node 18.18.2. (also tried with node 20.16.0)

@ssikyou
Copy link

ssikyou commented Oct 19, 2024

@Noitidart
Copy link

Noitidart commented Oct 22, 2024

Thanks @ssikyou - are you sure that makes an impact though? Because in non-edge runtime this code works perfect - https://stackoverflow.com/questions/78960913/converting-pkcs8-node-crypto-to-web-crypto-apple-mapkit-js-token

But as soon as it's in edge runtime it doesn't work.

@ssikyou
Copy link

ssikyou commented Oct 23, 2024

@Noitidart, I'm not sure why this makes the difference. But edge-runtime 2.5.x + nodejs 18.16.1 works for me in local env.

@kevva
Copy link

kevva commented Dec 10, 2024

This fails with something simple as crypto.subtle.digest('SHA-512', (new Uint8Array(10)).buffer) as well.

Failed to execute 'digest' on 'SubtleCrypto': 2nd argument is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.

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

No branches or pull requests

7 participants