Handle invalid bcrypt salt errors in Bcrypt compare (#2615)

This commit is contained in:
Zain Nadeem 2026-07-03 13:52:04 +05:00 committed by GitHub
parent d6f087efad
commit 6f95a2e17d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 5 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";
@ -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";

View File

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