From 301c962b9bf5431bc73fcf45567151b1ddad22d1 Mon Sep 17 00:00:00 2001 From: Garrett Date: Fri, 12 Jul 2024 12:11:50 -0400 Subject: [PATCH] Update hmac.js to include a better error for undefined key When key is undefined, the user of the package will get the error `Cannot read property 'sigBytes' of undefined` This fix prevents that and returns an error that says that the key is undefined. --- src/hmac.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/hmac.js b/src/hmac.js index 24762a9..0625608 100644 --- a/src/hmac.js +++ b/src/hmac.js @@ -25,10 +25,20 @@ // Init hasher hasher = this._hasher = new hasher.init(); - // Convert string to WordArray, else assume WordArray already - if (typeof key == 'string') { + if (key == null) { // This checks for both undefined and null + throw new Error("Key is undefined or null"); + } + + // Convert string to WordArray, else assume WordArray already + if (typeof key == "string") { key = Utf8.parse(key); - } + } + + // Determine if the key is missing any values - most notably, the 'sigBytes' property + if (!Object.prototype.hasOwnProperty.call(key, "sigBytes")) { + // throw error + throw new Error("Key missing 'sigBytes' property"); + } // Shortcuts var hasherBlockSize = hasher.blockSize;