Skip to content

Commit

Permalink
crypto: do not add undefined hash in webcrypto normalizeAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Apr 1, 2022
1 parent 8dbdca8 commit 5de34e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,30 +206,28 @@ function validateMaxBufferLength(data, name) {
}
}

function normalizeAlgorithm(algorithm, label = 'algorithm') {
function normalizeAlgorithm(algorithm) {
if (algorithm != null) {
if (typeof algorithm === 'string')
algorithm = { name: algorithm };

if (typeof algorithm === 'object') {
algorithm = { ...algorithm };
const { name } = algorithm;
let hash;
if (typeof name !== 'string' ||
!ArrayPrototypeIncludes(
kAlgorithmsKeys,
StringPrototypeToLowerCase(name))) {
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
}
algorithm.name = kAlgorithms[StringPrototypeToLowerCase(name)];
if (algorithm.hash !== undefined) {
hash = normalizeAlgorithm(algorithm.hash, 'algorithm.hash');
const hash = normalizeAlgorithm(algorithm.hash, 'algorithm.hash');
if (!ArrayPrototypeIncludes(kHashTypes, hash.name))
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
algorithm.hash = hash;
}
return {
...algorithm,
name: kAlgorithms[StringPrototypeToLowerCase(name)],
hash,
};
return algorithm;
}
}
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-webcrypto-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');

const {
normalizeAlgorithm,
} = require('internal/crypto/util');

{
// Check that normalizeAlgorithm does not add an undefined hash property
assert.strictEqual('hash' in normalizeAlgorithm({ name: 'ECDH' }), false);
assert.strictEqual('hash' in normalizeAlgorithm('ECDH'), false);
}

0 comments on commit 5de34e7

Please sign in to comment.