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 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());
}
}

View File

@ -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());
}
}

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",
input: "",