A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix: key rate limits by user id for authed requests instead of ip (#3283)
Mohamed Bassem1116e333c41e
diff --git a/packages/api/middlewares/rateLimit.test.ts b/packages/api/middlewares/rateLimit.test.tsindex a7c56c81..33e9b3bd 100644--- a/packages/api/middlewares/rateLimit.test.ts+++ b/packages/api/middlewares/rateLimit.test.ts@@ -19,7 +19,8 @@ vi.mock("@karakeep/shared/config", () => ({},}));-vi.mock("@karakeep/shared/ratelimiting", () => ({+vi.mock("@karakeep/shared/ratelimiting", async (original) => ({+ ...(await original<typeof import("@karakeep/shared/ratelimiting")>()),getRateLimitClient: getRateLimitClientMock,}));@@ -57,7 +58,7 @@ describe("createRateLimitMiddleware", () => {.get("/", (c) => c.text("ok"));}- it("includes the authed user id in the rate limit key", async () => {+ it("keys authed requests by user id instead of ip", async () => {const response = await buildApp({user: {id: "user-123",@@ -71,11 +72,11 @@ describe("createRateLimitMiddleware", () => {expect(response.status).toBe(200);expect(client.checkRateLimit).toHaveBeenCalledWith(config,- "127.0.0.1:user:user-123:assets.upload",+ "user:user-123:assets.upload",);});- it("keeps the existing ip-based key for unauthed requests", async () => {+ it("keys unauthed requests by ip", async () => {await buildApp({user: null,db: {} as never,@@ -84,7 +85,51 @@ describe("createRateLimitMiddleware", () => {expect(client.checkRateLimit).toHaveBeenCalledWith(config,- "127.0.0.1:assets.upload",+ "ip:127.0.0.1:assets.upload",);});++ it("rate limits authed requests even without an ip", async () => {+ await buildApp({+ user: {+ id: "user-123",+ email: "[email protected]",+ role: "user",+ },+ db: {} as never,+ req: { ip: null },+ }).request("http://localhost/");++ expect(client.checkRateLimit).toHaveBeenCalledWith(+ config,+ "user:user-123:assets.upload",+ );+ });++ it("skips rate limiting when there's neither a user nor an ip", async () => {+ const response = await buildApp({+ user: null,+ db: {} as never,+ req: { ip: null },+ }).request("http://localhost/");++ expect(response.status).toBe(200);+ expect(client.checkRateLimit).not.toHaveBeenCalled();+ });++ it("skips rate limiting for internal (impersonated) calls", async () => {+ const response = await buildApp({+ user: {+ id: "user-123",+ email: "[email protected]",+ role: "user",+ },+ auth: { type: "internal" },+ db: {} as never,+ req: { ip: null },+ }).request("http://localhost/");++ expect(response.status).toBe(200);+ expect(client.checkRateLimit).not.toHaveBeenCalled();+ });});diff --git a/packages/api/middlewares/rateLimit.ts b/packages/api/middlewares/rateLimit.tsindex 3ac1d1a5..cf288441 100644--- a/packages/api/middlewares/rateLimit.ts+++ b/packages/api/middlewares/rateLimit.ts@@ -3,7 +3,10 @@ import { HTTPException } from "hono/http-exception";import type { RateLimitConfig } from "@karakeep/shared/ratelimiting";import serverConfig from "@karakeep/shared/config";-import { getRateLimitClient } from "@karakeep/shared/ratelimiting";+import {+ getRateLimitClient,+ getRateLimitIdentity,+} from "@karakeep/shared/ratelimiting";import { Context } from "@karakeep/trpc";export function createRateLimitMiddleware(config: RateLimitConfig) {@@ -16,8 +19,12 @@ export function createRateLimitMiddleware(config: RateLimitConfig) {return next();}- const ip = c.var.ctx.req.ip;- if (!ip) {+ const identity = getRateLimitIdentity({+ userId: c.var.ctx.user?.id,+ ip: c.var.ctx.req.ip,+ internal: c.var.ctx.auth?.type === "internal",+ });+ if (!identity) {return next();}@@ -26,8 +33,7 @@ export function createRateLimitMiddleware(config: RateLimitConfig) {return next();}- const userSegment = c.var.ctx.user?.id ? `:user:${c.var.ctx.user.id}` : "";- const key = `${ip}${userSegment}:${config.name}`;+ const key = `${identity}:${config.name}`;const result = await client.checkRateLimit(config, key);if (!result.allowed) {diff --git a/packages/shared/ratelimiting.ts b/packages/shared/ratelimiting.tsindex 183720ac..5fdb0a29 100644--- a/packages/shared/ratelimiting.ts+++ b/packages/shared/ratelimiting.ts@@ -38,3 +38,28 @@ export interface RateLimitClient {export async function getRateLimitClient(): Promise<RateLimitClient | null> {return PluginManager.getClient(PluginType.RateLimit);}++/**+ * Build the identity part of a rate limit key. Authenticated requests are+ * keyed by user id so that a user gets a single bucket regardless of the IP+ * they come from (client IPs are derived from spoofable headers). Anonymous+ * requests fall back to the client IP.+ * @returns The identity key, or null if the request shouldn't be rate limited+ * (internal server-initiated calls, or requests that can't be identified).+ */+export function getRateLimitIdentity(opts: {+ userId?: string | null;+ ip?: string | null;+ internal?: boolean;+}): string | null {+ if (opts.internal) {+ return null;+ }+ if (opts.userId) {+ return `user:${opts.userId}`;+ }+ if (opts.ip) {+ return `ip:${opts.ip}`;+ }+ return null;+}diff --git a/packages/trpc/index.ts b/packages/trpc/index.tsindex 031494c4..cf04b9a5 100644--- a/packages/trpc/index.ts+++ b/packages/trpc/index.ts@@ -40,6 +40,11 @@ export type RequestAuth =| {type: "session";}+ | {+ // Server-initiated calls on behalf of a user (workers, impersonation).+ // These are exempt from user-facing rate limits.+ type: "internal";+ }| null;export interface Context {diff --git a/packages/trpc/lib/__tests__/impersonate.test.ts b/packages/trpc/lib/__tests__/impersonate.test.tsnew file mode 100644index 00000000..aaa1819c--- /dev/null+++ b/packages/trpc/lib/__tests__/impersonate.test.ts@@ -0,0 +1,83 @@+import { eq } from "drizzle-orm";+import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";++import { users } from "@karakeep/db/schema";+import serverConfig from "@karakeep/shared/config";+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";++import type { CustomTestContext } from "../../testUtils";+import { defaultBeforeEach } from "../../testUtils";+import { buildImpersonatingAuthedContext } from "../impersonate";++const { getRateLimitClientMock, checkRateLimitMock } = vi.hoisted(() => ({+ getRateLimitClientMock: vi.fn(),+ checkRateLimitMock: vi.fn(),+}));++vi.mock("@karakeep/shared/ratelimiting", async (original) => ({+ ...(await original<typeof import("@karakeep/shared/ratelimiting")>()),+ getRateLimitClient: getRateLimitClientMock,+}));++let wasRateLimitingEnabled: boolean;++async function getFirstUserId(db: CustomTestContext["db"]) {+ const user = await db.query.users.findFirst({+ where: eq(users.email, "[email protected]"),+ });+ return user!.id;+}++beforeEach<CustomTestContext>(async (context) => {+ await defaultBeforeEach(true)(context);+ wasRateLimitingEnabled = serverConfig.rateLimiting.enabled;+ serverConfig.rateLimiting.enabled = true;+ // Every rate limit check fails, so any request that reaches the limiter is+ // rejected.+ checkRateLimitMock.mockReset();+ checkRateLimitMock.mockResolvedValue({ allowed: false, resetInSeconds: 60 });+ getRateLimitClientMock.mockResolvedValue({+ checkRateLimit: checkRateLimitMock,+ });+});++afterEach(() => {+ serverConfig.rateLimiting.enabled = wasRateLimitingEnabled;+});++describe("buildImpersonatingAuthedContext", () => {+ test<CustomTestContext>("marks the context as internal", async ({ db }) => {+ const userId = await getFirstUserId(db);+ const ctx = await buildImpersonatingAuthedContext(userId, db);+ expect(ctx.auth).toEqual({ type: "internal" });+ });++ test<CustomTestContext>("impersonated calls bypass user-facing rate limits", async ({+ db,+ }) => {+ const userId = await getFirstUserId(db);+ // Imported lazily so that testUtils' queue mocks are in place first.+ const { createCallerFactory } = await import("../..");+ const { appRouter } = await import("../../routers/_app");+ const caller = createCallerFactory(appRouter)(+ await buildImpersonatingAuthedContext(userId, db),+ );++ const bookmark = await caller.bookmarks.createBookmark({+ type: BookmarkTypes.LINK,+ url: "https://example.com",+ });+ expect(bookmark.id).toBeDefined();+ });++ test<CustomTestContext>("session calls for the same user are still rate limited", async ({+ apiCallers,+ }) => {+ await expect(+ apiCallers[0].bookmarks.createBookmark({+ type: BookmarkTypes.LINK,+ url: "https://example.com",+ }),+ ).rejects.toMatchObject({ code: "TOO_MANY_REQUESTS" });+ });+});diff --git a/packages/trpc/lib/__tests__/rateLimit.test.ts b/packages/trpc/lib/__tests__/rateLimit.test.tsindex 4e47cf23..af493699 100644--- a/packages/trpc/lib/__tests__/rateLimit.test.ts+++ b/packages/trpc/lib/__tests__/rateLimit.test.ts@@ -17,7 +17,8 @@ vi.mock("@karakeep/shared/config", () => ({},}));-vi.mock("@karakeep/shared/ratelimiting", () => ({+vi.mock("@karakeep/shared/ratelimiting", async (original) => ({+ ...(await original<typeof import("@karakeep/shared/ratelimiting")>()),getRateLimitClient: getRateLimitClientMock,}));@@ -41,7 +42,7 @@ describe("createRateLimitMiddleware", () => {getRateLimitClientMock.mockResolvedValue(client);});- it("includes the authed user id in the rate limit key", async () => {+ it("keys authed requests by user id instead of ip", async () => {const next = vi.fn().mockResolvedValue("ok");const middleware = createRateLimitMiddleware(config);@@ -57,11 +58,11 @@ describe("createRateLimitMiddleware", () => {expect(result).toBe("ok");expect(client.checkRateLimit).toHaveBeenCalledWith(config,- "127.0.0.1:user:user-123:bookmarks.create",+ "user:user-123:bookmarks.create",);});- it("keeps the existing ip-based key for unauthed requests", async () => {+ it("keys unauthed requests by ip", async () => {const next = vi.fn().mockResolvedValue("ok");const middleware = createRateLimitMiddleware(config);@@ -76,7 +77,82 @@ describe("createRateLimitMiddleware", () => {expect(client.checkRateLimit).toHaveBeenCalledWith(config,- "127.0.0.1:users.create",+ "ip:127.0.0.1:users.create",);});++ it("rate limits authed requests even without an ip", async () => {+ const next = vi.fn().mockResolvedValue("ok");+ const middleware = createRateLimitMiddleware(config);++ await middleware({+ path: "bookmarks.create",+ ctx: {+ req: { ip: null },+ user: { id: "user-123" },+ },+ next,+ });++ expect(client.checkRateLimit).toHaveBeenCalledWith(+ config,+ "user:user-123:bookmarks.create",+ );+ });++ it("skips rate limiting when there's neither a user nor an ip", async () => {+ const next = vi.fn().mockResolvedValue("ok");+ const middleware = createRateLimitMiddleware(config);++ const result = await middleware({+ path: "users.create",+ ctx: {+ req: { ip: null },+ user: null,+ },+ next,+ });++ expect(result).toBe("ok");+ expect(client.checkRateLimit).not.toHaveBeenCalled();+ });++ it("rejects requests over the limit", async () => {+ vi.mocked(client.checkRateLimit).mockResolvedValueOnce({+ allowed: false,+ resetInSeconds: 30,+ });+ const next = vi.fn().mockResolvedValue("ok");+ const middleware = createRateLimitMiddleware(config);++ await expect(+ middleware({+ path: "bookmarks.create",+ ctx: {+ req: { ip: "127.0.0.1" },+ user: { id: "user-123" },+ },+ next,+ }),+ ).rejects.toMatchObject({ code: "TOO_MANY_REQUESTS" });+ expect(next).not.toHaveBeenCalled();+ });++ it("skips rate limiting for internal (impersonated) calls", async () => {+ const next = vi.fn().mockResolvedValue("ok");+ const middleware = createRateLimitMiddleware(config);++ const result = await middleware({+ path: "bookmarks.createBookmark",+ ctx: {+ req: { ip: null },+ user: { id: "user-123" },+ auth: { type: "internal" },+ },+ next,+ });++ expect(result).toBe("ok");+ expect(client.checkRateLimit).not.toHaveBeenCalled();+ });});diff --git a/packages/trpc/lib/impersonate.ts b/packages/trpc/lib/impersonate.tsindex 993c66ba..157009de 100644--- a/packages/trpc/lib/impersonate.ts+++ b/packages/trpc/lib/impersonate.ts@@ -23,6 +23,7 @@ export async function buildImpersonatingAuthedContext(email: user.email,role: user.role,},+ auth: { type: "internal" },db,req: {ip: null,diff --git a/packages/trpc/lib/rateLimit.ts b/packages/trpc/lib/rateLimit.tsindex 2f064cea..42db9e1a 100644--- a/packages/trpc/lib/rateLimit.ts+++ b/packages/trpc/lib/rateLimit.ts@@ -2,7 +2,10 @@ import { TRPCError } from "@trpc/server";import type { RateLimitConfig } from "@karakeep/shared/ratelimiting";import serverConfig from "@karakeep/shared/config";-import { getRateLimitClient } from "@karakeep/shared/ratelimiting";+import {+ getRateLimitClient,+ getRateLimitIdentity,+} from "@karakeep/shared/ratelimiting";/*** Create a tRPC middleware for rate limiting@@ -15,6 +18,7 @@ export function createRateLimitMiddleware<T>(config: RateLimitConfig) {ctx: {req: { ip: string | null };user?: { id?: string | null } | null;+ auth?: { type: string } | null;};next: () => Promise<T>;}) {@@ -22,9 +26,13 @@ export function createRateLimitMiddleware<T>(config: RateLimitConfig) {return opts.next();}- const ip = opts.ctx.req.ip;+ const identity = getRateLimitIdentity({+ userId: opts.ctx.user?.id,+ ip: opts.ctx.req.ip,+ internal: opts.ctx.auth?.type === "internal",+ });- if (!ip) {+ if (!identity) {return opts.next();}@@ -35,8 +43,7 @@ export function createRateLimitMiddleware<T>(config: RateLimitConfig) {return opts.next();}- const userSegment = opts.ctx.user?.id ? `:user:${opts.ctx.user.id}` : "";- const key = `${ip}${userSegment}:${opts.path}`;+ const key = `${identity}:${opts.path}`;const result = await client.checkRateLimit(config, key);if (!result.allowed) {diff --git a/packages/trpc/routers/bookmarks.search.test.ts b/packages/trpc/routers/bookmarks.search.test.tsindex 3102faf3..35fc82b6 100644--- a/packages/trpc/routers/bookmarks.search.test.ts+++ b/packages/trpc/routers/bookmarks.search.test.ts@@ -121,7 +121,7 @@ describe("bookmark search modes", () => {windowMs: 60_000,maxRequests: 300,},- "127.0.0.1:user:user-123:bookmarks.searchBookmarks",+ "user:user-123:bookmarks.searchBookmarks",);} finally {serverConfig.rateLimiting.enabled = wasRateLimitingEnabled;