mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-07 14:12:56 +00:00
fix(gateway): clamp auth limiter prune intervals
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { MAX_TIMER_TIMEOUT_MS } from "../shared/number-coercion.js";
|
||||
import {
|
||||
AUTH_RATE_LIMIT_SCOPE_DEVICE_TOKEN,
|
||||
AUTH_RATE_LIMIT_SCOPE_HOOK_AUTH,
|
||||
@@ -238,6 +239,19 @@ describe("auth rate limiter", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("clamps oversized positive auto-prune intervals", () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const setIntervalSpy = vi.spyOn(globalThis, "setInterval");
|
||||
|
||||
limiter = createAuthRateLimiter({ pruneIntervalMs: Number.MAX_SAFE_INTEGER });
|
||||
|
||||
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
// ---------- undefined / empty IP ----------
|
||||
|
||||
it("normalizes undefined IP to 'unknown'", () => {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
* {@link createAuthRateLimiter} and pass it where needed.
|
||||
*/
|
||||
|
||||
import { resolveTimerTimeoutMs } from "../shared/number-coercion.js";
|
||||
import { isLoopbackAddress, resolveClientIp } from "./net.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -96,12 +97,22 @@ export function normalizeRateLimitClientIp(ip: string | undefined): string {
|
||||
return resolveClientIp({ remoteAddr: ip }) ?? "unknown";
|
||||
}
|
||||
|
||||
function resolvePruneIntervalMs(value: number | undefined): number {
|
||||
if (value === undefined) {
|
||||
return PRUNE_INTERVAL_MS;
|
||||
}
|
||||
if (Number.isFinite(value) && value <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return resolveTimerTimeoutMs(value, PRUNE_INTERVAL_MS);
|
||||
}
|
||||
|
||||
export function createAuthRateLimiter(config?: RateLimitConfig): AuthRateLimiter {
|
||||
const maxAttempts = config?.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
|
||||
const windowMs = config?.windowMs ?? DEFAULT_WINDOW_MS;
|
||||
const lockoutMs = config?.lockoutMs ?? DEFAULT_LOCKOUT_MS;
|
||||
const exemptLoopback = config?.exemptLoopback ?? true;
|
||||
const pruneIntervalMs = config?.pruneIntervalMs ?? PRUNE_INTERVAL_MS;
|
||||
const pruneIntervalMs = resolvePruneIntervalMs(config?.pruneIntervalMs);
|
||||
|
||||
const entries = new Map<string, RateLimitEntry>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user