Skip to content

Commit fd3a8bf

Browse files
panvajuanarbol
authored andcommitted
crypto: do not add undefined hash in webcrypto normalizeAlgorithm
PR-URL: #42559 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 1b8cc7b commit fd3a8bf

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

lib/internal/crypto/util.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -206,30 +206,33 @@ function validateMaxBufferLength(data, name) {
206206
}
207207
}
208208

209-
function normalizeAlgorithm(algorithm, label = 'algorithm') {
209+
function normalizeAlgorithm(algorithm) {
210210
if (algorithm != null) {
211211
if (typeof algorithm === 'string')
212212
algorithm = { name: algorithm };
213213

214214
if (typeof algorithm === 'object') {
215215
const { name } = algorithm;
216-
let hash;
217216
if (typeof name !== 'string' ||
218217
!ArrayPrototypeIncludes(
219218
kAlgorithmsKeys,
220219
StringPrototypeToLowerCase(name))) {
221220
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
222221
}
223-
if (algorithm.hash !== undefined) {
224-
hash = normalizeAlgorithm(algorithm.hash, 'algorithm.hash');
222+
let { hash } = algorithm;
223+
if (hash !== undefined) {
224+
hash = normalizeAlgorithm(hash);
225225
if (!ArrayPrototypeIncludes(kHashTypes, hash.name))
226226
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
227227
}
228-
return {
228+
const normalized = {
229229
...algorithm,
230230
name: kAlgorithms[StringPrototypeToLowerCase(name)],
231-
hash,
232231
};
232+
if (hash) {
233+
normalized.hash = hash;
234+
}
235+
return normalized;
233236
}
234237
}
235238
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');

lib/internal/crypto/webcrypto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,10 @@ async function unwrapKey(
587587
extractable,
588588
keyUsages) {
589589
wrappedKey = getArrayBufferOrView(wrappedKey, 'wrappedKey');
590-
590+
unwrapAlgo = normalizeAlgorithm(unwrapAlgo);
591591
let keyData = await cipherOrWrap(
592592
kWebCryptoCipherDecrypt,
593-
normalizeAlgorithm(unwrapAlgo),
593+
unwrapAlgo,
594594
unwrappingKey,
595595
wrappedKey,
596596
'unwrapKey');

test/parallel/test-webcrypto-util.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
const assert = require('assert');
9+
10+
const {
11+
normalizeAlgorithm,
12+
} = require('internal/crypto/util');
13+
14+
{
15+
// Check that normalizeAlgorithm does not add an undefined hash property.
16+
assert.strictEqual('hash' in normalizeAlgorithm({ name: 'ECDH' }), false);
17+
assert.strictEqual('hash' in normalizeAlgorithm('ECDH'), false);
18+
}
19+
20+
{
21+
// Check that normalizeAlgorithm does not mutate object inputs.
22+
const algorithm = { name: 'ECDH', hash: 'SHA-256' };
23+
assert.strictEqual(normalizeAlgorithm(algorithm) !== algorithm, true);
24+
assert.deepStrictEqual(algorithm, { name: 'ECDH', hash: 'SHA-256' });
25+
}

0 commit comments

Comments
 (0)