diff --git a/apps/web/__tests__/unit/collection-password-verify.test.ts b/apps/web/__tests__/unit/collection-password-verify.test.ts new file mode 100644 index 00000000000..a6b0a91b321 --- /dev/null +++ b/apps/web/__tests__/unit/collection-password-verify.test.ts @@ -0,0 +1,191 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const mocks = vi.hoisted(() => ({ + headers: vi.fn(), + checkRateLimit: vi.fn(), + revalidatePath: vi.fn(), + setVerifiedPasswordCookie: vi.fn(), + verifyPassword: vi.fn(), + getPublicCollectionPasswordHash: vi.fn(), +})); + +vi.mock("next/headers", () => ({ headers: mocks.headers })); +vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath })); +vi.mock("@vercel/firewall", () => ({ checkRateLimit: mocks.checkRateLimit })); +vi.mock("@cap/database/crypto", () => ({ + verifyPassword: mocks.verifyPassword, +})); +vi.mock("@/lib/password-cookie", () => ({ + setVerifiedPasswordCookie: mocks.setVerifiedPasswordCookie, +})); +vi.mock("@/lib/public-collections", () => ({ + getPublicCollectionPasswordHash: mocks.getPublicCollectionPasswordHash, +})); + +const TOO_MANY = { + success: false, + error: "Too many attempts. Please try again later.", +}; +const WRONG = { success: false, error: "Failed to verify password" }; +const VERIFIED = { success: true, value: "Password verified" }; + +const loadAction = async () => { + vi.resetModules(); + return (await import("@/actions/collections/password")) + .verifyCollectionPassword; +}; + +const fromIp = (ip: string) => + mocks.headers.mockResolvedValue(new Headers({ "x-real-ip": ip })); + +beforeEach(() => { + mocks.headers.mockReset(); + mocks.checkRateLimit.mockReset(); + mocks.revalidatePath.mockReset(); + mocks.setVerifiedPasswordCookie.mockReset(); + mocks.verifyPassword + .mockReset() + .mockImplementation( + async (hash: string, password: string) => hash === `hash:${password}`, + ); + mocks.getPublicCollectionPasswordHash + .mockReset() + .mockImplementation(async (id: string) => + id === "collection-open" ? null : `hash:${id}-secret`, + ); + vi.spyOn(console, "warn").mockImplementation(() => {}); + vi.stubEnv("VERCEL", "1"); + fromIp("203.0.113.1"); +}); + +afterEach(() => { + vi.unstubAllEnvs(); +}); + +describe("verifyCollectionPassword attempt limits", () => { + it("verifies a correct password and sets the cookie", async () => { + const verifyCollectionPassword = await loadAction(); + + expect( + await verifyCollectionPassword("collection-1", "collection-1-secret"), + ).toEqual(VERIFIED); + expect(mocks.setVerifiedPasswordCookie).toHaveBeenCalledWith( + "hash:collection-1-secret", + ); + expect(mocks.revalidatePath).toHaveBeenCalledWith("/c/collection-1"); + }); + + it("stops verifying after repeated attempts from one client", async () => { + const verifyCollectionPassword = await loadAction(); + + for (let attempt = 0; attempt < 20; attempt++) { + expect(await verifyCollectionPassword("collection-1", "nope")).toEqual( + WRONG, + ); + } + mocks.verifyPassword.mockClear(); + mocks.getPublicCollectionPasswordHash.mockClear(); + + expect( + await verifyCollectionPassword("collection-1", "collection-1-secret"), + ).toEqual(TOO_MANY); + expect(mocks.getPublicCollectionPasswordHash).not.toHaveBeenCalled(); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + expect(mocks.setVerifiedPasswordCookie).not.toHaveBeenCalled(); + }); + + it("stops verifying one collection after many attempts across clients", async () => { + const verifyCollectionPassword = await loadAction(); + + for (let client = 0; client < 10; client++) { + fromIp(`198.51.100.${client}`); + for (let attempt = 0; attempt < 20; attempt++) { + await verifyCollectionPassword("collection-1", "nope"); + } + } + mocks.verifyPassword.mockClear(); + + fromIp("192.0.2.200"); + expect( + await verifyCollectionPassword("collection-1", "collection-1-secret"), + ).toEqual(TOO_MANY); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + expect(mocks.setVerifiedPasswordCookie).not.toHaveBeenCalled(); + + expect( + await verifyCollectionPassword("collection-2", "collection-2-secret"), + ).toEqual(VERIFIED); + }); + + it("shares the per-collection budget across id casing", async () => { + mocks.getPublicCollectionPasswordHash.mockResolvedValue( + "hash:collection-1-secret", + ); + const verifyCollectionPassword = await loadAction(); + + for (let client = 0; client < 10; client++) { + fromIp(`198.51.100.${client}`); + for (let attempt = 0; attempt < 20; attempt++) { + await verifyCollectionPassword( + attempt % 2 === 0 ? "collection-1" : "COLLECTION-1", + "nope", + ); + } + } + mocks.verifyPassword.mockClear(); + + fromIp("192.0.2.200"); + expect( + await verifyCollectionPassword("Collection-1", "collection-1-secret"), + ).toEqual(TOO_MANY); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + }); + + it("rejects ids outside the id alphabet before any lookup", async () => { + const verifyCollectionPassword = await loadAction(); + + expect( + await verifyCollectionPassword("collection-1 ", "collection-1-secret"), + ).toEqual(WRONG); + expect(mocks.getPublicCollectionPasswordHash).not.toHaveBeenCalled(); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + }); + + it("does not count attempts rejected per client against the collection", async () => { + const verifyCollectionPassword = await loadAction(); + + for (let attempt = 0; attempt < 500; attempt++) { + await verifyCollectionPassword("collection-1", "nope"); + } + + fromIp("192.0.2.200"); + expect( + await verifyCollectionPassword("collection-1", "collection-1-secret"), + ).toEqual(VERIFIED); + }); + + it("keeps the generic failure without logging errors for a missing hash", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}); + const verifyCollectionPassword = await loadAction(); + + expect(await verifyCollectionPassword("collection-open", "x")).toEqual( + WRONG, + ); + expect(error).not.toHaveBeenCalled(); + }); + + it("limits attempts in production when the firewall check throws", async () => { + vi.stubEnv("NODE_ENV", "production"); + mocks.checkRateLimit.mockRejectedValue( + new Error("Could not determine rate limit key."), + ); + const verifyCollectionPassword = await loadAction(); + + for (let attempt = 0; attempt < 20; attempt++) { + await verifyCollectionPassword("collection-1", "nope"); + } + expect( + await verifyCollectionPassword("collection-1", "collection-1-secret"), + ).toEqual(TOO_MANY); + }); +}); diff --git a/apps/web/__tests__/unit/share-password-gate-errors.test.ts b/apps/web/__tests__/unit/share-password-gate-errors.test.ts new file mode 100644 index 00000000000..bd7f8dfae04 --- /dev/null +++ b/apps/web/__tests__/unit/share-password-gate-errors.test.ts @@ -0,0 +1,108 @@ +// @vitest-environment jsdom + +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { act, createElement, type ReactNode } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { PasswordGate } from "@/app/s/[videoId]/_components/PasswordGate"; + +const mocks = vi.hoisted(() => ({ + verifyVideoPassword: vi.fn(), + refresh: vi.fn(), +})); + +vi.mock("@/actions/videos/password", () => ({ + verifyVideoPassword: mocks.verifyVideoPassword, +})); +vi.mock("next/navigation", () => ({ + useRouter: () => ({ refresh: mocks.refresh }), +})); +vi.mock("@cap/env", () => ({ + buildEnv: { NEXT_PUBLIC_WEB_URL: "https://cap.example" }, +})); +vi.mock("@/components/paper-state/doodles", () => ({ + PasswordDoodle: () => null, +})); +vi.mock("@/components/paper-state/PaperState", () => ({ + PaperState: ({ children }: { children: ReactNode }) => + createElement("div", null, children), + paperStateStyles: new Proxy({}, { get: (_, key) => String(key) }), +})); + +let root: Root; +let container: HTMLDivElement; +let queryClient: QueryClient; + +beforeEach(() => { + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + mocks.verifyVideoPassword.mockReset(); + container = document.createElement("div"); + document.body.append(container); + root = createRoot(container); + queryClient = new QueryClient({ + defaultOptions: { mutations: { retry: false } }, + }); +}); + +afterEach(async () => { + await act(async () => root.unmount()); + container.remove(); + queryClient.clear(); + vi.unstubAllGlobals(); +}); + +async function submitPassword(password: string) { + await act(async () => { + root.render( + createElement( + QueryClientProvider, + { client: queryClient }, + createElement(PasswordGate, { videoId: "video-1" as never }), + ), + ); + }); + const input = container.querySelector( + "input[type='password']", + ); + const setter = Object.getOwnPropertyDescriptor( + HTMLInputElement.prototype, + "value", + )?.set; + if (!input || !setter) throw new Error("Missing password input"); + await act(async () => { + setter.call(input, password); + input.dispatchEvent(new Event("input", { bubbles: true })); + }); + await act(async () => { + container.querySelector("form")?.requestSubmit(); + }); + await act(async () => { + await new Promise((resolve) => setTimeout(resolve, 0)); + }); + return container.querySelector("[role='alert']")?.textContent; +} + +describe("PasswordGate errors", () => { + it("keeps the generic message for a wrong password", async () => { + mocks.verifyVideoPassword.mockResolvedValue({ + success: false, + error: "Failed to verify password", + }); + + expect(await submitPassword("nope")).toBe( + "That password didn't work. Check it and try again.", + ); + }); + + it("tells the viewer to wait when attempts are limited", async () => { + mocks.verifyVideoPassword.mockResolvedValue({ + success: false, + error: "Too many attempts. Please try again later.", + }); + + expect(await submitPassword("open")).toBe( + "Too many attempts. Please try again later.", + ); + expect(mocks.refresh).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/web/__tests__/unit/video-password-verify.test.ts b/apps/web/__tests__/unit/video-password-verify.test.ts new file mode 100644 index 00000000000..94ec43c1b14 --- /dev/null +++ b/apps/web/__tests__/unit/video-password-verify.test.ts @@ -0,0 +1,219 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const mocks = vi.hoisted(() => ({ + headers: vi.fn(), + checkRateLimit: vi.fn(), + setVerifiedPasswordCookie: vi.fn(), + verifyPassword: vi.fn(), + videoRows: vi.fn(), + spacePasswordRows: vi.fn(), +})); + +vi.mock("next/headers", () => ({ headers: mocks.headers })); +vi.mock("next/cache", () => ({ revalidatePath: vi.fn() })); +vi.mock("@vercel/firewall", () => ({ checkRateLimit: mocks.checkRateLimit })); +vi.mock("@cap/database/auth/session", () => ({ getCurrentUser: vi.fn() })); +vi.mock("@cap/database/crypto", () => ({ + hashPassword: vi.fn(), + verifyPassword: mocks.verifyPassword, +})); +vi.mock("@cap/database/schema", () => ({ + spaces: { password: "spaces.password", id: "spaces.id" }, + spaceVideos: { + spaceId: "spaceVideos.spaceId", + videoId: "spaceVideos.videoId", + }, + videos: { id: "videos.id" }, +})); +vi.mock("@cap/database", () => ({ + db: () => ({ + select: () => ({ + from: () => ({ + where: () => mocks.videoRows(), + innerJoin: () => ({ where: () => mocks.spacePasswordRows() }), + }), + }), + }), +})); +vi.mock("@cap/web-backend", () => ({ + collectPasswordHashes: ({ + videoPassword, + spacePasswords, + }: { + videoPassword?: string | null; + spacePasswords: Array<{ password?: string | null }>; + }) => + [videoPassword, ...spacePasswords.map(({ password }) => password)].filter( + (hash): hash is string => typeof hash === "string", + ), +})); +vi.mock("@/lib/password-cookie", () => ({ + setVerifiedPasswordCookie: mocks.setVerifiedPasswordCookie, +})); + +const TOO_MANY = { + success: false, + error: "Too many attempts. Please try again later.", +}; +const WRONG = { success: false, error: "Failed to verify password" }; +const VERIFIED = { success: true, value: "Password verified" }; + +const loadAction = async () => { + vi.resetModules(); + return (await import("@/actions/videos/password")).verifyVideoPassword; +}; + +const fromIp = (ip: string) => + mocks.headers.mockResolvedValue(new Headers({ "x-real-ip": ip })); + +beforeEach(() => { + mocks.headers.mockReset(); + mocks.setVerifiedPasswordCookie.mockReset(); + mocks.verifyPassword + .mockReset() + .mockImplementation( + async (hash: string, password: string) => hash === `hash:${password}`, + ); + mocks.videoRows + .mockReset() + .mockImplementation(async () => [{ id: "video-1", password: "hash:open" }]); + mocks.spacePasswordRows.mockReset().mockResolvedValue([]); + vi.spyOn(console, "warn").mockImplementation(() => {}); + vi.stubEnv("VERCEL", "1"); + fromIp("203.0.113.1"); +}); + +afterEach(() => { + vi.unstubAllEnvs(); +}); + +describe("verifyVideoPassword attempt limits", () => { + it("verifies a correct password and sets the cookie", async () => { + const verifyVideoPassword = await loadAction(); + + expect(await verifyVideoPassword("video-1" as never, "open")).toEqual( + VERIFIED, + ); + expect(mocks.setVerifiedPasswordCookie).toHaveBeenCalledWith("hash:open"); + }); + + it("stops verifying after repeated attempts from one client", async () => { + const verifyVideoPassword = await loadAction(); + + for (let attempt = 0; attempt < 20; attempt++) { + expect(await verifyVideoPassword("video-1" as never, "nope")).toEqual( + WRONG, + ); + } + mocks.verifyPassword.mockClear(); + mocks.videoRows.mockClear(); + + expect(await verifyVideoPassword("video-1" as never, "open")).toEqual( + TOO_MANY, + ); + expect(mocks.videoRows).not.toHaveBeenCalled(); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + expect(mocks.setVerifiedPasswordCookie).not.toHaveBeenCalled(); + + fromIp("203.0.113.2"); + expect(await verifyVideoPassword("video-1" as never, "open")).toEqual( + VERIFIED, + ); + }); + + it("stops verifying one video after many attempts across clients", async () => { + const verifyVideoPassword = await loadAction(); + + for (let client = 0; client < 10; client++) { + fromIp(`198.51.100.${client}`); + for (let attempt = 0; attempt < 20; attempt++) { + await verifyVideoPassword("video-1" as never, "nope"); + } + } + mocks.verifyPassword.mockClear(); + + fromIp("192.0.2.200"); + expect(await verifyVideoPassword("video-1" as never, "open")).toEqual( + TOO_MANY, + ); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + expect(mocks.setVerifiedPasswordCookie).not.toHaveBeenCalled(); + + mocks.videoRows.mockResolvedValue([ + { id: "video-2", password: "hash:other" }, + ]); + expect(await verifyVideoPassword("video-2" as never, "other")).toEqual( + VERIFIED, + ); + }); + + it("shares the per-video budget across id casing", async () => { + const verifyVideoPassword = await loadAction(); + + for (let client = 0; client < 10; client++) { + fromIp(`198.51.100.${client}`); + for (let attempt = 0; attempt < 20; attempt++) { + await verifyVideoPassword( + (attempt % 2 === 0 ? "video-1" : "VIDEO-1") as never, + "nope", + ); + } + } + mocks.verifyPassword.mockClear(); + + fromIp("192.0.2.200"); + expect(await verifyVideoPassword("Video-1" as never, "open")).toEqual( + TOO_MANY, + ); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + }); + + it("rejects ids outside the id alphabet before any lookup", async () => { + const verifyVideoPassword = await loadAction(); + + expect(await verifyVideoPassword("video-1 " as never, "open")).toEqual( + WRONG, + ); + expect(mocks.videoRows).not.toHaveBeenCalled(); + expect(mocks.verifyPassword).not.toHaveBeenCalled(); + }); + + it("does not count attempts rejected per client against the video", async () => { + const verifyVideoPassword = await loadAction(); + + for (let attempt = 0; attempt < 500; attempt++) { + await verifyVideoPassword("video-1" as never, "nope"); + } + + fromIp("192.0.2.200"); + expect(await verifyVideoPassword("video-1" as never, "open")).toEqual( + VERIFIED, + ); + }); + + it("keeps the generic failure for videos without a password", async () => { + mocks.videoRows.mockResolvedValue([{ id: "video-3", password: null }]); + const verifyVideoPassword = await loadAction(); + + expect(await verifyVideoPassword("video-3" as never, "anything")).toEqual( + WRONG, + ); + expect(mocks.setVerifiedPasswordCookie).not.toHaveBeenCalled(); + }); + + it("limits attempts in production when the firewall rule is missing", async () => { + vi.stubEnv("NODE_ENV", "production"); + mocks.checkRateLimit.mockResolvedValue({ + rateLimited: false, + error: "not-found", + }); + const verifyVideoPassword = await loadAction(); + + for (let attempt = 0; attempt < 20; attempt++) { + await verifyVideoPassword("video-1" as never, "nope"); + } + expect(await verifyVideoPassword("video-1" as never, "open")).toEqual( + TOO_MANY, + ); + }); +}); diff --git a/apps/web/actions/collections/password.ts b/apps/web/actions/collections/password.ts index 7a77f82b3df..ba6644f78c9 100644 --- a/apps/web/actions/collections/password.ts +++ b/apps/web/actions/collections/password.ts @@ -1,39 +1,14 @@ "use server"; import { verifyPassword as verifyPlainPassword } from "@cap/database/crypto"; -import { NODE_ENV } from "@cap/env"; -import { checkRateLimit } from "@vercel/firewall"; import { revalidatePath } from "next/cache"; -import { headers } from "next/headers"; +import { + passwordAttemptResourceKey, + TOO_MANY_PASSWORD_ATTEMPTS, +} from "@/lib/password-attempts"; import { setVerifiedPasswordCookie } from "@/lib/password-cookie"; import { getPublicCollectionPasswordHash } from "@/lib/public-collections"; - -const COLLECTION_PASSWORD_RATE_LIMIT_ID = "rl_collection_password"; - -/** Per-IP throttle so the public action isn't an open brute-force oracle. */ -async function isRateLimited() { - if (NODE_ENV !== "production") return false; - - try { - const headersList = await headers(); - const request = new Request("https://cap.so/api/collection-password", { - method: "POST", - headers: headersList, - }); - - const { rateLimited } = await checkRateLimit( - COLLECTION_PASSWORD_RATE_LIMIT_ID, - { request }, - ); - return rateLimited; - } catch (error) { - // Best-effort: self-hosted deploys without the Vercel firewall (or an - // x-real-ip header) must not lose password verification entirely; the - // PBKDF2 verification cost still slows brute force. - console.warn("Collection password rate limit check failed:", error); - return false; - } -} +import { isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit"; export async function verifyCollectionPassword( collectionId: string, @@ -44,11 +19,18 @@ export async function verifyCollectionPassword( return { success: false, error: "Failed to verify password" }; } - if (await isRateLimited()) { - return { - success: false, - error: "Too many attempts. Please try again later.", - }; + const resourceKey = passwordAttemptResourceKey(collectionId); + if (!resourceKey) { + return { success: false, error: "Failed to verify password" }; + } + + if ( + (await isRateLimited(RATE_LIMIT_IDS.COLLECTION_PASSWORD)) || + (await isRateLimited(RATE_LIMIT_IDS.COLLECTION_PASSWORD_PER_COLLECTION, { + key: `collection:${resourceKey}`, + })) + ) { + return { success: false, error: TOO_MANY_PASSWORD_ATTEMPTS }; } // Missing hash and wrong password are expected outcomes (typos, links to diff --git a/apps/web/actions/videos/password.ts b/apps/web/actions/videos/password.ts index f8c8dc81d6c..7a45bd1b8d0 100644 --- a/apps/web/actions/videos/password.ts +++ b/apps/web/actions/videos/password.ts @@ -11,7 +11,12 @@ import { collectPasswordHashes } from "@cap/web-backend"; import type { Video } from "@cap/web-domain"; import { eq } from "drizzle-orm"; import { revalidatePath } from "next/cache"; +import { + passwordAttemptResourceKey, + TOO_MANY_PASSWORD_ATTEMPTS, +} from "@/lib/password-attempts"; import { setVerifiedPasswordCookie } from "@/lib/password-cookie"; +import { isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit"; export async function setVideoPassword( videoId: Video.VideoId, @@ -91,6 +96,20 @@ export async function verifyVideoPassword( if (!videoId || typeof password !== "string") return { success: false, error: "Failed to verify password" }; + const resourceKey = passwordAttemptResourceKey(videoId); + if (!resourceKey) { + return { success: false, error: "Failed to verify password" }; + } + + if ( + (await isRateLimited(RATE_LIMIT_IDS.VIDEO_PASSWORD)) || + (await isRateLimited(RATE_LIMIT_IDS.VIDEO_PASSWORD_PER_VIDEO, { + key: `video:${resourceKey}`, + })) + ) { + return { success: false, error: TOO_MANY_PASSWORD_ATTEMPTS }; + } + const [video] = await db() .select() .from(videos) diff --git a/apps/web/app/s/[videoId]/_components/PasswordGate.tsx b/apps/web/app/s/[videoId]/_components/PasswordGate.tsx index c6b6406f296..f1c2b413208 100644 --- a/apps/web/app/s/[videoId]/_components/PasswordGate.tsx +++ b/apps/web/app/s/[videoId]/_components/PasswordGate.tsx @@ -11,6 +11,7 @@ import { PaperState, paperStateStyles as styles, } from "@/components/paper-state/PaperState"; +import { TOO_MANY_PASSWORD_ATTEMPTS } from "@/lib/password-attempts"; export function PasswordGate({ videoId }: { videoId: Video.VideoId }) { const [password, setPassword] = useState(""); @@ -66,7 +67,9 @@ export function PasswordGate({ videoId }: { videoId: Video.VideoId }) { {failed && (

