Skip to content

Commit b738318

Browse files
panvatargos
authored andcommitted
crypto: fix subtle.getPublicKey error for secret type key inputs
PR-URL: #59558 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 389a24b commit b738318

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/internal/crypto/webcrypto.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,8 @@ async function getPublicKey(key, keyUsages) {
10901090
});
10911091

10921092
if (key[kKeyType] !== 'private')
1093-
throw lazyDOMException('key must be a private key', 'InvalidAccessError');
1093+
throw lazyDOMException('key must be a private key',
1094+
key[kKeyType] === 'secret' ? 'NotSupportedError' : 'InvalidAccessError');
10941095

10951096
const keyObject = createPublicKey(key[kKeyObject]);
10961097

test/parallel/test-webcrypto-get-public-key.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ for await (const { privateKey } of [
4141
name: 'SyntaxError',
4242
message: /Unsupported key usage/
4343
});
44+
45+
await assert.rejects(() => subtle.getPublicKey(publicKey, publicKey.usages), {
46+
name: 'InvalidAccessError',
47+
message: 'key must be a private key'
48+
});
4449
}
4550

4651
const secretKey = await subtle.generateKey(
4752
{ name: 'AES-CBC', length: 128 }, true, ['encrypt', 'decrypt']);
4853
await assert.rejects(() => subtle.getPublicKey(secretKey, ['encrypt', 'decrypt']), {
49-
name: 'InvalidAccessError',
54+
name: 'NotSupportedError',
5055
message: 'key must be a private key'
5156
});

0 commit comments

Comments
 (0)