crypto: fix webcrypto generateKey() with empty usages

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Filip Skokan authored and Michaël Zasso committed 2022-07-12 07:41:28 +02:00
1 parent 64a9dd7b83
commit 679f19128e
3 files changed
+15 -1

No files matched your search

+5
View File
@@ -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
+9
View File
@@ -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))
@@ -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'