7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.
TypeScript
fix(auth): Retry-After on the auth 429s (login lockout, reset-code cooldown) (#1341)
tashdroiddc4c608ffa57
diff --git a/server/src/__tests__/routes/auth-login-throttle.test.ts b/server/src/__tests__/routes/auth-login-throttle.test.tsindex 1d235c94..9a4b286b 100644--- a/server/src/__tests__/routes/auth-login-throttle.test.ts+++ b/server/src/__tests__/routes/auth-login-throttle.test.ts@@ -69,6 +69,21 @@ describe('login brute-force throttling', () => {// brute force as the reason. Express runs middleware in registration order,// so the limiter has to be registered before the /api/auth mount or the// login route answers and ends the response before the limiter is entered.+ // RFC 6585: a 429 must carry Retry-After. The gateway's other 429s (proxy+ // limiter, key exhaustion, monthly budget) all do; the per-email lockout+ // was the one that told the client nothing about when to come back.+ it('lockout 429 carries a Retry-After within the 15-minute window', async () => {+ const EMAIL2 = '[email protected]';+ for (let i = 0; i < 5; i++) {+ expect((await login(EMAIL2, `wrong-${i}`)).status).toBe(401);+ }+ const locked = await login(EMAIL2, 'wrong');+ expect(locked.status).toBe(429);+ const retryAfter = Number(locked.headers.get('retry-after'));+ expect(retryAfter).toBeGreaterThan(0);+ expect(retryAfter).toBeLessThanOrEqual(15 * 60);+ });+it('routes login through the per-IP admin limiter', async () => {// A fresh address, so the lockout the previous test armed (the bucket map// is module state and outlives the DB reset) cannot mask the header check.diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.tsindex 88ee7ca6..238a15f5 100644--- a/server/src/routes/auth.ts+++ b/server/src/routes/auth.ts@@ -66,6 +66,14 @@ function isLockedOut(email: string): boolean {const a = attempts.get(throttleKey(email));return !!a && a.lockedUntil > Date.now();}+// Seconds until the per-email lockout lifts (0 when not locked). The 429+// carries this as Retry-After so a client (or a scripted login retry loop)+// backs off for the ACTUAL remaining time instead of the worst-case 15 min.+function lockoutRetryAfterSec(email: string, now = Date.now()): number {+ const a = attempts.get(throttleKey(email));+ if (!a || a.lockedUntil <= now) return 0;+ return Math.max(1, Math.ceil((a.lockedUntil - now) / 1000));+}function recordFailure(email: string): void {const key = throttleKey(email);const a = attempts.get(key) ?? { count: 0, lockedUntil: 0 };@@ -176,6 +184,10 @@ authRouter.post('/login', (req: Request, res: Response) => {const { email, password } = parsed.data;if (isLockedOut(email)) {+ // RFC 6585: a 429 must say when the client may return. The whole gateway+ // already does this (proxy limiter, exhaustion Retry-After, monthly budget+ // cap); the dashboard's own lockout was the one 429 that didn't.+ res.setHeader('Retry-After', String(lockoutRetryAfterSec(email)));res.status(429).json({ error: { message: 'Too many attempts. Wait 15 minutes or restart the app.', type: 'rate_limit_error' } });return;}@@ -294,6 +306,7 @@ authRouter.post('/forgot-password', (_req: Request, res: Response) => {}const now = Date.now();if (now - lastResetCodeAt < RESET_CODE_MIN_INTERVAL_MS) {+ res.setHeader('Retry-After', String(Math.max(1, Math.ceil((RESET_CODE_MIN_INTERVAL_MS - (now - lastResetCodeAt)) / 1000))));res.status(429).json({ error: { message: 'Too many reset-code requests. Try again later.', type: 'rate_limit_error' } });return;}