import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { cache } from "~/utils/cache.server"; import { checkAuthRateLimit, getAuthRateLimitKey } from "./auth-rate-limit.server"; describe("auth rate limiting", () => { beforeEach(() => { cache.clear(); vi.spyOn(Date, "now").mockReturnValue(new Date("2026-05-25T12:00:00.000Z").getTime()); }); afterEach(() => { vi.restoreAllMocks(); cache.clear(); }); it("builds stable keys from action, client ip, and normalized identifier", () => { const request = new Request("https://example.test/login", { headers: { "cf-connecting-ip": "203.0.113.10" }, }); expect(getAuthRateLimitKey({ request, action: "login", identifier: " Ada@Example.COM " })).toBe( getAuthRateLimitKey({ request, action: "login", identifier: "ada@example.com" }) ); expect(getAuthRateLimitKey({ request, action: "login", identifier: "ada@example.com" })).not.toBe( getAuthRateLimitKey({ request, action: "forgot-password", identifier: "ada@example.com" }) ); }); it("does not key on a spoofable X-Forwarded-For / X-Client-IP", () => { // Both spoofable headers must be ignored, so a forged value cannot move the // request into a fresh rate-limit bucket. Both requests collapse to the same // (untrusted -> "unknown") key. const forged = new Request("https://example.test/login", { headers: { "x-forwarded-for": "9.9.9.9", "x-client-ip": "8.8.8.8" } }); const bare = new Request("https://example.test/login", {}); expect(getAuthRateLimitKey({ request: forged, action: "login", identifier: "ada@example.com" })).toBe( getAuthRateLimitKey({ request: bare, action: "login", identifier: "ada@example.com" }) ); }); it("allows requests up to the limit and returns retry timing after the limit", () => { const request = new Request("https://example.test/login", { headers: { "cf-connecting-ip": "203.0.113.10" }, }); const options = { request, action: "login" as const, identifier: "ada@example.com", limit: 2, windowMs: 30_000 }; expect(checkAuthRateLimit(options)).toEqual({ allowed: true }); expect(checkAuthRateLimit(options)).toEqual({ allowed: true }); expect(checkAuthRateLimit(options)).toEqual({ allowed: false, retryAfterSeconds: 30 }); }); it("starts a new window after the cached counter expires", () => { const request = new Request("https://example.test/mfa", { headers: { "cf-connecting-ip": "203.0.113.10" }, }); const options = { request, action: "mfa" as const, identifier: "ada@example.com", limit: 1, windowMs: 10_000 }; expect(checkAuthRateLimit(options)).toEqual({ allowed: true }); expect(checkAuthRateLimit(options)).toEqual({ allowed: false, retryAfterSeconds: 10 }); vi.mocked(Date.now).mockReturnValue(new Date("2026-05-25T12:00:11.000Z").getTime()); expect(checkAuthRateLimit(options)).toEqual({ allowed: true }); }); });