From 6f95a2e17db7dd45789be96385ca19faa42fbf05 Mon Sep 17 00:00:00 2001 From: Zain Nadeem Date: Fri, 3 Jul 2026 13:52:04 +0500 Subject: [PATCH] Handle invalid bcrypt salt errors in Bcrypt compare (#2615) --- src/core/operations/BcryptCompare.mjs | 16 +++++++++++----- tests/operations/tests/Hash.mjs | 11 +++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/core/operations/BcryptCompare.mjs b/src/core/operations/BcryptCompare.mjs index 824316ae..9720976a 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,11 +44,16 @@ 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)}%`); - }); + let match; + try { + match = await bcrypt.compare(input, hash, undefined, p => { + // Progress callback + if (isWorkerEnvironment()) + self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`); + }); + } catch (err) { + throw new OperationError(err.toString()); + } return match ? "Match: " + input : "No match"; diff --git a/tests/operations/tests/Hash.mjs b/tests/operations/tests/Hash.mjs index ba502934..1ffb749c 100644 --- a/tests/operations/tests/Hash.mjs +++ b/tests/operations/tests/Hash.mjs @@ -993,6 +993,17 @@ TestRegister.addTests([ } ] }, + { + name: "Bcrypt compare: invalid salt version", + input: "password", + expectedOutput: "Error: Invalid salt version: $a", + recipeConfig: [ + { + op: "Bcrypt compare", + args: ["$ab$04$K.H1WlFDQ/iIo/PiprT/puwluJ5rzuSE5q8D/Fk3NuLgU2aXiGR9m"] + } + ] + }, { name: "Scrypt: RFC test vector 1", input: "",