- That password didn't work. Check it and try again. + {verifyPassword.error.message === TOO_MANY_PASSWORD_ATTEMPTS + ? TOO_MANY_PASSWORD_ATTEMPTS + : "That password didn't work. Check it and try again."}

)} diff --git a/apps/web/lib/password-attempts.ts b/apps/web/lib/password-attempts.ts new file mode 100644 index 00000000000..18dd64ad702 --- /dev/null +++ b/apps/web/lib/password-attempts.ts @@ -0,0 +1,8 @@ +export const TOO_MANY_PASSWORD_ATTEMPTS = + "Too many attempts. Please try again later."; + +const PASSWORD_RESOURCE_ID = /^[0-9A-Za-z_-]{1,64}$/; + +export function passwordAttemptResourceKey(id: string): string | null { + return PASSWORD_RESOURCE_ID.test(id) ? id.toLowerCase() : null; +} diff --git a/apps/web/lib/rate-limit.ts b/apps/web/lib/rate-limit.ts index fe9889b6190..f39a1b8ec18 100644 --- a/apps/web/lib/rate-limit.ts +++ b/apps/web/lib/rate-limit.ts @@ -130,6 +130,14 @@ export const RATE_LIMIT_IDS = { DOCS_ASK: "rl_docs_ask", AUTH_SSO_START: "rl_auth_sso_start", MCP_CLIENT_REGISTRATION: "rl_mcp_client_registration", + /** Share-page video password attempts. Suggested: 20 / 10m per IP. */ + VIDEO_PASSWORD: "rl_video_password", + /** Video password attempts across all clients. Suggested: 200 / 10m per key (video). */ + VIDEO_PASSWORD_PER_VIDEO: "rl_video_password_per_video", + /** Public collection password attempts. Suggested: 20 / 10m per IP. */ + COLLECTION_PASSWORD: "rl_collection_password", + /** Collection password attempts across all clients. Suggested: 200 / 10m per key (collection). */ + COLLECTION_PASSWORD_PER_COLLECTION: "rl_collection_password_per_collection", } as const; type FallbackRateLimit = { limit: number; windowMs: number }; @@ -156,6 +164,16 @@ const FALLBACK_RATE_LIMITS: Record = { [RATE_LIMIT_IDS.DOCS_ASK]: { limit: 10, windowMs: MINUTE_MS }, [RATE_LIMIT_IDS.AUTH_SSO_START]: { limit: 100, windowMs: MINUTE_MS }, [RATE_LIMIT_IDS.MCP_CLIENT_REGISTRATION]: { limit: 60, windowMs: MINUTE_MS }, + [RATE_LIMIT_IDS.VIDEO_PASSWORD]: { limit: 20, windowMs: 10 * MINUTE_MS }, + [RATE_LIMIT_IDS.VIDEO_PASSWORD_PER_VIDEO]: { + limit: 200, + windowMs: 10 * MINUTE_MS, + }, + [RATE_LIMIT_IDS.COLLECTION_PASSWORD]: { limit: 20, windowMs: 10 * MINUTE_MS }, + [RATE_LIMIT_IDS.COLLECTION_PASSWORD_PER_COLLECTION]: { + limit: 200, + windowMs: 10 * MINUTE_MS, + }, }; const DEFAULT_FALLBACK_RATE_LIMIT: FallbackRateLimit = {