From 6dd1c75f4a7f08fa8a652ae1094fec6b82a17637 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 21 Mar 2024 13:12:31 +0200 Subject: [PATCH] crypto: fix `input` validation in `crypto.hash` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/52070 Refs: https://github.com/nodejs/node/pull/51044/files#r1522362983 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Luigi Pinca --- lib/internal/crypto/hash.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 50af7b6bb32abf..fda2017fa0cc95 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -52,7 +52,6 @@ const { validateEncoding, validateString, validateUint32, - validateBuffer, } = require('internal/validators'); const { @@ -196,8 +195,8 @@ async function asyncDigest(algorithm, data) { function hash(algorithm, input, outputEncoding = 'hex') { validateString(algorithm, 'algorithm'); - if (typeof input !== 'string') { - validateBuffer(input, 'input'); + if (typeof input !== 'string' && !isArrayBufferView(input)) { + throw new ERR_INVALID_ARG_TYPE('input', ['Buffer', 'TypedArray', 'DataView', 'string'], input); } let normalized = outputEncoding; // Fast case: if it's 'hex', we don't need to validate it further.