Skip to content

Commit

Permalink
crypto: fix webcrypto generateKey() with empty usages
Browse files Browse the repository at this point in the history
PR-URL: #43431
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
panva authored and targos committed Jul 18, 2022
1 parent 8f687d7 commit 821d419
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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

0 comments on commit 821d419

Please sign in to comment.