Skip to content

Commit 230d47d

Browse files
committed
crypto: refactor hasAnyNotIn to avoid unsafe array iteration
1 parent 08a2383 commit 230d47d

File tree

8 files changed

+19
-17
lines changed

8 files changed

+19
-17
lines changed

lib/internal/crypto/aes.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
223223

224224
const usageSet = new SafeSet(keyUsages);
225225

226-
if (hasAnyNotIn(usageSet, 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey')) {
226+
if (hasAnyNotIn(usageSet, ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
227227
throw lazyDOMException(
228228
'Unsupported key usage for an AES key',
229229
'SyntaxError');
@@ -258,7 +258,8 @@ async function aesImportKey(
258258
if (name !== 'AES-KW')
259259
ArrayPrototypePush(checkUsages, 'encrypt', 'decrypt');
260260

261-
if (ReflectApply(hasAnyNotIn, null, checkUsages)) {
261+
const usagesSet = new SafeSet(keyUsages);
262+
if (hasAnyNotIn(usagesSet, checkUsages)) {
262263
throw lazyDOMException(
263264
'Unsupported key usage for an AES key',
264265
'SyntaxError');

lib/internal/crypto/diffiehellman.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ function verifyAcceptableDhKeyUse(name, type, usages) {
357357
case 'public':
358358
break;
359359
}
360-
if (ReflectApply(hasAnyNotIn, null, args)) {
360+
if (hasAnyNotIn(usages, checkSet)) {
361361
throw lazyDOMException(
362362
`Unsupported key usage for an ${name} key`,
363363
'SyntaxError');
@@ -370,7 +370,7 @@ async function dhGenerateKey(
370370
keyUsages) {
371371
const usageSet = new SafeSet(keyUsages);
372372

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

lib/internal/crypto/dsa.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function verifyAcceptableDsaKeyUse(name, type, usages) {
6060
check = 'verify';
6161
break;
6262
}
63-
if (hasAnyNotIn(usages, check)) {
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function verifyAcceptableEcKeyUse(name, type, usages) {
8080
break;
8181
}
8282
}
83-
if (ReflectApply(hasAnyNotIn, null, args)) {
83+
if (hasAnyNotIn(usages, checkSet)) {
8484
throw lazyDOMException(
8585
`Unsupported key usage for a ${name} key`,
8686
'SyntaxError');
@@ -150,14 +150,14 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
150150
case 'NODE-ED25519':
151151
// Fall through
152152
case 'NODE-ED448':
153-
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
153+
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
154154
throw lazyDOMException(
155155
'Unsupported key usage for an ECDSA key',
156156
'SyntaxError');
157157
}
158158
break;
159159
case 'ECDH':
160-
if (hasAnyNotIn(usageSet, 'deriveKey', 'deriveBits')) {
160+
if (hasAnyNotIn(usageSet, ['deriveKey', 'deriveBits'])) {
161161
throw lazyDOMException(
162162
'Unsupported key usage for an ECDH key',
163163
'SyntaxError');

lib/internal/crypto/mac.js

+2-2
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

+4-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function verifyAcceptableRsaKeyUse(name, type, usages) {
9595
break;
9696
}
9797
}
98-
if (ReflectApply(hasAnyNotIn, null, args)) {
98+
if (hasAnyNotIn(usages, checkSet)) {
9999
throw lazyDOMException(
100100
`Unsupported key usage for an ${name} key`,
101101
'SyntaxError');
@@ -157,14 +157,15 @@ async function rsaKeyGenerate(
157157

158158
switch (name) {
159159
case 'RSA-OAEP':
160-
if (hasAnyNotIn(usageSet, 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey')) {
160+
if (hasAnyNotIn(usageSet,
161+
['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
161162
throw lazyDOMException(
162163
'Unsupported key usage for a RSA key',
163164
'SyntaxError');
164165
}
165166
break;
166167
default:
167-
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
168+
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
168169
throw lazyDOMException(
169170
'Unsupported key usage for a RSA key',
170171
'SyntaxError');

lib/internal/crypto/util.js

+1-1
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

+2-2
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)