crypto: fix subtle.getPublicKey error for secret type key inputs

PR-URL: https://github.com/nodejs/node/pull/59558
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Filip Skokan 2025-08-22 17:56:56 +02:00 committed by GitHub
parent 3b4f9b26b1
commit 7178e9141a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -1090,7 +1090,8 @@ async function getPublicKey(key, keyUsages) {
});
if (key[kKeyType] !== 'private')
throw lazyDOMException('key must be a private key', 'InvalidAccessError');
throw lazyDOMException('key must be a private key',
key[kKeyType] === 'secret' ? 'NotSupportedError' : 'InvalidAccessError');
const keyObject = createPublicKey(key[kKeyObject]);

View File

@ -41,11 +41,16 @@ for await (const { privateKey } of [
name: 'SyntaxError',
message: /Unsupported key usage/
});
await assert.rejects(() => subtle.getPublicKey(publicKey, publicKey.usages), {
name: 'InvalidAccessError',
message: 'key must be a private key'
});
}
const secretKey = await subtle.generateKey(
{ name: 'AES-CBC', length: 128 }, true, ['encrypt', 'decrypt']);
await assert.rejects(() => subtle.getPublicKey(secretKey, ['encrypt', 'decrypt']), {
name: 'InvalidAccessError',
name: 'NotSupportedError',
message: 'key must be a private key'
});