From 92ca2c208bf8c1a8abb21df800619b2890f72461 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 26 Jul 2019 10:32:40 -0400 Subject: [PATCH] crypto: add null check to outputLength logic The Hash constructor's outputLength logic checks if the options input is an object, but doesn't check for null objects. This commit adds that check. PR-URL: https://github.com/nodejs/node/pull/28864 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: Ben Noordhuis --- lib/internal/crypto/hash.js | 3 ++- test/parallel/test-crypto-hash.js | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 667624dce08..38b125e5f22 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -36,7 +36,8 @@ function Hash(algorithm, options) { if (!(this instanceof Hash)) return new Hash(algorithm, options); validateString(algorithm, 'algorithm'); - const xofLen = typeof options === 'object' ? options.outputLength : undefined; + const xofLen = typeof options === 'object' && options !== null ? + options.outputLength : undefined; if (xofLen !== undefined) validateUint32(xofLen, 'options.outputLength'); this[kHandle] = new _Hash(algorithm, xofLen); diff --git a/test/parallel/test-crypto-hash.js b/test/parallel/test-crypto-hash.js index 7f185146bc8..4d3214adb2d 100644 --- a/test/parallel/test-crypto-hash.js +++ b/test/parallel/test-crypto-hash.js @@ -191,6 +191,8 @@ common.expectsError( // Default outputLengths. assert.strictEqual(crypto.createHash('shake128').digest('hex'), '7f9c2ba4e88f827d616045507605853e'); + assert.strictEqual(crypto.createHash('shake128', null).digest('hex'), + '7f9c2ba4e88f827d616045507605853e'); assert.strictEqual(crypto.createHash('shake256').digest('hex'), '46b9dd2b0ba88d13233b3feb743eeb24' + '3fcd52ea62b81b82b50c27646ed5762f');