Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
crypto: fix webcrypto generateKey() with empty usages
  • Loading branch information
panva committed Jun 14, 2022
commit 6de6ec9e996ff78a8b70770aed4755cd1267fa6c
5 changes: 5 additions & 0 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-webcrypto-sign-verify-hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down