fix(bcrypt): handle invalid rounds by throwing OperationError

This commit is contained in:
anushkagupta200615-jpg 2026-06-15 15:01:04 +05:30
parent 7a28e0534b
commit c10e1c44dd
3 changed files with 33 additions and 12 deletions

View File

@ -5,6 +5,7 @@
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import { isWorkerEnvironment } from "../Utils.mjs"; import { isWorkerEnvironment } from "../Utils.mjs";
@ -41,6 +42,7 @@ class Bcrypt extends Operation {
*/ */
async run(input, args) { async run(input, args) {
const rounds = args[0]; const rounds = args[0];
try {
const salt = await bcrypt.genSalt(rounds); const salt = await bcrypt.genSalt(rounds);
return await bcrypt.hash(input, salt, undefined, p => { return await bcrypt.hash(input, salt, undefined, p => {
@ -48,6 +50,9 @@ class Bcrypt extends Operation {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`); self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`);
}); });
} catch (err) {
throw new OperationError(err.toString());
}
} }

View File

@ -5,6 +5,7 @@
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import { isWorkerEnvironment } from "../Utils.mjs"; import { isWorkerEnvironment } from "../Utils.mjs";
@ -43,6 +44,7 @@ class BcryptCompare extends Operation {
async run(input, args) { async run(input, args) {
const hash = args[0]; const hash = args[0];
try {
const match = await bcrypt.compare(input, hash, undefined, p => { const match = await bcrypt.compare(input, hash, undefined, p => {
// Progress callback // Progress callback
if (isWorkerEnvironment()) if (isWorkerEnvironment())
@ -50,6 +52,9 @@ class BcryptCompare extends Operation {
}); });
return match ? "Match: " + input : "No match"; return match ? "Match: " + input : "No match";
} catch (err) {
throw new OperationError(err.toString());
}
} }

View File

@ -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", name: "Scrypt: RFC test vector 1",
input: "", input: "",