Skip to content

Email for professionals and teams

fix(auth): key login rate limits by real client IP behind proxies

ssthormess1d36335f7d86
diff --git a/src/lib/auth/rate-limit.ts b/src/lib/auth/rate-limit.ts
index ea94f52..83f89a2 100644
--- a/src/lib/auth/rate-limit.ts
+++ b/src/lib/auth/rate-limit.ts
@@ -1,10 +1,24 @@
+/**
+ * Client IP for rate-limit keys. On Workers, cf-connecting-ip is set by the
+ * platform; self-hosted sits behind a reverse proxy, so fall back to the
+ * proxy-set x-forwarded-for first hop. Without a real per-client key every
+ * visitor lands in one shared "unknown" bucket and scanners can lock out
+ * legitimate logins.
+ */
+function clientRateLimitKey(request: Request): string {
+ return (
+ request.headers.get("cf-connecting-ip")?.trim() ||
+ request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||
+ "unknown"
+ );
+}
+
/** Mobile sign-ins have their own, stricter budget (they skip Turnstile). */
export async function allowDeviceLoginAttempt(env: CloudflareEnv, request: Request): Promise<boolean> {
const limiter = env.DEVICE_LOGIN_RATE_LIMIT ?? env.LOGIN_RATE_LIMIT;
if (!limiter) return true;
- const ip = request.headers.get("cf-connecting-ip")?.trim() || "unknown";
try {
- return (await limiter.limit({ key: `device:${ip}` })).success;
+ return (await limiter.limit({ key: `device:${clientRateLimitKey(request)}` })).success;
} catch (error) {
console.warn("Device login rate limiter unavailable", error);
return true;
@@ -13,9 +27,8 @@ export async function allowDeviceLoginAttempt(env: CloudflareEnv, request: Reque
export async function allowLoginAttempt(env: CloudflareEnv, request: Request): Promise<boolean> {
if (!env.LOGIN_RATE_LIMIT) return true;
- const ip = request.headers.get("cf-connecting-ip")?.trim() || "unknown";
try {
- const outcome = await env.LOGIN_RATE_LIMIT.limit({ key: ip });
+ const outcome = await env.LOGIN_RATE_LIMIT.limit({ key: clientRateLimitKey(request) });
return outcome.success;
} catch (error) {
console.warn("Login rate limiter unavailable", error);