From 5de34e728b284468d25598317996671093ba23c7 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 1 Apr 2022 11:02:24 +0200 Subject: [PATCH] crypto: do not add undefined hash in webcrypto normalizeAlgorithm --- lib/internal/crypto/util.js | 14 ++++++-------- test/parallel/test-webcrypto-utils.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 test/parallel/test-webcrypto-utils.js diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index eafcc3d9669288..3e0e1e4edb7567 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -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'); diff --git a/test/parallel/test-webcrypto-utils.js b/test/parallel/test-webcrypto-utils.js new file mode 100644 index 00000000000000..37b87f9ad89433 --- /dev/null +++ b/test/parallel/test-webcrypto-utils.js @@ -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); +}