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/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import * as utils from '../lib/utils'
const security = require('../lib/insecurity')
const users = require('../data/datacache').users

const loginAttempts: Map<string, { attempts: number, lockUntil: number | null }> = new Map()
const MAX_ATTEMPTS = 5
const LOCK_TIME = 15 * 60 * 1000
const CAPTCHA_THRESHOLD = 3

// vuln-code-snippet start loginAdminChallenge loginBenderChallenge loginJimChallenge
module.exports = function login () {
function afterLogin (user: { data: User, bid: number }, res: Response, next: NextFunction) {
Expand Down Expand Up @@ -50,7 +55,27 @@ module.exports = function login () {
// @ts-expect-error FIXME some properties missing in user - vuln-code-snippet hide-line
afterLogin(user, res, next)
} else {
res.status(401).send(res.__('Invalid email or password.'))
const key = req.body.email || req.ip
let entry = loginAttempts.get(key) || { attempts: 0, lockUntil: null }
const now = Date.now()
if (entry.lockUntil && entry.lockUntil > now) {
const retryAfter = Math.ceil((entry.lockUntil - now) / 1000)
return res.status(423).json({ status: 'locked', retryAfter })
}
entry.attempts++
const delay = Math.min(Math.pow(2, entry.attempts), 32) * 1000
if (entry.attempts >= MAX_ATTEMPTS) {
entry.lockUntil = now + LOCK_TIME
}
loginAttempts.set(key, entry)
console.warn(`[login] Failed login attempt for ${key} from IP ${req.ip}, attempt ${entry.attempts}`)
setTimeout(() => {
res.status(401).json({
status: 'Invalid email or password.',
captchaRequired: entry.attempts >= CAPTCHA_THRESHOLD,
lock: entry.attempts >= MAX_ATTEMPTS
})
}, delay)
}
}).catch((error: Error) => {
next(error)
Expand Down