From d2e67017263444ee4f4785ec7ae4a0d45f2c76bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 2 May 2022 19:46:04 +0200 Subject: [PATCH] crypto: clean up parameter validation in HKDF PR-URL: https://github.com/nodejs/node/pull/42924 Reviewed-By: Filip Skokan Reviewed-By: Antoine du Hamel Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Luigi Pinca Reviewed-By: Zeyu Yang --- lib/internal/crypto/hkdf.js | 9 +++------ lib/internal/crypto/util.js | 2 +- test/parallel/test-crypto-hkdf.js | 5 +++++ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/internal/crypto/hkdf.js b/lib/internal/crypto/hkdf.js index 1d8251a9245b66..f47a2766e0cf53 100644 --- a/lib/internal/crypto/hkdf.js +++ b/lib/internal/crypto/hkdf.js @@ -53,13 +53,10 @@ const { } = require('internal/errors'); const validateParameters = hideStackFrames((hash, key, salt, info, length) => { - key = prepareKey(key); - salt = toBuf(salt); - info = toBuf(info); - validateString(hash, 'digest'); - validateByteSource(salt, 'salt'); - validateByteSource(info, 'info'); + key = prepareKey(key); + salt = validateByteSource(salt, 'salt'); + info = validateByteSource(info, 'info'); validateInteger(length, 'length', 0, kMaxLength); diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index 854994277e5d30..4c1590ed9da9d5 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -271,7 +271,7 @@ const validateByteSource = hideStackFrames((val, name) => { val = toBuf(val); if (isAnyArrayBuffer(val) || isArrayBufferView(val)) - return; + return val; throw new ERR_INVALID_ARG_TYPE( name, diff --git a/test/parallel/test-crypto-hkdf.js b/test/parallel/test-crypto-hkdf.js index 16744201a935dc..2d6689a486ddb6 100644 --- a/test/parallel/test-crypto-hkdf.js +++ b/test/parallel/test-crypto-hkdf.js @@ -15,6 +15,11 @@ const { } = require('crypto'); { + assert.throws(() => hkdf(), { + code: 'ERR_INVALID_ARG_TYPE', + message: /The "digest" argument must be of type string/ + }); + [1, {}, [], false, Infinity].forEach((i) => { assert.throws(() => hkdf(i, 'a'), { code: 'ERR_INVALID_ARG_TYPE',