mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
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) <noreply@anthropic.com> * feedback --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2beacf9d56
commit
7652d26d07
3 files changed
+96
-8
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user