From dcc4fd370af2c259975e14f69d2a1feefcb2ce29 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 4 Mar 2026 17:55:25 +0300 Subject: [PATCH] feat: replace fixed login rate limit with progressive lockout Made-with: Cursor --- .env.example | 3 +- src/config/env.ts | 1 - src/plugins/rateLimit.ts | 2 - src/routes/auth.ts | 44 ++++++++++++---- src/utils/loginLockout.ts | 106 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 src/utils/loginLockout.ts diff --git a/.env.example b/.env.example index a41b8c4..9827bea 100644 --- a/.env.example +++ b/.env.example @@ -23,8 +23,7 @@ LLM_MAX_RETRIES=1 LLM_TEMPERATURE=0.7 LLM_MAX_TOKENS=2048 -# Rate limits -RATE_LIMIT_LOGIN=5 +# Rate limits (login uses progressive lockout: 5/10/20 failed attempts -> 15m/1h/24h block) RATE_LIMIT_REGISTER=3 RATE_LIMIT_FORGOT_PASSWORD=3 RATE_LIMIT_VERIFY_EMAIL=5 diff --git a/src/config/env.ts b/src/config/env.ts index fb0c5bd..e01945f 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -22,7 +22,6 @@ const envSchema = z.object({ LLM_TEMPERATURE: z.coerce.number().min(0).max(2).default(0.7), LLM_MAX_TOKENS: z.coerce.number().default(2048), - RATE_LIMIT_LOGIN: z.coerce.number().default(5), RATE_LIMIT_REGISTER: z.coerce.number().default(3), RATE_LIMIT_FORGOT_PASSWORD: z.coerce.number().default(3), RATE_LIMIT_VERIFY_EMAIL: z.coerce.number().default(5), diff --git a/src/plugins/rateLimit.ts b/src/plugins/rateLimit.ts index 41bb9f2..1b97f95 100644 --- a/src/plugins/rateLimit.ts +++ b/src/plugins/rateLimit.ts @@ -6,7 +6,6 @@ import { env } from '../config/env.js'; declare module 'fastify' { interface FastifyInstance { rateLimitOptions: { - login: { max: number; timeWindow: string }; register: { max: number; timeWindow: string }; forgotPassword: { max: number; timeWindow: string }; verifyEmail: { max: number; timeWindow: string }; @@ -18,7 +17,6 @@ declare module 'fastify' { const rateLimitPlugin: FastifyPluginAsync = async (app: FastifyInstance) => { const options = { - login: { max: env.RATE_LIMIT_LOGIN, timeWindow: '15 minutes' }, register: { max: env.RATE_LIMIT_REGISTER, timeWindow: '1 hour' }, forgotPassword: { max: env.RATE_LIMIT_FORGOT_PASSWORD, timeWindow: '1 hour' }, verifyEmail: { max: env.RATE_LIMIT_VERIFY_EMAIL, timeWindow: '15 minutes' }, diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 9b351ca..c987f19 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -1,5 +1,6 @@ import type { FastifyInstance } from 'fastify'; import { AuthService } from '../services/auth/auth.service.js'; +import { checkBlocked, clearOnSuccess, recordFailedAttempt } from '../utils/loginLockout.js'; const registerSchema = { body: { @@ -89,20 +90,43 @@ export async function authRoutes(app: FastifyInstance) { app.post( '/login', - { schema: loginSchema, config: { rateLimit: rateLimitOptions.login } }, + { + schema: loginSchema, + preValidation: async (req, reply) => { + const ip = req.ip ?? 'unknown'; + const { blocked, retryAfter } = await checkBlocked(app.redis, ip); + if (blocked) { + if (retryAfter !== undefined) { + reply.header('Retry-After', String(retryAfter)); + } + return reply.status(429).send({ + error: { + code: 'RATE_LIMIT_EXCEEDED', + message: 'Too many failed login attempts. Please try again later.', + retryAfter, + }, + }); + } + }, + }, async (req, reply) => { const body = req.body as { email: string; password: string }; const userAgent = req.headers['user-agent']; - const ipAddress = req.ip; + const ip = req.ip ?? 'unknown'; - const result = await authService.login({ - email: body.email, - password: body.password, - userAgent, - ipAddress, - }); - - return reply.send(result); + try { + const result = await authService.login({ + email: body.email, + password: body.password, + userAgent, + ipAddress: ip, + }); + await clearOnSuccess(app.redis, ip); + return reply.send(result); + } catch (err) { + await recordFailedAttempt(app.redis, ip); + throw err; + } }, ); diff --git a/src/utils/loginLockout.ts b/src/utils/loginLockout.ts new file mode 100644 index 0000000..ad702b5 --- /dev/null +++ b/src/utils/loginLockout.ts @@ -0,0 +1,106 @@ +import type { Redis } from 'ioredis'; + +const WINDOW_15M_SEC = 15 * 60; // 900 +const WINDOW_1H_SEC = 60 * 60; // 3600 +const WINDOW_24H_SEC = 24 * 60 * 60; // 86400 + +const THRESHOLD_15M = 5; +const THRESHOLD_1H = 10; +const THRESHOLD_24H = 20; + +const BLOCK_15M_SEC = WINDOW_15M_SEC; +const BLOCK_1H_SEC = WINDOW_1H_SEC; +const BLOCK_24H_SEC = WINDOW_24H_SEC; + +const KEY_PREFIX = 'lockout'; + +function key15m(ip: string): string { + return `${KEY_PREFIX}:15m:${ip}`; +} +function key1h(ip: string): string { + return `${KEY_PREFIX}:1h:${ip}`; +} +function key24h(ip: string): string { + return `${KEY_PREFIX}:24h:${ip}`; +} +function keyBlocked(ip: string): string { + return `${KEY_PREFIX}:blocked:${ip}`; +} + +/** + * Check if the IP is currently blocked due to progressive login lockout. + * @returns { blocked: true, retryAfter } if blocked, { blocked: false } otherwise + */ +export async function checkBlocked( + redis: Redis, + ip: string +): Promise<{ blocked: boolean; retryAfter?: number }> { + const blockedKey = keyBlocked(ip); + const ttl = await redis.ttl(blockedKey); + if (ttl > 0) { + return { blocked: true, retryAfter: ttl }; + } + return { blocked: false }; +} + +const RECORD_SCRIPT = ` + local c15 = redis.call('INCR', KEYS[1]) + if redis.call('TTL', KEYS[1]) == -1 then redis.call('EXPIRE', KEYS[1], ARGV[1]) end + local c1h = redis.call('INCR', KEYS[2]) + if redis.call('TTL', KEYS[2]) == -1 then redis.call('EXPIRE', KEYS[2], ARGV[2]) end + local c24 = redis.call('INCR', KEYS[3]) + if redis.call('TTL', KEYS[3]) == -1 then redis.call('EXPIRE', KEYS[3], ARGV[3]) end + return {c15, c1h, c24} +`; + +/** + * Record a failed login attempt. Increments counters and sets blocked key when thresholds are reached. + * Thresholds: 5 in 15m -> 15m block; 10 in 1h -> 1h block; 20 in 24h -> 24h block. + */ +export async function recordFailedAttempt(redis: Redis, ip: string): Promise { + const k15 = key15m(ip); + const k1h = key1h(ip); + const k24 = key24h(ip); + const kBlocked = keyBlocked(ip); + + const counts = (await redis.eval( + RECORD_SCRIPT, + 3, + k15, + k1h, + k24, + String(WINDOW_15M_SEC), + String(WINDOW_1H_SEC), + String(WINDOW_24H_SEC) + )) as number[]; + + const count15m = counts[0] ?? 0; + const count1h = counts[1] ?? 0; + const count24h = counts[2] ?? 0; + + let blockTtl = 0; + if (count24h >= THRESHOLD_24H) { + blockTtl = BLOCK_24H_SEC; + } else if (count1h >= THRESHOLD_1H) { + blockTtl = BLOCK_1H_SEC; + } else if (count15m >= THRESHOLD_15M) { + blockTtl = BLOCK_15M_SEC; + } + + if (blockTtl > 0) { + await redis.setex(kBlocked, blockTtl, '1'); + } +} + +/** + * Clear all lockout counters and blocked state on successful login. + */ +export async function clearOnSuccess(redis: Redis, ip: string): Promise { + const keys = [ + key15m(ip), + key1h(ip), + key24h(ip), + keyBlocked(ip), + ]; + await redis.del(...keys); +}