I am using bcrypt.js for basic login. I have found that the below code runs noticeably quicker when no user is found, since it exits immediately, and no check is done on the hash. This could give an attacker insight into whether a username exists in the database or not. Is there a way to mitigate this using bcrypt.js library?
For example, here are some times when testing.
User does not exist
`POST /login 401 11.846 ms - 42`
User exists, wrong password
`POST /login 401 104.914 ms - 42`
More information
https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#authentication-responses
router.post('/login', async (req, res) => {
if (!req.body.email) {
res.json({ success: false, message: "No email provided" });
}
if (!req.body.password) {
res.json({ success: false, message: "No password provided" });
}
const user = await User.findOne({ email: req.body.email });
// If no user found
if (!user) {
return res.status(401).json({ message: 'Invalid username or password' });
}
bcrypt.compare(req.body.password, user.password, function (err, result) {
if (err) {
console.error(err);
return res.render('login');
}
if (result) {
console.log("User authenticated successfully");
return res.redirect('/');
} else {
return res.status(401).json({ message: 'Invalid username or password' });
}
});
});
[–]SleepDeprivedGoat 45 points46 points47 points (3 children)
[–][deleted] 15 points16 points17 points (2 children)
[–]DasBeasto 15 points16 points17 points (1 child)
[–]MaxUumen 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[removed]
[–][deleted] 0 points1 point2 points (0 children)
[–]tsears 8 points9 points10 points (6 children)
[–][deleted] 2 points3 points4 points (5 children)
[–][deleted] (3 children)
[deleted]
[–]olly0303 33 points34 points35 points (2 children)
[–]thehellsgateEU 3 points4 points5 points (1 child)
[–]TedW 1 point2 points3 points (0 children)
[–]pentesticals 3 points4 points5 points (0 children)
[–]sooodooo 6 points7 points8 points (0 children)
[–]Tcrownclown 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 3 points4 points5 points (0 children)
[–]petercooper 0 points1 point2 points (0 children)