Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+38
-18
@@ -66,34 +66,54 @@ export function mfaChallengeRateLimitKey(request: Request, userId: string): MfaC
|
||||
}
|
||||
|
||||
export async function checkMfaChallengeRateLimit(key: MfaChallengeRateLimitKey, now: Date = new Date()): Promise<MfaChallengeRateLimitResult> {
|
||||
const { store } = getLimiters();
|
||||
const [perIpCount, perUserCount] = await Promise.all([
|
||||
peekCount(store, `rl:fixed:mfa-challenge:${key.perIp}`),
|
||||
peekCount(store, `rl:fixed:mfa-challenge-user:${key.userId}`),
|
||||
]);
|
||||
// Block as soon as either bound is reached. The per-user window is longer, so
|
||||
// report its reset when it is the binding constraint.
|
||||
if (perUserCount >= MFA_CHALLENGE_USER_LIMIT) {
|
||||
return { allowed: false, retryAfterSeconds: MFA_CHALLENGE_USER_WINDOW_SECONDS, resetAt: new Date(now.getTime() + MFA_CHALLENGE_USER_WINDOW_SECONDS * 1000) };
|
||||
}
|
||||
if (perIpCount >= MFA_CHALLENGE_LIMIT) {
|
||||
const { perIp, perUser, store } = getLimiters();
|
||||
// Reserve before TOTP/recovery-code verification. Read-then-consume permits
|
||||
// every request in a parallel burst to observe the same old count.
|
||||
const perIpReservation = await reserveWithCorruptionRecovery(
|
||||
perIp,
|
||||
store,
|
||||
key.perIp,
|
||||
`rl:fixed:mfa-challenge:${key.perIp}`,
|
||||
);
|
||||
if (!perIpReservation.allowed) {
|
||||
return { allowed: false, retryAfterSeconds: MFA_CHALLENGE_WINDOW_SECONDS, resetAt: new Date(now.getTime() + MFA_CHALLENGE_WINDOW_SECONDS * 1000) };
|
||||
}
|
||||
const perUserReservation = await reserveWithCorruptionRecovery(
|
||||
perUser,
|
||||
store,
|
||||
key.userId,
|
||||
`rl:fixed:mfa-challenge-user:${key.userId}`,
|
||||
);
|
||||
if (!perUserReservation.allowed) {
|
||||
return { allowed: false, retryAfterSeconds: MFA_CHALLENGE_USER_WINDOW_SECONDS, resetAt: new Date(now.getTime() + MFA_CHALLENGE_USER_WINDOW_SECONDS * 1000) };
|
||||
}
|
||||
return {
|
||||
allowed: true,
|
||||
remaining: Math.min(MFA_CHALLENGE_LIMIT - perIpCount, MFA_CHALLENGE_USER_LIMIT - perUserCount),
|
||||
remaining: Math.min(perIpReservation.remaining, perUserReservation.remaining),
|
||||
resetAt: new Date(now.getTime() + MFA_CHALLENGE_WINDOW_SECONDS * 1000),
|
||||
};
|
||||
}
|
||||
|
||||
async function peekCount(store: SharedStore, suffix: string): Promise<number> {
|
||||
const raw = await store.get(suffix);
|
||||
return raw === null ? 0 : Number.parseInt(raw, 10) || 0;
|
||||
export async function recordFailedMfaChallengeAttempt(key: MfaChallengeRateLimitKey, _now: Date = new Date()): Promise<void> {
|
||||
// checkMfaChallengeRateLimit already reserved this attempt before verification.
|
||||
void key;
|
||||
void _now;
|
||||
}
|
||||
|
||||
export async function recordFailedMfaChallengeAttempt(key: MfaChallengeRateLimitKey, _now: Date = new Date()): Promise<void> {
|
||||
const { perIp, perUser } = getLimiters();
|
||||
await Promise.all([perIp.consume(key.perIp), perUser.consume(key.userId)]);
|
||||
async function reserveWithCorruptionRecovery(
|
||||
limiter: FixedWindowRateLimiter,
|
||||
store: SharedStore,
|
||||
key: string,
|
||||
storageKey: string,
|
||||
) {
|
||||
try {
|
||||
return await limiter.consume(key);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
if (!/non-numeric|not an integer/i.test(message)) throw error;
|
||||
await store.del(storageKey);
|
||||
return limiter.consume(key);
|
||||
}
|
||||
}
|
||||
|
||||
export async function resetMfaChallengeRateLimit(key: MfaChallengeRateLimitKey): Promise<void> {
|
||||
|
||||
Reference in new issue
Block a user