From d98dfcd6f8fb08817207a5d792e6d213e86768d6 Mon Sep 17 00:00:00 2001 From: marko1olo Date: Sat, 6 Jun 2026 01:27:08 +0400 Subject: [PATCH] Fix HMAC empty hash function validation --- src/core/operations/HMAC.mjs | 5 +++++ tests/node/tests/operations.mjs | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/core/operations/HMAC.mjs b/src/core/operations/HMAC.mjs index cb129692..e02ccbe4 100644 --- a/src/core/operations/HMAC.mjs +++ b/src/core/operations/HMAC.mjs @@ -6,6 +6,7 @@ import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; +import OperationError from "../errors/OperationError.mjs"; import CryptoApi from "crypto-api/src/crypto-api.mjs"; /** @@ -67,6 +68,10 @@ class HMAC extends Operation { * @returns {string} */ run(input, args) { + if (!args[1]) { + throw new OperationError("Hashing function must be selected."); + } + const key = Utils.convertToByteString(args[0].string || "", args[0].option), hashFunc = args[1].toLowerCase(), msg = Utils.arrayBufferToStr(input, false), diff --git a/tests/node/tests/operations.mjs b/tests/node/tests/operations.mjs index 6cf85718..a0751f0a 100644 --- a/tests/node/tests/operations.mjs +++ b/tests/node/tests/operations.mjs @@ -661,6 +661,22 @@ WWFkYSBZYWRh\r assert.strictEqual(chef.HMAC("On Cloud Nine", {key: "idea"}).toString(), "e15c268b4ee755c9e52db094ed50add7"); }), + it("HMAC rejects an empty hashing function", () => { + assert.throws( + () => chef.HMAC("hi", { + key: { + option: "UTF8", + string: "", + }, + hashingFunction: "", + }), + { + type: "OperationError", + message: "Hashing function must be selected.", + } + ); + }), + it("JPathExpression", () => { assert.strictEqual(chef.JPathExpression("{\"key\" : \"value\"}", {query: "$.key"}).toString(), "\"value\""); }), @@ -1178,4 +1194,3 @@ ExifImageHeight: 57`); ]); -