fix(security): address PR 48 findings with bounded concurrent authentication

Classify IPv6 subnets numerically, reserve login capacity before hashing, bind authentication to credential revisions, revoke other sessions on password rotation, reuse trusted proxy client keys and bound PCM encoding by monotonic time. Credit ph1gros for the findings and proposed regression cases.
This commit is contained in:
illusia committed 2026-10-26 10:28:45 +08:00
1 parent 8bca848c21
commit 275e27c4ac
31 files changed
+449 -84

No files matched your search

+32 -29
View File
@@ -1,51 +1,54 @@
import { performance } from "node:perf_hooks";
const WINDOW_MS = 5 * 60 * 1000;
const MAX_FAILURES = 5;
interface LoginAttempt {
failures: number;
pending: number;
firstFailureAt: number;
}
type Reservation = { allowed: false; retryAfterMs: number }
| { allowed: true; finish(result: "success" | "failure" | "cancelled"): number };
export class AdminLoginRateLimiter {
private readonly attempts = new Map<string, LoginAttempt>();
private pending = 0;
constructor(private readonly maxTrackedPeers = 1024) {}
constructor(private readonly maxTrackedPeers = 1024, private readonly maxConcurrentChecks = 8) {}
retryAfterMs(peer: string, now = Date.now()): number {
const attempt = this.attempts.get(peer);
if (!attempt) return 0;
if (now - attempt.firstFailureAt >= WINDOW_MS) {
this.attempts.delete(peer);
return 0;
}
if (attempt.failures < MAX_FAILURES) return 0;
return WINDOW_MS - (now - attempt.firstFailureAt);
}
recordFailure(peer: string, now = Date.now()): number {
/** Reserve before hashing; pending checks count against capacity, successes are not failures. */
reserve(peer: string, now = performance.now()): Reservation {
this.pruneExpired(now);
const current = this.attempts.get(peer);
if (!current) {
this.pruneExpired(now);
while (this.attempts.size >= this.maxTrackedPeers) {
const oldestPeer = this.attempts.keys().next().value;
if (!oldestPeer) break;
this.attempts.delete(oldestPeer);
}
if (current && current.failures + current.pending >= MAX_FAILURES) {
return { allowed: false, retryAfterMs: Math.max(1000, WINDOW_MS - (now - current.firstFailureAt)) };
}
const attempt = !current || now - current.firstFailureAt >= WINDOW_MS
? { failures: 1, firstFailureAt: now }
: { ...current, failures: current.failures + 1 };
if (this.pending >= this.maxConcurrentChecks || (!current && this.attempts.size >= this.maxTrackedPeers)) {
// Never evict another visitor's lockout or pending check to admit a new address.
return { allowed: false, retryAfterMs: 1000 };
}
const attempt = current ?? { failures: 0, pending: 0, firstFailureAt: now };
this.attempts.set(peer, attempt);
return Math.min(2000, 250 * (2 ** Math.min(3, attempt.failures - 1)));
}
recordSuccess(peer: string): void {
this.attempts.delete(peer);
attempt.pending++;
this.pending++;
let finished = false;
return { allowed: true, finish: result => {
if (finished) return 0;
finished = true;
this.pending--;
attempt.pending--;
if (result === "success") attempt.failures = 0;
else if (result === "failure") attempt.failures++;
if (!attempt.failures && !attempt.pending) this.attempts.delete(peer);
return result === "failure" ? Math.min(2000, 250 * 2 ** Math.min(3, attempt.failures - 1)) : 0;
} };
}
private pruneExpired(now: number): void {
for (const [peer, attempt] of this.attempts) {
if (now - attempt.firstFailureAt >= WINDOW_MS) this.attempts.delete(peer);
if (!attempt.pending && now - attempt.firstFailureAt >= WINDOW_MS) this.attempts.delete(peer);
}
}
}