feat: add IP-based login brute-force protection (#531)
* feat: add IP-based login brute-force protection - Per-IP rate limiting: 3 failed login attempts locks the IP for 1 hour - Separate counters for password login and token auth - Global safety net: 20 req/min, hard lock after 50 total failures - Persistent lock state to ~/.hermes-web-ui/.login-lock.json (survives restarts) - Manual unlock: edit or delete the lock file - Frontend handles 429/503 responses with localized error messages - i18n support for 8 languages * feat: add locked IP management endpoint and UI - GET /api/auth/locked-ips: list all currently locked IPs (protected) - DELETE /api/auth/locked-ips/:ip: unlock a specific IP (protected) - DELETE /api/auth/locked-ips: unlock all IPs (protected) - AccountSettings: shows locked IPs with remaining time, unlock buttons - i18n support for 8 languages - Clean up stale .js artifacts, add .gitignore rule * fix: cross-type IP lock and IPv6-compatible unlock route - Password and token login now share IP lock state: if an IP is locked by either method, ALL auth methods are blocked for that IP - Changed unlock endpoint from path param to query param (?ip=xxx) to support IPv6 addresses containing colons - Merged unlockIp and unlockAll into a single handler * chore: increase global login rate limit from 20 to 100 requests per minute Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: ekko <fqsy1416@gmail.com> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { Context } from 'koa'
|
||||
import { getCredentials, setCredentials, verifyCredentials, deleteCredentials } from '../services/credentials'
|
||||
import { getToken } from '../services/auth'
|
||||
import { checkPassword, recordPasswordFailure, recordPasswordSuccess, extractIp, getLockedIps, unlockIp, unlockAll } from '../services/login-limiter'
|
||||
|
||||
/**
|
||||
* GET /api/auth/status
|
||||
@@ -27,8 +28,17 @@ export async function login(ctx: Context) {
|
||||
return
|
||||
}
|
||||
|
||||
const ip = extractIp(ctx)
|
||||
const result = checkPassword(ip)
|
||||
if (!result.allowed) {
|
||||
ctx.status = result.status
|
||||
ctx.body = { error: 'Too many login attempts, please try again later' }
|
||||
return
|
||||
}
|
||||
|
||||
const valid = await verifyCredentials(username, password)
|
||||
if (!valid) {
|
||||
recordPasswordFailure(ip)
|
||||
ctx.status = 401
|
||||
ctx.body = { error: 'Invalid username or password' }
|
||||
return
|
||||
@@ -41,6 +51,7 @@ export async function login(ctx: Context) {
|
||||
return
|
||||
}
|
||||
|
||||
recordPasswordSuccess(ip)
|
||||
ctx.body = { token }
|
||||
}
|
||||
|
||||
@@ -150,3 +161,33 @@ export async function removePassword(ctx: Context) {
|
||||
await deleteCredentials()
|
||||
ctx.body = { success: true }
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/auth/locked-ips
|
||||
* List all currently locked IPs (protected).
|
||||
*/
|
||||
export async function listLockedIps(ctx: Context) {
|
||||
const locks = getLockedIps()
|
||||
ctx.body = { locks }
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/auth/locked-ips?ip=xxx
|
||||
* Unlock a specific IP. No ip param = unlock all.
|
||||
*/
|
||||
export async function unlockIpHandler(ctx: Context) {
|
||||
const ip = ctx.query.ip as string
|
||||
if (ip) {
|
||||
const found = unlockIp(ip)
|
||||
if (!found) {
|
||||
ctx.status = 404
|
||||
ctx.body = { error: 'IP not locked' }
|
||||
return
|
||||
}
|
||||
ctx.body = { success: true }
|
||||
return
|
||||
}
|
||||
// No IP specified — unlock all
|
||||
const count = unlockAll()
|
||||
ctx.body = { success: true, count }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user