From 7652d26d07307df2ab1ef3377df2bc1dd8a3da0c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 21 Aug 2026 20:59:21 -0400 Subject: [PATCH] fix: Consistent rate limiter keys across path variants (#13504) * fix: Normalize path when keying rate limiters The router matches paths case-insensitively and with an optional trailing slash, so those variants each received their own quota. Collapse them onto a single key at both registration and consumption. Co-Authored-By: Claude Opus 5 (1M context) * feedback --------- Co-authored-by: Claude Opus 5 (1M context) --- server/middlewares/rateLimiter.test.ts | 64 ++++++++++++++++++++++++++ server/middlewares/rateLimiter.ts | 8 +++- server/utils/RateLimiter.ts | 32 ++++++++++--- 3 files changed, 96 insertions(+), 8 deletions(-) diff --git a/server/middlewares/rateLimiter.test.ts b/server/middlewares/rateLimiter.test.ts index c57f5e7274..770f6af3bc 100644 --- a/server/middlewares/rateLimiter.test.ts +++ b/server/middlewares/rateLimiter.test.ts @@ -122,6 +122,70 @@ describe("rateLimiter middleware", () => { expect(limiter.points).toBe(1); }); + it("registers one limiter for case and trailing slash variants of a path", async () => { + const registerMiddleware = rateLimiter({ duration: 60, requests: 5 }); + + await registerMiddleware( + createContext({ path: "/documents.export", mountPath: "/api" }), + vi.fn() + ); + await registerMiddleware( + createContext({ path: "/Documents.Export", mountPath: "/API" }), + vi.fn() + ); + await registerMiddleware( + createContext({ path: "/documents.export/", mountPath: "/api" }), + vi.fn() + ); + + expect(RateLimiter.rateLimiterMap.size).toBe(1); + + const limiter = RateLimiter.getRateLimiter("/api/documents.export"); + for (const variant of [ + "/API/Documents.Export", + "/api/documents.export/", + "/API/Documents.Export/", + ]) { + expect(RateLimiter.hasRateLimiter(variant)).toBe(true); + expect(RateLimiter.getRateLimiter(variant)).toBe(limiter); + } + }); + + it("consumes one bucket for case and trailing slash variants of a path", async () => { + const registerMiddleware = rateLimiter({ duration: 60, requests: 5 }); + await registerMiddleware( + createContext({ path: "/documents.export", mountPath: "/api" }), + vi.fn() + ); + + const customLimiter = RateLimiter.getRateLimiter("/api/documents.export"); + const consumeSpy = vi + .spyOn(customLimiter, "consume") + .mockResolvedValue({} as never); + + const middleware = defaultRateLimiter(); + for (const path of [ + "/documents.export", + "/Documents.Export", + "/documents.export/", + ]) { + await middleware( + createContext({ path, mountPath: "/api", ip: "127.0.0.1" }), + vi.fn() + ); + } + + expect(consumeSpy.mock.calls.map(([key]) => key)).toEqual([ + "/api/documents.export:127.0.0.1", + "/api/documents.export:127.0.0.1", + "/api/documents.export:127.0.0.1", + ]); + }); + + it("keeps the root path when normalizing", () => { + expect(RateLimiter.normalizePath("/")).toBe("/"); + }); + it("should use default rate limiter when no custom rate limiter is registered", async () => { const fullPath = "/some/random/path"; expect(RateLimiter.hasRateLimiter(fullPath)).toBe(false); diff --git a/server/middlewares/rateLimiter.ts b/server/middlewares/rateLimiter.ts index c0a645487a..1ddfb8bc29 100644 --- a/server/middlewares/rateLimiter.ts +++ b/server/middlewares/rateLimiter.ts @@ -115,7 +115,9 @@ export function defaultRateLimiter() { return next(); } - const fullPath = `${ctx.mountPath ?? ""}${ctx.path}`; + const fullPath = RateLimiter.normalizePath( + `${ctx.mountPath ?? ""}${ctx.path}` + ); const identifiers = await getRateLimiterIdentifiers(ctx); // Kept for a route that registers its own limiter further down the chain, @@ -151,7 +153,9 @@ export function rateLimiter(config: RateLimiterConfig) { return next(); } - const fullPath = `${ctx.mountPath ?? ""}${ctx.path}`; + const fullPath = RateLimiter.normalizePath( + `${ctx.mountPath ?? ""}${ctx.path}` + ); if (!RateLimiter.hasRateLimiter(fullPath)) { const points = Math.max( diff --git a/server/utils/RateLimiter.ts b/server/utils/RateLimiter.ts index 29a15a2359..e3b2c0a543 100644 --- a/server/utils/RateLimiter.ts +++ b/server/utils/RateLimiter.ts @@ -50,36 +50,56 @@ export default class RateLimiter { return this._defaultRateLimiter; } + /** + * Normalizes a request path into the key that identifies its rate limiter. + * The router matches paths case-insensitively and with an optional trailing + * slash, so every one of those variants must collapse onto a single key. + * + * @param path the request path to normalize. + * @returns the normalized path. + */ + static normalizePath(path: string): string { + const lowercased = path.toLowerCase(); + return lowercased.length > 1 && lowercased.endsWith("/") + ? lowercased.slice(0, -1) + : lowercased; + } + /** * Returns the rate limiter registered for a path, falling back to the - * default rate limiter. + * default rate limiter. The path is normalized before lookup. * * @param path the request path to look up. * @returns the rate limiter for the path. */ static getRateLimiter(path: string): RateLimiterRedis { - return this.rateLimiterMap.get(path) || this.defaultRateLimiter; + return ( + this.rateLimiterMap.get(this.normalizePath(path)) || + this.defaultRateLimiter + ); } /** - * Registers a rate limiter with a custom configuration for a path. + * Registers a rate limiter with a custom configuration for a path. The path + * is normalized before registration. * * @param path the request path to register the rate limiter for. * @param config the rate limiter configuration. */ static setRateLimiter(path: string, config: IRateLimiterStoreOptions): void { const rateLimiter = new RateLimiterRedis(config); - this.rateLimiterMap.set(path, rateLimiter); + this.rateLimiterMap.set(this.normalizePath(path), rateLimiter); } /** - * Checks whether a custom rate limiter is registered for a path. + * Checks whether a custom rate limiter is registered for a path. The path is + * normalized before lookup. * * @param path the request path to check. * @returns true if a custom rate limiter is registered. */ static hasRateLimiter(path: string): boolean { - return this.rateLimiterMap.has(path); + return this.rateLimiterMap.has(this.normalizePath(path)); } /**