Skip to content

Commit

Permalink
crypto: refactor hasAnyNotIn to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Feb 19, 2021
1 parent 08a2383 commit 230d47d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {

const usageSet = new SafeSet(keyUsages);

if (hasAnyNotIn(usageSet, 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey')) {
if (hasAnyNotIn(usageSet, ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
throw lazyDOMException(
'Unsupported key usage for an AES key',
'SyntaxError');
Expand Down Expand Up @@ -258,7 +258,8 @@ async function aesImportKey(
if (name !== 'AES-KW')
ArrayPrototypePush(checkUsages, 'encrypt', 'decrypt');

if (ReflectApply(hasAnyNotIn, null, checkUsages)) {
const usagesSet = new SafeSet(keyUsages);
if (hasAnyNotIn(usagesSet, checkUsages)) {
throw lazyDOMException(
'Unsupported key usage for an AES key',
'SyntaxError');
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ function verifyAcceptableDhKeyUse(name, type, usages) {
case 'public':
break;
}
if (ReflectApply(hasAnyNotIn, null, args)) {
if (hasAnyNotIn(usages, checkSet)) {
throw lazyDOMException(
`Unsupported key usage for an ${name} key`,
'SyntaxError');
Expand All @@ -370,7 +370,7 @@ async function dhGenerateKey(
keyUsages) {
const usageSet = new SafeSet(keyUsages);

if (hasAnyNotIn(usageSet, 'deriveKey', 'deriveBits')) {
if (hasAnyNotIn(usageSet, ['deriveKey', 'deriveBits'])) {
throw lazyDOMException(
'Unsupported key usage for a DH key',
'SyntaxError');
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/dsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function verifyAcceptableDsaKeyUse(name, type, usages) {
check = 'verify';
break;
}
if (hasAnyNotIn(usages, check)) {
if (hasAnyNotIn(usages, checkSet)) {
throw lazyDOMException(
`Unsupported key usage for an ${name} key`,
'SyntaxError');
Expand All @@ -84,7 +84,7 @@ async function dsaGenerateKey(

const usageSet = new SafeSet(keyUsages);

if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
throw lazyDOMException(
'Unsupported key usage for a DSA key',
'SyntaxError');
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/crypto/ec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function verifyAcceptableEcKeyUse(name, type, usages) {
break;
}
}
if (ReflectApply(hasAnyNotIn, null, args)) {
if (hasAnyNotIn(usages, checkSet)) {
throw lazyDOMException(
`Unsupported key usage for a ${name} key`,
'SyntaxError');
Expand Down Expand Up @@ -150,14 +150,14 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
case 'NODE-ED25519':
// Fall through
case 'NODE-ED448':
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
throw lazyDOMException(
'Unsupported key usage for an ECDSA key',
'SyntaxError');
}
break;
case 'ECDH':
if (hasAnyNotIn(usageSet, 'deriveKey', 'deriveBits')) {
if (hasAnyNotIn(usageSet, ['deriveKey', 'deriveBits'])) {
throw lazyDOMException(
'Unsupported key usage for an ECDH key',
'SyntaxError');
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) {
validateBitLength(length, 'algorithm.length', true);

const usageSet = new SafeSet(keyUsages);
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
throw lazyDOMException(
'Unsupported key usage for an HMAC key',
'SyntaxError');
Expand Down Expand Up @@ -89,7 +89,7 @@ async function hmacImportKey(
throw new ERR_MISSING_OPTION('algorithm.hash');

const usagesSet = new SafeSet(keyUsages);
if (hasAnyNotIn(usagesSet, 'sign', 'verify')) {
if (hasAnyNotIn(usagesSet, ['sign', 'verify'])) {
throw lazyDOMException(
'Unsupported key usage for an HMAC key',
'SyntaxError');
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function verifyAcceptableRsaKeyUse(name, type, usages) {
break;
}
}
if (ReflectApply(hasAnyNotIn, null, args)) {
if (hasAnyNotIn(usages, checkSet)) {
throw lazyDOMException(
`Unsupported key usage for an ${name} key`,
'SyntaxError');
Expand Down Expand Up @@ -157,14 +157,15 @@ async function rsaKeyGenerate(

switch (name) {
case 'RSA-OAEP':
if (hasAnyNotIn(usageSet, 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey')) {
if (hasAnyNotIn(usageSet,
['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
throw lazyDOMException(
'Unsupported key usage for a RSA key',
'SyntaxError');
}
break;
default:
if (hasAnyNotIn(usageSet, 'sign', 'verify')) {
if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
throw lazyDOMException(
'Unsupported key usage for a RSA key',
'SyntaxError');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function normalizeAlgorithm(algorithm, label = 'algorithm') {
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
}

function hasAnyNotIn(set, ...check) {
function hasAnyNotIn(set, check) {
for (const s of set)
if (!ArrayPrototypeIncludes(check, s))
return true;
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ async function importGenericSecretKey(
if (extractable)
throw lazyDOMException(`${name} keys are not extractable`, 'SyntaxError');

if (hasAnyNotIn(usagesSet, 'deriveKey', 'deriveBits')) {
if (hasAnyNotIn(usagesSet, ['deriveKey', 'deriveBits'])) {
throw lazyDOMException(
`Unsupported key usage for a ${name} key`,
'SyntaxError');
Expand All @@ -419,7 +419,7 @@ async function importGenericSecretKey(
break;
}
case 'raw':
if (hasAnyNotIn(usagesSet, 'deriveKey', 'deriveBits')) {
if (hasAnyNotIn(usagesSet, ['deriveKey', 'deriveBits'])) {
throw lazyDOMException(
`Unsupported key usage for a ${name} key`,
'SyntaxError');
Expand Down

0 comments on commit 230d47d

Please sign in to comment.