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 18, 2021
1 parent d345ac9 commit e7283ad
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,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 @@ -257,7 +257,7 @@ async function aesImportKey(
ArrayPrototypePush(checkUsages, 'encrypt', 'decrypt');

const usagesSet = new SafeSet(keyUsages);
if (hasAnyNotIn(usagesSet, ...checkUsages)) {
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 @@ -356,7 +356,7 @@ function verifyAcceptableDhKeyUse(name, type, usages) {
checkSet = [];
break;
}
if (hasAnyNotIn(usages, ...checkSet)) {
if (hasAnyNotIn(usages, checkSet)) {
throw lazyDOMException(
`Unsupported key usage for an ${name} key`,
'SyntaxError');
Expand All @@ -369,7 +369,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) {
checkSet = ['verify'];
break;
}
if (hasAnyNotIn(usages, ...checkSet)) {
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 @@ -78,7 +78,7 @@ function verifyAcceptableEcKeyUse(name, type, usages) {
break;
}
}
if (hasAnyNotIn(usages, ...checkSet)) {
if (hasAnyNotIn(usages, checkSet)) {
throw lazyDOMException(
`Unsupported key usage for a ${name} key`,
'SyntaxError');
Expand Down Expand Up @@ -148,14 +148,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 @@ -93,7 +93,7 @@ function verifyAcceptableRsaKeyUse(name, type, usages) {
break;
}
}
if (hasAnyNotIn(usages, ...checkSet)) {
if (hasAnyNotIn(usages, checkSet)) {
throw lazyDOMException(
`Unsupported key usage for an ${name} key`,
'SyntaxError');
Expand Down Expand Up @@ -155,14 +155,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 e7283ad

Please sign in to comment.