diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index a5f442b0e57cfb..dc2e94aae7770e 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -89,6 +89,11 @@ async function generateKey( algorithm = normalizeAlgorithm(algorithm); validateBoolean(extractable, 'extractable'); validateArray(keyUsages, 'keyUsages'); + if (keyUsages.length === 0) { + throw lazyDOMException( + 'Usages cannot be empty when creating a key', + 'SyntaxError'); + } switch (algorithm.name) { case 'RSASSA-PKCS1-v1_5': // Fall through diff --git a/test/parallel/test-webcrypto-keygen.js b/test/parallel/test-webcrypto-keygen.js index 32e8fc39ee6d15..5d039fdf036344 100644 --- a/test/parallel/test-webcrypto-keygen.js +++ b/test/parallel/test-webcrypto-keygen.js @@ -210,6 +210,15 @@ const vectors = { // Test bad usages { async function test(name) { + await assert.rejects( + subtle.generateKey( + { + name, ...vectors[name].algorithm + }, + true, + []), + { message: /Usages cannot be empty/ }); + const invalidUsages = []; allUsages.forEach((usage) => { if (!vectors[name].usages.includes(usage)) diff --git a/test/parallel/test-webcrypto-sign-verify-hmac.js b/test/parallel/test-webcrypto-sign-verify-hmac.js index 3028816054ca76..0962773b132768 100644 --- a/test/parallel/test-webcrypto-sign-verify-hmac.js +++ b/test/parallel/test-webcrypto-sign-verify-hmac.js @@ -153,7 +153,7 @@ async function testSign({ hash, } await assert.rejects( - subtle.generateKey({ name }, false, []), { + subtle.generateKey({ name }, false, ['sign', 'verify']), { name: 'TypeError', code: 'ERR_MISSING_OPTION', message: 'algorithm.hash is required'