From 229fc6117919f2a0bc39d3a732f11f51fbc6b14e Mon Sep 17 00:00:00 2001 From: marko1olo Date: Sat, 6 Jun 2026 06:51:53 +0400 Subject: [PATCH] Fix MD2 rounds validation Reject negative and fractional MD2 round counts before passing the option into the hash implementation. This prevents negative rounds from producing an all-zero digest while preserving zero and positive integer round counts. Co-authored-by: OpenAI Codex --- src/core/operations/MD2.mjs | 8 +++++++- tests/operations/tests/Hash.mjs | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/operations/MD2.mjs b/src/core/operations/MD2.mjs index 38f6d325..126175e5 100644 --- a/src/core/operations/MD2.mjs +++ b/src/core/operations/MD2.mjs @@ -6,6 +6,7 @@ import Operation from "../Operation.mjs"; import {runHash} from "../lib/Hash.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** * MD2 operation @@ -40,7 +41,12 @@ class MD2 extends Operation { * @returns {string} */ run(input, args) { - return runHash("md2", input, {rounds: args[0]}); + const rounds = args[0] ?? 18; + + if (!Number.isInteger(rounds) || rounds < 0) + throw new OperationError("Rounds must be a non-negative integer"); + + return runHash("md2", input, {rounds}); } } diff --git a/tests/operations/tests/Hash.mjs b/tests/operations/tests/Hash.mjs index ba502934..d10c2a72 100644 --- a/tests/operations/tests/Hash.mjs +++ b/tests/operations/tests/Hash.mjs @@ -19,6 +19,28 @@ TestRegister.addTests([ } ] }, + { + name: "MD2 rejects negative rounds", + input: "Hello, World!", + expectedOutput: "Rounds must be a non-negative integer", + recipeConfig: [ + { + "op": "MD2", + "args": [-1] + } + ] + }, + { + name: "MD2 rejects fractional rounds", + input: "Hello, World!", + expectedOutput: "Rounds must be a non-negative integer", + recipeConfig: [ + { + "op": "MD2", + "args": [1.2] + } + ] + }, { name: "MD4", input: "Hello, World!",