From c10e1c44dde0f2963947903fbe19dd0526d2478d Mon Sep 17 00:00:00 2001 From: anushkagupta200615-jpg Date: Mon, 15 Jun 2026 15:01:04 +0530 Subject: [PATCH] fix(bcrypt): handle invalid rounds by throwing OperationError --- src/core/operations/Bcrypt.mjs | 17 +++++++++++------ src/core/operations/BcryptCompare.mjs | 17 +++++++++++------ tests/operations/tests/Hash.mjs | 11 +++++++++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/core/operations/Bcrypt.mjs b/src/core/operations/Bcrypt.mjs index adf6c9c6..725fe935 100644 --- a/src/core/operations/Bcrypt.mjs +++ b/src/core/operations/Bcrypt.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import bcrypt from "bcryptjs"; import { isWorkerEnvironment } from "../Utils.mjs"; @@ -41,13 +42,17 @@ class Bcrypt extends Operation { */ async run(input, args) { const rounds = args[0]; - const salt = await bcrypt.genSalt(rounds); + try { + const salt = await bcrypt.genSalt(rounds); - return await bcrypt.hash(input, salt, undefined, p => { - // Progress callback - if (isWorkerEnvironment()) - self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`); - }); + return await bcrypt.hash(input, salt, undefined, p => { + // Progress callback + if (isWorkerEnvironment()) + self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`); + }); + } catch (err) { + throw new OperationError(err.toString()); + } } diff --git a/src/core/operations/BcryptCompare.mjs b/src/core/operations/BcryptCompare.mjs index 824316ae..62dcb953 100644 --- a/src/core/operations/BcryptCompare.mjs +++ b/src/core/operations/BcryptCompare.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import bcrypt from "bcryptjs"; import { isWorkerEnvironment } from "../Utils.mjs"; @@ -43,13 +44,17 @@ class BcryptCompare extends Operation { async run(input, args) { const hash = args[0]; - const match = await bcrypt.compare(input, hash, undefined, p => { - // Progress callback - if (isWorkerEnvironment()) - self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`); - }); + try { + const match = await bcrypt.compare(input, hash, undefined, p => { + // Progress callback + if (isWorkerEnvironment()) + self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`); + }); - return match ? "Match: " + input : "No match"; + return match ? "Match: " + input : "No match"; + } catch (err) { + throw new OperationError(err.toString()); + } } diff --git a/tests/operations/tests/Hash.mjs b/tests/operations/tests/Hash.mjs index ba502934..e1c4e9c0 100644 --- a/tests/operations/tests/Hash.mjs +++ b/tests/operations/tests/Hash.mjs @@ -993,6 +993,17 @@ TestRegister.addTests([ } ] }, + { + name: "Bcrypt compare: invalid rounds", + input: "hello", + expectedOutput: "Error: Illegal number of rounds (4-31): 34", + recipeConfig: [ + { + op: "Bcrypt compare", + args: ["$2b$34$K.H1WlFDQ/iIo/PiprT/puwluJ5rzuSE5q8D/Fk3NuLgU2aXiGR9m"] + } + ] + }, { name: "Scrypt: RFC test vector 1", input: "",