Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
crypto: do not add undefined hash in webcrypto normalizeAlgorithm
  • Loading branch information
panva committed Apr 1, 2022
commit 379b548cdaa080c71b179ed8ffba1cdd8b24882a
16 changes: 10 additions & 6 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,35 +206,39 @@ 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') {
const { name } = algorithm;
let hash;
if (typeof name !== 'string' ||
!ArrayPrototypeIncludes(
kAlgorithmsKeys,
StringPrototypeToLowerCase(name))) {
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
}
if (algorithm.hash !== undefined) {
hash = normalizeAlgorithm(algorithm.hash, 'algorithm.hash');
let { hash } = algorithm;
if (hash !== undefined) {
hash = normalizeAlgorithm(hash);
if (!ArrayPrototypeIncludes(kHashTypes, hash.name))
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
}
return {
const normalized = {
...algorithm,
name: kAlgorithms[StringPrototypeToLowerCase(name)],
hash,
};
if (hash) {
normalized.hash = hash;
}
return normalized;
}
}
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
}


Comment thread
panva marked this conversation as resolved.
Outdated
function hasAnyNotIn(set, checks) {
for (const s of set)
if (!ArrayPrototypeIncludes(checks, s))
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 @@ -557,10 +557,10 @@ async function unwrapKey(
extractable,
keyUsages) {
wrappedKey = getArrayBufferOrView(wrappedKey, 'wrappedKey');

unwrapAlgo = normalizeAlgorithm(unwrapAlgo);
let keyData = await cipherOrWrap(
kWebCryptoCipherDecrypt,
normalizeAlgorithm(unwrapAlgo),
unwrapAlgo,
unwrappingKey,
wrappedKey,
'unwrapKey');
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-webcrypto-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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
Comment thread
panva marked this conversation as resolved.
Outdated
assert.strictEqual('hash' in normalizeAlgorithm({ name: 'ECDH' }), false);
assert.strictEqual('hash' in normalizeAlgorithm('ECDH'), false);
}

{
// Check that normalizeAlgorithm does not mutate object inputs
Comment thread
panva marked this conversation as resolved.
Outdated
const algorithm = { name: 'ECDH', hash: 'SHA-256' };
assert.strictEqual(normalizeAlgorithm(algorithm) !== algorithm, true);
assert.deepStrictEqual(algorithm, { name: 'ECDH', hash: 'SHA-256' });
}