Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion routes/resetPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import challengeUtils = require('../lib/challengeUtils')
const users = require('../data/datacache').users
const security = require('../lib/insecurity')

const resetFailures: Record<string, { count: number, firstAttempt: number }> = {}
const MAX_RESET_ATTEMPTS = 5
const LOCKOUT_DURATION = 30 * 60 * 1000 // 30 minutes

module.exports = function resetPassword () {
return ({ body, connection }: Request, res: Response, next: NextFunction) => {
const email = body.email
Expand All @@ -27,6 +31,16 @@ module.exports = function resetPassword () {
} else if (newPassword !== repeatPassword) {
res.status(401).send(res.__('New and repeated password do not match.'))
} else {
const now = Date.now()
const attempts = resetFailures[email]
if (attempts) {
if (now - attempts.firstAttempt > LOCKOUT_DURATION) {
delete resetFailures[email]
} else if (attempts.count >= MAX_RESET_ATTEMPTS) {
res.status(429).send(res.__('Too many failed attempts. Try again later.'))
return
}
}
SecurityAnswerModel.findOne({
include: [{
model: UserModel,
Expand All @@ -38,14 +52,25 @@ module.exports = function resetPassword () {
user?.update({ password: newPassword }).then((user: UserModel) => {
verifySecurityAnswerChallenges(user, answer)
res.json({ user })
delete resetFailures[email]
}).catch((error: unknown) => {
next(error)
})
}).catch((error: unknown) => {
next(error)
})
} else {
res.status(401).send(res.__('Wrong answer to security question.'))
const now = Date.now()
if (!resetFailures[email]) {
resetFailures[email] = { count: 1, firstAttempt: now }
} else {
resetFailures[email].count++
}
if (resetFailures[email].count >= MAX_RESET_ATTEMPTS) {
res.status(429).send(res.__('Too many failed attempts. Try again later.'))
} else {
res.status(401).send(res.__('Wrong answer to security question.'))
}
}
}).catch((error: unknown) => {
next(error)
Expand Down