Skip to content

Commit df21856

Browse files
committed
crypto: do not add undefined hash in webcrypto normalizeAlgorithm
1 parent 8dbdca8 commit df21856

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

lib/internal/crypto/util.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,35 +206,43 @@ function validateMaxBufferLength(data, name) {
206206
}
207207
}
208208

209-
function normalizeAlgorithm(algorithm, label = 'algorithm') {
209+
function validateAlgorithmName(name) {
210+
if (typeof name !== 'string' ||
211+
!ArrayPrototypeIncludes(
212+
kAlgorithmsKeys,
213+
StringPrototypeToLowerCase(name))) {
214+
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
215+
}
216+
}
217+
218+
function normalizeAlgorithm(algorithm) {
210219
if (algorithm != null) {
211220
if (typeof algorithm === 'string')
212221
algorithm = { name: algorithm };
213222

214223
if (typeof algorithm === 'object') {
215224
const { name } = algorithm;
216-
let hash;
217-
if (typeof name !== 'string' ||
218-
!ArrayPrototypeIncludes(
219-
kAlgorithmsKeys,
220-
StringPrototypeToLowerCase(name))) {
221-
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
222-
}
223-
if (algorithm.hash !== undefined) {
224-
hash = normalizeAlgorithm(algorithm.hash, 'algorithm.hash');
225+
validateAlgorithmName(name);
226+
let { hash } = algorithm;
227+
if (hash !== undefined) {
228+
hash = normalizeAlgorithm(hash);
225229
if (!ArrayPrototypeIncludes(kHashTypes, hash.name))
226230
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
227231
}
228-
return {
232+
const normalized = {
229233
...algorithm,
230234
name: kAlgorithms[StringPrototypeToLowerCase(name)],
231-
hash,
232235
};
236+
if (hash) {
237+
normalized.hash = hash;
238+
}
239+
return normalized;
233240
}
234241
}
235242
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
236243
}
237244

245+
238246
function hasAnyNotIn(set, checks) {
239247
for (const s of set)
240248
if (!ArrayPrototypeIncludes(checks, s))

lib/internal/crypto/webcrypto.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,10 @@ async function unwrapKey(
557557
extractable,
558558
keyUsages) {
559559
wrappedKey = getArrayBufferOrView(wrappedKey, 'wrappedKey');
560-
560+
unwrapAlgo = normalizeAlgorithm(unwrapAlgo);
561561
let keyData = await cipherOrWrap(
562562
kWebCryptoCipherDecrypt,
563-
normalizeAlgorithm(unwrapAlgo),
563+
unwrapAlgo,
564564
unwrappingKey,
565565
wrappedKey,
566566
'unwrapKey');
Lines changed: 25 additions & 0 deletions
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)