Skip to content

Commit e7283ad

Browse files
committed
crypto: refactor hasAnyNotIn to avoid unsafe array iteration
1 parent d345ac9 commit e7283ad

File tree

8 files changed

+18
-17
lines changed

8 files changed

+18
-17
lines changed

lib/internal/crypto/aes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
222222

223223
const usageSet = new SafeSet(keyUsages);
224224

225-
if (hasAnyNotIn(usageSet, 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey')) {
225+
if (hasAnyNotIn(usageSet, ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
226226
throw lazyDOMException(
227227
'Unsupported key usage for an AES key',
228228
'SyntaxError');
@@ -257,7 +257,7 @@ async function aesImportKey(
257257
ArrayPrototypePush(checkUsages, 'encrypt', 'decrypt');
258258

259259
const usagesSet = new SafeSet(keyUsages);
260-
if (hasAnyNotIn(usagesSet, ...checkUsages)) {
260+
if (hasAnyNotIn(usagesSet, checkUsages)) {
261261
throw lazyDOMException(
262262
'Unsupported key usage for an AES key',
263263
'SyntaxError');

lib/internal/crypto/diffiehellman.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function verifyAcceptableDhKeyUse(name, type, usages) {
356356
checkSet = [];
357357
break;
358358
}
359-
if (hasAnyNotIn(usages, ...checkSet)) {
359+
if (hasAnyNotIn(usages, checkSet)) {
360360
throw lazyDOMException(
361361
`Unsupported key usage for an ${name} key`,
362362
'SyntaxError');
@@ -369,7 +369,7 @@ async function dhGenerateKey(
369369
keyUsages) {
370370
const usageSet = new SafeSet(keyUsages);
371371

372-
if (hasAnyNotIn(usageSet, 'deriveKey', 'deriveBits')) {
372+
if (hasAnyNotIn(usageSet, ['deriveKey', 'deriveBits'])) {
373373
throw lazyDOMException(
374374
'Unsupported key usage for a DH key',
375375
'SyntaxError');

lib/internal/crypto/dsa.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function verifyAcceptableDsaKeyUse(name, type, usages) {
6060
checkSet = ['verify'];
6161
break;
6262
}
63-
if (hasAnyNotIn(usages, ...checkSet)) {
63+
if (hasAnyNotIn(usages, checkSet)) {
6464
throw lazyDOMException(
6565
`Unsupported key usage for an ${name} key`,
6666
'SyntaxError');
@@ -84,7 +84,7 @@ async function dsaGenerateKey(
8484

8585
const usageSet = new SafeSet(keyUsages);
8686

87-
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
87+
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
8888
throw lazyDOMException(
8989
'Unsupported key usage for a DSA key',
9090
'SyntaxError');

lib/internal/crypto/ec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function verifyAcceptableEcKeyUse(name, type, usages) {
7878
break;
7979
}
8080
}
81-
if (hasAnyNotIn(usages, ...checkSet)) {
81+
if (hasAnyNotIn(usages, checkSet)) {
8282
throw lazyDOMException(
8383
`Unsupported key usage for a ${name} key`,
8484
'SyntaxError');
@@ -148,14 +148,14 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
148148
case 'NODE-ED25519':
149149
// Fall through
150150
case 'NODE-ED448':
151-
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
151+
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
152152
throw lazyDOMException(
153153
'Unsupported key usage for an ECDSA key',
154154
'SyntaxError');
155155
}
156156
break;
157157
case 'ECDH':
158-
if (hasAnyNotIn(usageSet, 'deriveKey', 'deriveBits')) {
158+
if (hasAnyNotIn(usageSet, ['deriveKey', 'deriveBits'])) {
159159
throw lazyDOMException(
160160
'Unsupported key usage for an ECDH key',
161161
'SyntaxError');

lib/internal/crypto/mac.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) {
5656
validateBitLength(length, 'algorithm.length', true);
5757

5858
const usageSet = new SafeSet(keyUsages);
59-
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
59+
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
6060
throw lazyDOMException(
6161
'Unsupported key usage for an HMAC key',
6262
'SyntaxError');
@@ -89,7 +89,7 @@ async function hmacImportKey(
8989
throw new ERR_MISSING_OPTION('algorithm.hash');
9090

9191
const usagesSet = new SafeSet(keyUsages);
92-
if (hasAnyNotIn(usagesSet, 'sign', 'verify')) {
92+
if (hasAnyNotIn(usagesSet, ['sign', 'verify'])) {
9393
throw lazyDOMException(
9494
'Unsupported key usage for an HMAC key',
9595
'SyntaxError');

lib/internal/crypto/rsa.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function verifyAcceptableRsaKeyUse(name, type, usages) {
9393
break;
9494
}
9595
}
96-
if (hasAnyNotIn(usages, ...checkSet)) {
96+
if (hasAnyNotIn(usages, checkSet)) {
9797
throw lazyDOMException(
9898
`Unsupported key usage for an ${name} key`,
9999
'SyntaxError');
@@ -155,14 +155,15 @@ async function rsaKeyGenerate(
155155

156156
switch (name) {
157157
case 'RSA-OAEP':
158-
if (hasAnyNotIn(usageSet, 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey')) {
158+
if (hasAnyNotIn(usageSet,
159+
['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
159160
throw lazyDOMException(
160161
'Unsupported key usage for a RSA key',
161162
'SyntaxError');
162163
}
163164
break;
164165
default:
165-
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
166+
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
166167
throw lazyDOMException(
167168
'Unsupported key usage for a RSA key',
168169
'SyntaxError');

lib/internal/crypto/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function normalizeAlgorithm(algorithm, label = 'algorithm') {
236236
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
237237
}
238238

239-
function hasAnyNotIn(set, ...check) {
239+
function hasAnyNotIn(set, check) {
240240
for (const s of set)
241241
if (!ArrayPrototypeIncludes(check, s))
242242
return true;

lib/internal/crypto/webcrypto.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ async function importGenericSecretKey(
402402
if (extractable)
403403
throw lazyDOMException(`${name} keys are not extractable`, 'SyntaxError');
404404

405-
if (hasAnyNotIn(usagesSet, 'deriveKey', 'deriveBits')) {
405+
if (hasAnyNotIn(usagesSet, ['deriveKey', 'deriveBits'])) {
406406
throw lazyDOMException(
407407
`Unsupported key usage for a ${name} key`,
408408
'SyntaxError');
@@ -419,7 +419,7 @@ async function importGenericSecretKey(
419419
break;
420420
}
421421
case 'raw':
422-
if (hasAnyNotIn(usagesSet, 'deriveKey', 'deriveBits')) {
422+
if (hasAnyNotIn(usagesSet, ['deriveKey', 'deriveBits'])) {
423423
throw lazyDOMException(
424424
`Unsupported key usage for a ${name} key`,
425425
'SyntaxError');

0 commit comments

Comments
 (0)