fix(bcrypt): handle invalid rounds by throwing OperationError
This commit is contained in:
parent
7a28e0534b
commit
c10e1c44dd
@ -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,13 +42,17 @@ class Bcrypt extends Operation {
|
|||||||
*/
|
*/
|
||||||
async run(input, args) {
|
async run(input, args) {
|
||||||
const rounds = args[0];
|
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 => {
|
return await bcrypt.hash(input, salt, undefined, p => {
|
||||||
// Progress callback
|
// Progress callback
|
||||||
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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,13 +44,17 @@ class BcryptCompare extends Operation {
|
|||||||
async run(input, args) {
|
async run(input, args) {
|
||||||
const hash = args[0];
|
const hash = args[0];
|
||||||
|
|
||||||
const match = await bcrypt.compare(input, hash, undefined, p => {
|
try {
|
||||||
// Progress callback
|
const match = await bcrypt.compare(input, hash, undefined, p => {
|
||||||
if (isWorkerEnvironment())
|
// Progress callback
|
||||||
self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`);
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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: "",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user