Skip to content

Commit 959b25a

Browse files
committed
fix(bcrypt): verify should always return boolean value
1 parent 4f7038e commit 959b25a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

packages/bcrypt/__tests__/bcrypt.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ test('verifySync hashed password from @node-rs/bcrypt should be true', async (t)
1616
const hashed = await hash(fx)
1717
t.true(verifySync(fx, hashed))
1818
})
19+
20+
test('verifySync should always return boolean even if the password is invalid', (t) => {
21+
t.false(verifySync('a', 'b'))
22+
t.false(verifySync('a', ''))
23+
t.false(verifySync('', ''))
24+
})

packages/bcrypt/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ fn js_async_hash(ctx: CallContext) -> Result<JsObject> {
8585
fn js_verify(ctx: CallContext) -> Result<JsBoolean> {
8686
let password = ctx.get::<JsBuffer>(0)?.into_value()?;
8787
let hash = ctx.get::<JsBuffer>(1)?.into_value()?;
88-
let result = VerifyTask::verify(&password, &hash)
89-
.map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))?;
88+
let result = VerifyTask::verify(&password, &hash)?;
9089
ctx.env.get_boolean(result)
9190
}
9291

packages/bcrypt/src/verify_task.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ impl VerifyTask {
1515

1616
#[inline]
1717
pub fn verify(password: &[u8], hash: &[u8]) -> Result<bool> {
18-
verify(
19-
&password,
20-
str::from_utf8(&hash).map_err(|_| Error::from_status(Status::StringExpected))?,
18+
Ok(
19+
verify(
20+
&password,
21+
str::from_utf8(&hash).map_err(|_| Error::from_status(Status::StringExpected))?,
22+
)
23+
.unwrap_or(false),
2124
)
22-
.map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
2325
}
2426
}
2527

0 commit comments

Comments
 (0)